Python-opencv实现红绿两色识别操作
作者:faith003 时间:2021-05-04 18:35:51
1.颜色空间转换(RGB转HSV)
为了较准确的调红色和绿色的HSV,我使用cv2.createTrackbar()函数创建了六个滚动条
#创建HSV最低滚动条
cv2.createTrackbar('H_min','image',35,180,nothing)
cv2.createTrackbar('S_min','image',43,255,nothing)
cv2.createTrackbar('V_min','image',46,255,nothing)
#创建HSV最高滚动条
cv2.createTrackbar('H_max','image',0,180,nothing)
cv2.createTrackbar('S_max','image',255,255,nothing)
cv2.createTrackbar('V_max','image',255,255,nothing)
实际效果如图
2.识别颜色并画矩形框
颜色阈值已经确定了,这就可以进行颜色识别了。
为了让识别更稳定,在代码中加入自适应阈值。
th_img = cv2.adaptiveThreshold(mask,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,5,2)
3.画矩形框
使用函数cv2.findContours()来检测物体轮框
再使用函数cv2.boundingRect()查找最小矩形框
使用函数cv2.rectangle()画出
contours_green,hierarchy = cv2.findContours(th_green,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
contours_red,hierarchy = cv2.findContours(th_red,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for red in contours_red:
x_red,y_red,w_red,h_red = cv2.boundingRect(red)
if w_red>width|h_red>height:
cv2.rectangle(img,(x_red,y_red),((x_red+h_red),(y_red+w_red)),(0,255,0),1)
for red in contours_red:
x_red,y_red,w_red,h_red = cv2.boundingRect(red)
if w_red>width|h_red>height:
cv2.rectangle(img,(x_red,y_red),((x_red+h_red),(y_red+w_red)),(0,255,0),1)
为了凸显出颜色的差距,我使用绿色的矩形框,画红色的物体,用红色的矩形框画绿色物体
来源:https://blog.csdn.net/faith003/article/details/97610992
标签:Python,opencv,红绿
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
网页设计求职全攻略
2008-07-09 18:56:00
MySQL列类型的选择与MySQL的查询效率
2009-01-14 12:53:00
asp使用 sql_dmo 添加新数据库代码
2010-03-17 20:57:00
Python读取stdin方法实例
2022-09-07 19:03:00
![](https://img.aspxhome.com/file/2023/9/86159_0s.png)
防注入asp过滤sql特殊字符函数
2007-10-23 17:50:00
javascript 45种缓动效果(一)
2009-09-19 18:30:00
pytorch中使用LSTM详解
2021-01-08 04:27:10
![](https://img.aspxhome.com/file/2023/4/89394_0s.jpg)
如何快速定位页面中复杂 CSS BUG 问题
2009-01-15 12:23:00
使用php将某个目录下面的所有文件罗列出来的方法详解
2023-09-29 10:47:05
PHPStudy hosts文件可能不存在或被阻止打开及同步hosts失败问题
2023-06-08 10:29:10
![](https://img.aspxhome.com/file/2023/4/55494_0s.jpg)
Sun正式发布MySQL 5.1版 简化数据库应用
2008-12-11 15:15:00
获取Django项目的全部url方法详解
2022-09-08 23:06:27
Laravel中间件的使用详解
2023-05-27 10:16:40
FusionCharts图表显示双Y轴双(多)曲线
2023-08-22 17:55:38
![](https://img.aspxhome.com/file/2023/9/56199_0s.png)
Python处理中文标点符号大集合
2021-12-07 16:03:45
ubuntu下简单配置mysql数据库
2009-07-31 09:17:00
Python调用百度api实现语音识别详解
2023-03-10 05:10:17
![](https://img.aspxhome.com/file/2023/8/67258_0s.jpg)
Django实现微信小程序的登录验证功能并维护登录态
2022-03-14 22:56:48
![](https://img.aspxhome.com/file/2023/6/76826_0s.png)
numpy使用fromstring创建矩阵的实例
2022-02-23 09:29:49
简单获取键盘的KeyCode
2008-04-18 12:37:00