OpenCV+python识别并打印HSV颜色

import cv2
import imutils


cap = cv2.VideoCapture(0)
## Read
img = cv2.imread("D:/deng/ppp/3.png")


def Detector_color():
    while(True):
        # Capture frame-by-frame

        ret, frame = cap.read()

        frame = imutils.resize(frame, width=600)
        # Our operations on the frame come here
        blurred = cv2.GaussianBlur(frame, (7, 7), 0)
        hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
        #hsv = cv2.GaussianBlur(hsv, (7, 7), 0)

        # perform edge detection, then perform a dilation + erosion to
        # close gaps in between object edges
        # edged = cv2.Canny(hsv, 50, 100)
        #inRange()函数用于图像颜色分割
        # edged = cv2.inRange(hsv, Redlower, Redupper)
        edged = cv2.inRange(hsv, (0, 0, 0), (180, 255, 255))
        edged = cv2.dilate(edged, None, iterations=2)
        edged = cv2.erode(edged, None, iterations=2)
        ## mask of green (36,0,0) ~ (70, 255,255)
        mask1 = cv2.inRange(hsv, (36, 43, 46), (70, 255, 255))

        ## mask o blue (100,43,46) ~ (124, 255, 255)
        mask2 = cv2.inRange(hsv, (100, 43, 46), (155, 255, 255))

        ##  red
        mask3 = cv2.inRange(hsv, (156, 43, 46), (180, 255, 255))
        mask = {"green": mask1, "blue": mask2, "red": mask3}
        for key, value in mask.items():
            target = cv2.bitwise_and(edged, edged, mask=value)
            cnts = cv2.findContours(target, cv2.RETR_EXTERNAL,
                                    cv2.CHAIN_APPROX_SIMPLE)[-2]

            for c in cnts:
                # if the contour is not sufficiently large, ignore it
                if cv2.contourArea(c) < 100:
                    continue
                else:
                    # y = i
                    print(key)
                    # return c


Detector_color()

仅需要将摄像头对准有颜色的物体即可。运行结果:
在这里插入图片描述
参考:
如何在Python-OpenCV中使用cv2.inRange检测两种不同的颜色?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值