1、使用python调用摄像头,显示摄像头中的画面
2、在视频上加一个矩形框
参考:
https://blog.csdn.net/u010554381/article/details/81018898(python+opencv选出视频中一帧再利用鼠标回调实现图像上画矩形框)
https://www.cnblogs.com/subic/p/6937611.html
https://blog.csdn.net/yang123p/article/details/79831697
https://blog.csdn.net/szfhy/article/details/51087301 (python+opencv鼠标操作以及制作调色板并画图)
https://blog.csdn.net/guyuealian/article/details/88013421 (Python OpenCV实现鼠标画框)
https://blog.csdn.net/dcrmg/article/details/52541174 (Opencv在视频中静态、动态方式绘制矩形框ROI)
def video_demo():
# 0是代表摄像头编号,只有一个的话默认为0
capture = cv.VideoCapture(0)
while (True):
ref, frame = capture.read()
cv.imshow("1", frame)
# 等待30ms显示图像,若过程中按“Esc”退出
c = cv.waitKey(30) & 0xff
if c == 27:
capture.release()
break
video_demo()
cv.waitKey()
cv.destroyAllWindows()
2、我结合自己的电脑做了一些啊改动:
cap = cv2.VideoCapture(-1)我的电脑打开摄像头参数是——0、
cap = cv2.VideoCapture(-1)改成0!
import cv2
drawing = False
ix, iy = -1, -1
tempFlag = False
def draw_circle(event, x, y, flags, param):
global ix, iy, drawing, mode, cap, template, tempFlag
if event == cv2.EVENT_LBUTTONDOWN:
tempFlag = True
drawing = True
ix, iy = x, y #按下鼠标左键,用全局变量ix,iy记录下当前坐标点
elif event == cv2.EVENT_LBUTTONUP:
if drawing == True:
drawing = False #鼠标左键抬起,画出矩形框
cv2.rectangle(frame, (ix, iy), (x, y), (0, 255, 0), 1)
template = frame[iy:y, ix:x, :] #截取框中的目标图像
cap = cv2.VideoCapture(0) #打开摄像头
cv2.imshow('img', frame) #显示画框后的图像
cv2.namedWindow('img')
cv2.setMouseCallback('img', draw_circle)
cap = cv2.VideoCapture(0)
while (True):
ret, frame = cap.read()
cv2.imshow('Vedio', frame)
if tempFlag == True: #如果框出了目标,显示该目标
cv2.imshow('temp', template)
k = cv2.waitKey(33)
if k == 27: #退出视频
break
elif k == 32: #如果按下空格键
while(1):
cap.release() #关掉摄像头
imgCOPY = frame #显示关闭摄像头前最后一张图像
cv2.imshow('img', frame)
k = cv2.waitKey(0) #等待调用鼠标回调函数框出目标
if k == 32: #框完目标,再次按下空格键,摄像头捕捉的画面重新播放
break