当前位置:网站首页>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 TCC
- Redis 的 最新windows 版本 5.0.14
- [microservices openfeign] openfeign quick start service invocation based on feign
- ?位置怎么写才能输出true
- native关键字的作用
- 摄像头基础知识
- Severe tire damage: the first rock band in the world to broadcast live on the Internet
- wordpress zibll子比主题6.4.1开心版 免授权
- Notepad++ -- common plug-ins
- [NOIP2002 普及组] 过河卒
猜你喜欢

Lumiprobe细胞成像分析:PKH26 细胞膜标记试剂盒

Project practice! Teach you JMeter performance test hand in hand

并发之wait/notify说明

恭喜我自己,公众号粉丝破万

PCR/qPCR研究:Lumiprobe丨dsGreen 用于实时 PCR

Blocking, non blocking, IO multiplexing select\poll\epoll

2022高处安装、维护、拆除考试题及答案

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

quartus 复制IP核

UI automation test framework construction - write an app automation
随机推荐
Play with double pointer
Feign implements path escape through custom annotations
Unity out ref params
无线传感器网络学习笔记(一)
[CSP-J2020] 优秀的拆分
交流电和直流电的区别是什么?
Where does the storm go? Whose pot is the weather forecast wrong?
DH parameters of robotics and derivation using MATLAB symbolic operation
CI & CD must be known!
如何学习可编程逻辑控制器(PLC)?
Interview: what are the similarities and differences between abstract classes and interfaces?
短视频本地生活版块成为热门,如何把握新的风口机遇?
深度强化学习笔记
高通平台 Camera 之 MCLK 配置
2022年安全员-B证考试题库及答案
lotus v1.16.0 calibnet
Lumiprobe细胞成像分析:PKH26 细胞膜标记试剂盒
乔布斯在斯坦福大学的演讲稿——Follow your heart
别卷!如何高质量地复现一篇论文?
玩转双指针