opencv检测图片上七种颜色,分辨颜色和对应位置

读取图片:使用cv2.imread()函数读取目标图片。

转换颜色空间:通常在HSV颜色空间中进行颜色检测,因为HSV颜色空间更适合描述颜色的属性。

定义颜色范围:为七种颜色定义HSV范围。例如,红色、绿色、蓝色、黄色、紫色、橙色和青色。

创建掩码:使用cv2.inRange()函数根据定义的颜色范围创建掩码。

位运算:使用掩码与原图进行位运算,提取特定颜色的区域。

查找轮廓:使用cv2.findContours()函数查找颜色区域的轮廓。

绘制轮廓:使用cv2.drawContours()函数在原图上绘制轮廓,以标记颜色的位置。

颜色识别:根据轮廓的位置和掩码,识别颜色。

import cv2
import numpy as np


def color_detection():
    """
    颜色检测
    :return:
    """
    # 读取图片
    image = cv2.imread(r'C:\Users\O_zhenhua.zhang\Desktop\cdc\AutoVehicleControl\vehicle_control_tools\received_image.jpg')

    # 转换颜色空间从BGR到HSV
    hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

    # 定义颜色的HSV范围
    color_ranges = {
        'red':((0, 120, 70), (10, 255, 255)),
        'green': ((40, 40, 40), (80, 255, 255)),
        'blue': ((110, 110, 140), (140, 255, 255)),
        'yellow': ((20, 40, 40), (40, 255, 255)),
        'purple': ((120, 120, 140), (160, 255, 255)),
        'orange': ((10, 40, 40), (20, 255, 255)),
        'cyan': ((160, 160, 180), (180, 255, 255))
    }

    # 检测颜色并绘制轮廓
    for color_name, (lower, upper) in color_ranges.items():
        # 创建颜色掩码
        mask = cv2.inRange(hsv_image, np.array(lower), np.array(upper))

        # 用掩码提取特定颜色的区域
        color_image = cv2.bitwise_and(image, image, mask=mask)

        # 寻找轮廓
        contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        # 绘制轮廓
        for contour in contours:
            area = cv2.contourArea(contour)
            if area > 1000:  # 可根据需要调整面积阈值
                cv2.drawContours(image, [contour], -1, (0, 255, 0), 3)  # 绿色轮廓

    # 显示结果
    cv2.imshow('Color Detection', image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == '__main__':
    color_detection()
GitHub 加速计划 / opencv31 / opencv
142
15
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:3 个月前 )
d9a139f9 Animated WebP Support #25608 related issues #24855 #22569 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 2 天前
09030615 V4l default image size #25500 Added ability to set default image width and height for V4L capture. This is required for cameras that does not support 640x480 resolution because otherwise V4L capture cannot be opened and failed with "Pixel format of incoming image is unsupported by OpenCV" and then with "can't open camera by index" message. Because of the videoio architecture it is not possible to insert actions between CvCaptureCAM_V4L::CvCaptureCAM_V4L and CvCaptureCAM_V4L::open so the only way I found is to use environment variables to preselect the resolution. Related bug report is [#25499](https://github.com/opencv/opencv/issues/25499) Maybe (but not confirmed) this is also related to [#24551](https://github.com/opencv/opencv/issues/24551) This fix was made and verified in my local environment: capture board AVMATRIX VC42, Ubuntu 20, NVidia Jetson Orin. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [X] I agree to contribute to the project under Apache 2 License. - [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [X] The PR is proposed to the proper branch - [X] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 2 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐