【发布时间】:2017-07-09 17:04:00
【问题描述】:
我最近开始探索 OpenCV,对此我非常了解。我无法在原始视频帧中显示缩放的视频帧。希望这是有道理的。一切正常,但是当我尝试更改缩放视频的颜色时,出现错误。这是我的代码,希望它可以自我解释。
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while True:
#"ret" returns a frame
ret, frame = cap.read()
#Draw a rectangle at given location
cv2.rectangle(frame,(500,80),(800,380),(0,255,0),5)
#takes a sample of the frame marked by the rectangle area(y,x)
face_track = frame[80:380, 500:800]
#converts the sample to gray
grayscaled = cv2.cvtColor(face_track,cv2.COLOR_BGR2GRAY)
#threshold range of sample
retvl, threshold = cv2.threshold(grayscaled,125,125,cv2.THRESH_BINARY)
#overwrites/display a new area with the sample taken by face_track in the left top corner of the frame
frame[0:300, 0:300] = threshold
#displays the frames captured by cap
cv2.imshow('frame',frame)
#cv2.imshow('frame',threshold)
#if key stroke is 'q' break and terminate
if cv2.waitKey(0) & 0xff == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
以下是错误:
帧[0:300, 0:300] = 阈值 ValueError: 无法将输入数组从形状 (300,300) 广播到形状 (300,300,3)
如果我把它改成这样:
帧[0:300, 0:300] = face_track
它有效。但这不是我想要的。
另外,如果我改为输出阈值,
cv2.imshow('frame',threshold),
它也可以工作。但又不是我想要的。
除了cv2.cvtColor之外还有什么其他函数不会改变数组的形状。
【问题讨论】:
标签: python arrays opencv video-capture