https://mydreamambitious.blog.csdn.net/article/details/125339425
https://mydreamambitious.blog.csdn.net/article/details/125351256
https://mydreamambitious.blog.csdn.net/article/details/125428402
cap=cv2.VideoCapture(0)
#设置显示的高宽
cap.set(cv2.CAP_PROP_FRAME_WIDTH,1100)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,750)
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)
#获取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"
#绘制检测区域
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)
#获取检测位置的物体颜色
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)
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')