博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OpenCV+python识别并打印HSV颜色
阅读量:2109 次
发布时间:2019-04-29

本文共 1759 字,大约阅读时间需要 5 分钟。

import cv2import imutilscap = cv2.VideoCapture(0)## Readimg = 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 cDetector_color()

仅需要将摄像头对准有颜色的物体即可。运行结果:

在这里插入图片描述
参考:

转载地址:http://zpfef.baihongyu.com/

你可能感兴趣的文章
Leetcode C++《热题 Hot 100-48》406.根据身高重建队列
查看>>
《kubernetes权威指南·第四版》第二章:kubernetes安装配置指南
查看>>
Leetcode C++《热题 Hot 100-49》399.除法求值
查看>>
Leetcode C++《热题 Hot 100-51》152. 乘积最大子序列
查看>>
[Kick Start 2020] Round A 1.Allocation
查看>>
Leetcode C++ 《第181场周赛-1》 5364. 按既定顺序创建目标数组
查看>>
Leetcode C++ 《第181场周赛-2》 1390. 四因数
查看>>
阿里云《云原生》公开课笔记 第一章 云原生启蒙
查看>>
阿里云《云原生》公开课笔记 第二章 容器基本概念
查看>>
阿里云《云原生》公开课笔记 第三章 kubernetes核心概念
查看>>
阿里云《云原生》公开课笔记 第四章 理解Pod和容器设计模式
查看>>
阿里云《云原生》公开课笔记 第五章 应用编排与管理
查看>>
阿里云《云原生》公开课笔记 第六章 应用编排与管理:Deployment
查看>>
阿里云《云原生》公开课笔记 第七章 应用编排与管理:Job和DaemonSet
查看>>
阿里云《云原生》公开课笔记 第八章 应用配置管理
查看>>
阿里云《云原生》公开课笔记 第九章 应用存储和持久化数据卷:核心知识
查看>>
linux系统 阿里云源
查看>>
国内外helm源记录
查看>>
牛客网题目1:最大数
查看>>
散落人间知识点记录one
查看>>