当前位置:网站首页>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
边栏推荐
- 氨基染料研究:Lumiprobe FAM 胺,6-异构体
- Blocking, non blocking, IO multiplexing select\poll\epoll
- openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
- native关键字的作用
- 二级造价工程师证书含金量到底有多高?看这些就知道了
- 并发之wait/notify说明
- Where does the storm go? Whose pot is the weather forecast wrong?
- PMP考试成绩多久出来?这些你务必知道!
- 吴恩达深度学习测验题:deeplearning.ai-week1-quiz
- mysql修改密码报错需要怎么做
猜你喜欢
随机推荐
Audio and video technology development weekly
Necessary skills for test and development: actual combat of security test vulnerability shooting range
lotus v1.16.0 calibnet
Operation of simulated examination platform of G3 boiler water treatment recurrent training question bank in 2022
深度强化学习笔记
!‘ Cat 'is not an internal or external command, nor is it a runnable program or batch file.
通过例子学习Rust
使用class toplevel的messagebox时,窗口弹出问题。
Unity delegate
创新之源 理解通透 二
Principle and implementation of SSD for target detection
几百行代码实现一个脚本解释器
Learning Tai Chi Maker - mqtt Chapter II (VI) mqtt wills
[csp-j2020] excellent splitting
程序员-放羊娃
Differences between pragma and ifndef
羧酸研究:Lumiprobe 磺基花青7二羧酸
cgo+gSoap+onvif学习总结:8、arm平台交叉编译运行及常见问题总结
分享一个因子挖掘的利器:遗传规划
Redis 的 最新windows 版本 5.0.14