当前位置:网站首页>Realizing color detection with OpenCV
Realizing color detection with OpenCV
2022-06-28 05:01:00 【Keep_ Trying_ Go】
List of articles
1. Pre knowledge points
(1) HSV,Opencv Medium Mat And picture properties
https://mydreamambitious.blog.csdn.net/article/details/125339425
(2)Opencv Basic knowledge points in
https://mydreamambitious.blog.csdn.net/article/details/125351256
(3)Opencv Realize the basic transformation of the image
https://mydreamambitious.blog.csdn.net/article/details/125428402
2. Code combat
(1) Create windows and set the size of video frames
cap=cv2.VideoCapture(0)
# Set the height and width of the display
cap.set(cv2.CAP_PROP_FRAME_WIDTH,1100)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,750)
(2) Flip the image and move the picture from BGR->HSV
ret,frame=cap.read()
# Flip the image frame ( because opencv The image is opposite to our normal
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) obtain hsv The hue value of hue
# obtain hsv The hue value of hue
pixel_center=hsv_frame[cy,cx]
hue_value=pixel_center[0]
(4) Define the color according to the hue value
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) Draw detection area
# Draw detection area
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) Draw the text of the test results
# Get the object color of the detection position
b,g,r=int(frame[cy,cx][0]),int(frame[cy,cx][1]),int(frame[cy,cx][2])
# Draw text
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) The overall code
import os
import cv2
import numpy as np
cap=cv2.VideoCapture(0)
# Set the height and width of the display
cap.set(cv2.CAP_PROP_FRAME_WIDTH,1100)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,750)
while cap.isOpened():
ret,frame=cap.read()
# Flip the image frame ( because opencv The image is opposite to our normal
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)
# Get the height and width of the read frame
height,width,channel=frame.shape
# print(height,width)
# Get the location of the checkpoint
cx = width // 2+190
cy=height//6+30
# Draw detection area
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)
# obtain hsv The hue value of 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"
# Get the object color of the detection position
b,g,r=int(frame[cy,cx][0]),int(frame[cy,cx][1]),int(frame[cy,cx][2])
# Draw text
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()
# # Picture reading and zooming
# img=cv2.imread('images/1.png')
# img=cv2.resize(src=img,dsize=(450,450))
# # display picture
# cv2.imshow('img',img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')
Reference link https://b23.tv/6ZJL1WY
边栏推荐
- Analysis of distributed transaction solution Seata golang
- Extjs图书管理系统源码 智能化图书管理系统源码
- Unity out ref params
- 灵活的IP网络测试工具——— X-Launch
- CUPTI error: CUPTI could not be loaded or symbol could not be found.
- 2022年低压电工考题及答案
- 乔布斯在斯坦福大学的演讲稿——Follow your heart
- PMP考试成绩多久出来?这些你务必知道!
- 论文详读:IMPROVING CONVOLUTIONAL MODELS FOR HANDWRITTEN TEXT RECOGNITION
- Opencv实现目标检测
猜你喜欢

大促场景下,如何做好网关高可用防护

Simulation questions and answers of the latest national fire-fighting facility operators (primary fire-fighting facility operators) in 2022

On the necessity of building a video surveillance convergence platform and its scenario application

CPG 固体支持物研究:Lumiprobe通用 CPG II 型

cgo+gSoap+onvif学习总结:8、arm平台交叉编译运行及常见问题总结

100+ data science interview questions and answers Summary - machine learning and deep learning

DH parameters of robotics and derivation using MATLAB symbolic operation

Role of native keyword
![[CSP-J2020] 优秀的拆分](/img/05/90f9cf71791b3cdc37073eaf5db989.png)
[CSP-J2020] 优秀的拆分

2022年低压电工考题及答案
随机推荐
Standard particle swarm optimization C language program
禁用右击、键盘打开控制台事件
JS 文本框失去焦点修改全半角文字和符号
Simulation questions and answers of the latest national fire-fighting facility operators (primary fire-fighting facility operators) in 2022
Code understanding: implementing volume models for hangwriten text recognition
Organize the online cake mall project
[NOIP2002 普及组] 过河卒
native关键字的作用
A bit of knowledge - online resources on Chinese Learning
TACo:一种关于文字识别的数据增强技术
并发之wait/notify说明
What to do when MySQL changes the password and reports an error
cgo+gSoap+onvif学习总结:8、arm平台交叉编译运行及常见问题总结
Unity delegate
学习太极创客 — MQTT 第二章(六)MQTT 遗嘱
BioVendor sRAGE Elisa试剂盒化学性质和技术研究
A doctor's 22 years in Huawei (full of dry goods)
Google Earth Engine(GEE)——全球洪水数据库 v1 (2000-2018年)
Notepad++ -- column editing mode -- Usage / instance
Opencv实现目标检测