当前位置:网站首页>Opencv实现颜色检测
Opencv实现颜色检测
2022-06-28 04:57:00 【Keep_Trying_Go】
文章目录
1.前置知识点
(1) HSV,Opencv中的Mat和图片的属性
https://mydreamambitious.blog.csdn.net/article/details/125339425
(2)Opencv中基础的知识点
https://mydreamambitious.blog.csdn.net/article/details/125351256
(3)Opencv实现图像的基本变换
https://mydreamambitious.blog.csdn.net/article/details/125428402
2.代码实战
(1)创建窗口和设置视频帧的大小调整
cap=cv2.VideoCapture(0)
#设置显示的高宽
cap.set(cv2.CAP_PROP_FRAME_WIDTH,1100)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,750)
(2)翻转图像和将图片从BGR->HSV
ret,frame=cap.read()
#对图像帧进行翻转(因为opencv图像和我们正常是反着的
frame=cv2.flip(src=frame,flipCode=2)
# frame=cv2.resize(src=frame,dsize=(750,600))
hsv_frame=cv2.cvtColor(src=frame,code=cv2.COLOR_BGR2HSV)
(3)获取hsv的色相值hue
#获取hsv的色相值hue
pixel_center=hsv_frame[cy,cx]
hue_value=pixel_center[0]
(4)根据色相值对颜色进行定义
color='Unknow'
if hue_value<5:
color='RED'
elif hue_value<22:
color='ORANGE'
elif hue_value<33:
color="YELLOW"
elif hue_value<78:
color="GREEN"
elif hue_value<131:
color="BLUE"
elif hue_value<167:
color="VIOLED"
else:
color="RED"
(5)绘制检测区域
#绘制检测区域
cv2.rectangle(img=frame,pt1=(width//2+100,20),pt2=(width//2+390,200),color=(0,255,0),thickness=2)
cv2.putText(img=frame,text='detect locate',org=(width//2+150,cy-10),fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=1.0,color=(0,255,0),thickness=2)
(6)绘制检测结果的文本
#获取检测位置的物体颜色
b,g,r=int(frame[cy,cx][0]),int(frame[cy,cx][1]),int(frame[cy,cx][2])
#绘制文本
cv2.putText(img=frame,text=color,org=(20,50),fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=1.0,color=(0,255,0),thickness=2)
cv2.circle(img=frame,center=(cx+30,cy),radius=5,color=(0,255,0),thickness=3)
(7)整体代码
import os
import cv2
import numpy as np
cap=cv2.VideoCapture(0)
#设置显示的高宽
cap.set(cv2.CAP_PROP_FRAME_WIDTH,1100)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,750)
while cap.isOpened():
ret,frame=cap.read()
#对图像帧进行翻转(因为opencv图像和我们正常是反着的
frame=cv2.flip(src=frame,flipCode=2)
# frame=cv2.resize(src=frame,dsize=(750,600))
hsv_frame=cv2.cvtColor(src=frame,code=cv2.COLOR_BGR2HSV)
#获取读取的帧的高宽
height,width,channel=frame.shape
# print(height,width)
#获取检测点位置
cx = width // 2+190
cy=height//6+30
#绘制检测区域
cv2.rectangle(img=frame,pt1=(width//2+100,20),pt2=(width//2+390,200),color=(0,255,0),thickness=2)
cv2.putText(img=frame,text='detect locate',org=(width//2+150,cy-10),fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=1.0,color=(0,255,0),thickness=2)
#获取hsv的色相值hue
pixel_center=hsv_frame[cy,cx]
hue_value=pixel_center[0]
color='Unknow'
if hue_value<5:
color='RED'
elif hue_value<22:
color='ORANGE'
elif hue_value<33:
color="YELLOW"
elif hue_value<78:
color="GREEN"
elif hue_value<131:
color="BLUE"
elif hue_value<167:
color="VIOLED"
else:
color="RED"
#获取检测位置的物体颜色
b,g,r=int(frame[cy,cx][0]),int(frame[cy,cx][1]),int(frame[cy,cx][2])
#绘制文本
cv2.putText(img=frame,text=color,org=(20,50),fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=1.0,color=(0,255,0),thickness=2)
cv2.circle(img=frame,center=(cx+30,cy),radius=5,color=(0,255,0),thickness=3)
cv2.imshow('frame',frame)
key=cv2.waitKey(1)
if key==27:
break
cap.release()
cv2.destroyAllWindows()
# #图片的读取和缩放
# img=cv2.imread('images/1.png')
# img=cv2.resize(src=img,dsize=(450,450))
# #显示图片
# cv2.imshow('img',img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')
边栏推荐
- Simulation questions and answers of the latest national fire-fighting facility operators (primary fire-fighting facility operators) in 2022
- S32ds jump to defaultisr
- Analysis of distributed transaction TCC
- 机器人学DH参数及利用matlab符号运算推导
- 2022西式面点师(高级)考试试题模拟考试平台操作
- metaRTC5.0编程之p2p网络穿透(stun)指南
- Interview: what are the similarities and differences between abstract classes and interfaces?
- 论文详读:IMPROVING CONVOLUTIONAL MODELS FOR HANDWRITTEN TEXT RECOGNITION
- OracleData安装问题
- 2022年安全员-B证考试题库及答案
猜你喜欢

Google Earth Engine(GEE)——全球洪水数据库 v1 (2000-2018年)

Google Earth engine (GEE) - global flood database V1 (2000-2018)

2022年G3锅炉水处理复训题库模拟考试平台操作

2022年安全员-A证考试题库及模拟考试

Generate QR code in wechat applet

?位置怎么写才能输出true

Audio and video technology development weekly

The latest examination questions and answers for the eight members (standard members) of Liaoning architecture in 2022

CPG 固体支持物研究:Lumiprobe通用 CPG II 型
![[applet] solution document using font awesome Font Icon (picture and text)](/img/1b/d1b738e6e35e59cc4a417df4ea0e8d.png)
[applet] solution document using font awesome Font Icon (picture and text)
随机推荐
Feign通过自定义注解实现路径的转义
大促场景下,如何做好网关高可用防护
Taco: a data enhancement technique for character recognition
学习太极创客 — MQTT 第二章(四)ESP8266 保留消息应用
PMP考试成绩多久出来?这些你务必知道!
100+ data science interview questions and answers Summary - machine learning and deep learning
[microservices openfeign] openfeign quick start service invocation based on feign
Analysis of distributed transaction TCC
控制器的功能和工作原理
[csp-j2020] excellent splitting
2022年安全员-B证考试题库及答案
Principle and implementation of SSD for target detection
恭喜我自己,公众号粉丝破万
项目经理考完PMP就够了?不是的!
BioVendor sRAGE Elisa试剂盒化学性质和技术研究
并发之wait/notify说明
公司为什么选择云数据库?它的魅力到底是什么!
羧酸研究:Lumiprobe 磺基花青7二羧酸
Standard particle swarm optimization C language program
2022电力电缆判断题模拟考试平台操作