OpenCV物体颜色检测(Python)
文章目录
- 前言
- 一、前期调查
- 二、方案
- 三、代码实现
- 效果展示
- 总结
前言
最近工作又来新活了,船舶颜色检测。开始接到这个活还是有点懵,后面慢慢的感觉来了!!!
一、前期调查
因为本项目涉及到颜色判断与分类,笔者一开始就想到的就是每种颜色的范围划分是什么,刚开始想的是否能够依据RGB值来划分颜色,很遗憾没找到各类颜色的RGB分割阈值,后来找到了关于HSV颜色模型的颜色分量范围资料。
OpenCV中HSV颜色模型及颜色分量范围
这给项目的颜色划分提供了依据。
二、方案
思路:由于项目已训练好的模型可以对船头进行定位截图,本文就默认对所给图片进行颜色判断。
步骤:
1.设定颜色的HSV的上下界;
2. 找出图片中属于设定颜色的区域;
3. 计算各个颜色区域的量化面积;
4. 通过颜色最大面积来判别所属颜色;
三、代码实现
# -*- coding:utf-8 -*-
# @Time : 2021/2/4 17:54
# @Author : JulyLi
# @File : 颜色总面积判断颜色.py
# @Software: PyCharmimport numpy as np
import cv2
import osfont = cv2.FONT_HERSHEY_SIMPLEX
lower_red = np.array([156, 43, 46]) # 红色阈值下界
higher_red = np.array([180, 255, 255]) # 红色阈值上界
lower_green = np.array([31, 43, 46]) # 绿色阈值下界
higher_green = np.array([77, 255, 255]) # 绿色阈值上界
lower_yellow = np.array([11, 43, 46]) # 黄色阈值下界
higher_yellow = np.array([30, 255, 255]) # 黄色阈值上界
lower_blue = np.array([78, 43, 46])
higher_blue = np.array([124, 255, 255]) # 蓝色
lower_gray = np.array([0, 0, 46])
higher_gray = np.array([180, 43, 220]) # 灰色
lower_black = np.array([0, 0, 0])
higher_black = np.array([180, 255, 46]) # 黑色
lower_white = np.array([0, 0, 221])
higher_white = np.array([180, 30, 225]) # 白色
lower_purple = np.array([125, 43, 46])
higher_purple = np.array([155, 255, 225]) # 紫色# frame = cv2.imread("shiphead/3.jpg")def color_detection(img):red_temp = [0]green_temp = [0]yellow_temp = [0]blue_temp = [0]black_temp = [0]white_temp = [0]gray_temp = [0]purple_temp = [0]maxsize_temp = {}frame = cv2.imread(img)img_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)mask_red = cv2.inRange(img_hsv, lower_red, higher_red) # 可以认为是过滤出红色部分,获得红色的掩膜mask_red = cv2.medianBlur(mask_red, 7) # 中值滤波mask_yellow = cv2.inRange(img_hsv, lower_yellow, higher_yellow) # 获得黄色部分掩膜mask_yellow = cv2.medianBlur(mask_yellow, 7) # 中值滤波mask_blue = cv2.inRange(img_hsv, lower_blue, higher_blue) # 获得蓝色部分掩膜mask_blue = cv2.medianBlur(mask_blue, 7) # 中值滤波mask_green = cv2.inRange(img_hsv, lower_green, higher_green) # 获得蓝色部分掩膜mask_green = cv2.medianBlur(mask_green, 7) # 中值滤波mask_black = cv2.inRange(img_hsv, lower_black, higher_black) # 获得黑色部分掩膜mask_black = cv2.medianBlur(mask_black, 7) # 中值滤波mask_while = cv2.inRange(img_hsv, lower_white, higher_white) # 获得白色部分掩膜mask_while = cv2.medianBlur(mask_while, 7) # 中值滤波mask_gray = cv2.inRange(img_hsv, lower_gray, higher_gray) # 获得灰色部分掩膜mask_gray = cv2.medianBlur(mask_gray, 7) # 中值滤波mask_purple = cv2.inRange(img_hsv, lower_purple, higher_purple) # 获得灰色部分掩膜mask_purple = cv2.medianBlur(mask_purple, 7) # 中值滤波# mask = cv2.bitwise_or(mask_green, mask_red) # 三部分掩膜进行按位或运算# print(mask_red)cnts1, hierarchy1 = cv2.findContours(mask_red, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # 轮廓检测 #红色cnts2, hierarchy2 = cv2.findContours(mask_blue, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # 轮廓检测 #蓝色cnts3, hierarchy3 = cv2.findContours(mask_yellow, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)cnts4, hierarchy4 = cv2.findContours(mask_black, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)cnts5, hierarchy5 = cv2.findContours(mask_while, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)cnts6, hierarchy6 = cv2.findContours(mask_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)cnts7, hierarchy7 = cv2.findContours(mask_green, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)cnts8, hierarchy8 = cv2.findContours(mask_purple, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)for cnt in cnts1:# (x, y, w, h) = cv2.boundingRect(cnt) # 该函数返回矩阵四个点# cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2) # 将检测到的颜色框起来# cv2.putText(frame, 'red', (x, y - 5), font, 0.7, (0, 0, 255), 2)# cv2.drawContours(frame, cnt, -1, (0, 0, 255), -1)size = cv2.contourArea(cnt)red_temp.append(size)for cnt in cnts2:# (x, y, w, h) = cv2.boundingRect(cnt) # 该函数返回矩阵四个点# cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) # 将检测到的颜色框起来# cv2.putText(frame, 'blue', (x, y - 5), font, 0.7, (255, 0, 0), 2)# cv2.drawContours(frame, cnt, -1, (255, 0, 0), -1)size = cv2.contourArea(cnt)blue_temp.append(size)for cnt in cnts3:# (x, y, w, h) = cv2.boundingRect(cnt) # 该函数返回矩阵四个点# cv2.rectangle(frame, (x, y), (x + w, y + h), (50, 50, 50), 2) # 将检测到的颜色框起来# cv2.putText(frame, 'yellow', (x, y - 5), font, 0.7, (50, 50, 50), 2)# cv2.drawContours(frame, cnt, -1, (50, 50, 50), -1)size = cv2.contourArea(cnt)yellow_temp.append(size)for cnt in cnts4:# (x, y, w, h) = cv2.boundingRect(cnt) # 该函数返回矩阵四个点# cv2.rectangle(frame, (x, y), (x + w, y + h), (88, 87, 86), 2) # 将检测到的颜色框起来# cv2.putText(frame, 'black', (x, y - 5), font, 0.7, (88, 87, 86), 2)# cv2.drawContours(frame, cnt, -1, (88, 87, 86), -1)size = cv2.contourArea(cnt)black_temp.append(size)for cnt in cnts5:# (x, y, w, h) = cv2.boundingRect(cnt) # 该函数返回矩阵四个点# cv2.rectangle(frame, (x, y), (x + w, y + h), (88, 87, 86), 2) # 将检测到的颜色框起来# cv2.putText(frame, 'white', (x, y - 5), font, 0.7, (88, 87, 86), 2)# cv2.drawContours(frame, cnt, -1, (88, 87, 86), -1)size = cv2.contourArea(cnt)white_temp.append(size)for cnt in cnts6:# (x, y, w, h) = cv2.boundingRect(cnt) # 该函数返回矩阵四个点# cv2.rectangle(frame, (x, y), (x + w, y + h), (220, 183, 183), 2) # 将检测到的颜色框起来# cv2.putText(frame, 'gray', (x, y - 5), font, 0.7, (220, 183, 183), 2)# cv2.drawContours(frame, cnt, -1, (220, 183, 183), -1)size = cv2.contourArea(cnt)gray_temp.append(size)for cnt in cnts7:# (x, y, w, h) = cv2.boundingRect(cnt) # 该函数返回矩阵四个点# cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # 将检测到的颜色框起来# cv2.putText(frame, 'green', (x, y - 5), font, 0.7, (0, 255, 0), 2)# cv2.drawContours(frame, cnt, -1, (0, 255, 0), -1)size = cv2.contourArea(cnt)green_temp.append(size)for cnt in cnts8:# (x, y, w, h) = cv2.boundingRect(cnt) # 该函数返回矩阵四个点# cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # 将检测到的颜色框起来# cv2.putText(frame, 'green', (x, y - 5), font, 0.7, (0, 255, 0), 2)# cv2.drawContours(frame, cnt, -1, (240, 32, 160), -1)size = cv2.contourArea(cnt)purple_temp.append(size)# print(max(red_temp))# print(max(gray_temp))# print(max(green_temp))# print(max(black_temp))# print(max(blue_temp))#黑色与灰色多为干扰颜色,故为其他颜色添加补偿系数maxsize_temp["red"] = sum(red_temp)*10maxsize_temp["blue"] = sum(blue_temp)*10maxsize_temp["green"] = sum(green_temp)*10maxsize_temp["yellow"] = sum(yellow_temp)*10maxsize_temp["black"] = max(black_temp)maxsize_temp["white"] = sum(white_temp)*10maxsize_temp["gray"] = max(gray_temp)maxsize_temp["purple"] = sum(purple_temp)*10# maxsize_temp["red"] = max(red_temp) * 10# maxsize_temp["blue"] = max(blue_temp) * 10# maxsize_temp["green"] = max(green_temp) * 10# maxsize_temp["yellow"] = max(yellow_temp) * 10# maxsize_temp["black"] = max(black_temp)# maxsize_temp["white"] = max(white_temp)*10# maxsize_temp["gray"] = max(gray_temp)# maxsize_temp["purple"] = max(purple_temp) * 10# print(maxsize_temp)d = list(zip(maxsize_temp.values(), maxsize_temp.keys()))d = sorted(d)# print(d)return d[-1][-1], frameif __name__ == '__main__':#单张测试# img = r"shiphead/1036.jpg"# color_final, img = color_detection(img)# print(color_final)# size = img.shape# w = size[1] # 宽度# h = size[0] # 高度# cv2.putText(img, color_final, (int(w/2), int(h/2)), font, 0.7, (0, 255, 0), 2)# cv2.imshow('frame', img)# # cv2.imwrite('res/res7.png', img)# cv2.waitKey(0)# cv2.destroyAllWindows()#批量测试img_dir = r'E:\opencv\colordetect\shiphead' # 原始文件目录# img_dir = r'E:\opencv\colordetect\test' # 原始文件目录save_dir = r'E:\opencv\colordetect\result3/' # 保存目录if not os.path.exists(save_dir):os.makedirs(save_dir)names = os.listdir(img_dir)for name in names:img_path = os.path.join(img_dir, name)# image = cv2.imread(img_path)color_final, img = color_detection(img_path)# print(color_final)size = img.shapew = size[1] # 宽度h = size[0] # 高度cv2.putText(img, color_final, (int(w / 2), int(h / 2)), font, 0.7, (255, 255, 255), 2)cv2.imwrite(save_dir + name, img)print("finished")
效果展示
总结
本方法是一种比较简单且通用的颜色检测方法,能满足简单真实场景下的颜色检测要求。
本方法不足之处为:
①准确度依赖剪裁图像的质量;
②颜色上下界阈值设定不一定符合人眼感受。
如果阅读本文对你有用,欢迎点赞关注评论收藏呀!!!
2021年2月5日17:57:46
OpenCV物体颜色检测(Python)相关推荐
- python颜色识别原理_python+opencv实现颜色检测、轮廓检测、颜色追踪
准备工作 python配置numpy和openCv库 读取图像和视频图像cv2.imread(路径) cv2.imshow(窗口名称,输出对象) cv2.waitkey(等待时间)import cv2 ...
- python颜色识别_颜色检测python
广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 版权声明:本文为博主原创文章,未经博主允许不得转载. https:blog.cs ...
- python人头识别_python+opencv实现人头检测
python+opencv实现人头检测 python+opencv实现人头检测 前段时间琢磨了下人头检测这个功能,现在有了初步的认知和体会,下面开始讲下我在实现人头检测过程中遇到的坑和解决方法. 环境 ...
- OpenCV乒乓球位置检测【python】【颜色追踪】【霍夫圆】
乒乓球位置检测 2021年3月14日 参考资料: https://www.bilibili.com/video/BV16K411W7x9 https://blog.csdn.net/weixin_44 ...
- python红色的颜色表达式_50行Python代码实现视频中物体颜色识别和跟踪(必须以红色为例)...
目前计算机视觉(CV)与自然语言处理(NLP)及语音识别并列为人工智能三大热点方向,而计算机视觉中的对象检测(objectdetection)应用非常广泛,比如自动驾驶.视频监控.工业质检.医疗诊断等 ...
- 基于物体颜色的目标检测与跟踪
目录 一.项目内容 二.项目分析 三.主要使用模块 四.选用HSV颜色空间 五.代码实现与分析 1. 颜色选择器: 2. 实现检测并跟踪绘制移动轨迹: 六.测试结果 一.项目内容 (1)利用摄像头,根 ...
- 《OpenCv视觉之眼》Python图像处理十六:Opencv图像处理实战一之图像中的硬币检测
本专栏主要介绍如果通过OpenCv-Python进行图像处理,通过原理理解OpenCv-Python的函数处理原型,在具体情况中,针对不同的图像进行不同等级的.不同方法的处理,以达到对图像进行去噪.锐 ...
- 《OpenCv视觉之眼》Python图像处理十九:Opencv图像处理实战四之通过OpenCV进行人脸口罩模型训练并进行口罩检测
本专栏主要介绍如果通过OpenCv-Python进行图像处理,通过原理理解OpenCv-Python的函数处理原型,在具体情况中,针对不同的图像进行不同等级的.不同方法的处理,以达到对图像进行去噪.锐 ...
- python实现颜色检测、轮廓检测、颜色追踪
python-Opencv学习 准备工作 读取图像和视频 基础功能 裁剪图像 绘制图形和文本 视角转换 图像拼接 颜色检测 轮廓检测 颜色追踪 准备工作 python配置numpy和openCv库 读 ...
- 使用Python,OpenCV和Scikit-Image检测低对比度图像
使用Python,OpenCV和Scikit-Image检测低对比度图像 1. 效果图 2. 原理 3. 源码 参考 这篇博客将介绍如何使用Python,OpenCV和Scikit-Image检测低对 ...
最新文章
- 怎样在Python中执行cmd
- VNC over reverse SSH
- Altium designer中元器件库(SCHLIB)元件引脚上文字(标号)大小及距离边缘位置设置
- 编程语言安全性排行榜:Ruby最佳,C语言漏洞最多?
- oracle导入的表在哪,oracle导入导出表及表结构
- 常用化学消毒剂使用方法
- 证据理论(2)—— 多种合成公式
- 百度黑洞效果实现原理
- 打开word后发现页面在左侧且只有72%。点击单页虽居中但大小也没法变动,调节右下角的大小变化但页面依然在左侧。
- 怎么用计算机打游戏视频,如何录制电脑上正在玩的游戏视频
- java动漫项目_狂拽酷炫diao炸天的开源动画项目:lottie-android,拿来就用!
- 大数据管理平台助力精准扶贫 “建始经验”将在全省推介
- Ubuntu-安装汉语拼音输入法
- 天玑9200实测成绩:CPU单核成绩突破1400分,堪称安卓芯皇
- 动态规划多段图和货郎担问题
- 【仅供娱乐】计算机算命之Python通天六十四卦
- 微信小程序获取获取群id和群名称
- android studio复选按钮样式_Android 自定义CheckBoxPreference的CheckBox复选框
- SpringBoot+Vue实现第三方微博登录(一)
- Java生成随机数的方式