我最近开始探索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之外,是否还有其他函数不更改数组形状。
发布于 2017-07-09 18:04:38
将阈值灰度转换为BGR
Cv2.cvtColor(阈值,cv2.COLOR_GRAY2BGR)
发布于 2018-04-22 08:55:50
错误是在三通道矩阵(8UC3)上执行单通道矩阵(8UC1)操作。
要解决这个问题,要么需要将单通道矩阵(灰度)转换为三通道,要么对单个信道矩阵进行单通道矩阵操作。要在python openCV中执行此操作,请执行以下操作:
cv2.cvtColor(threshold,cv2.COLOR_GRAY2BGR)https://stackoverflow.com/questions/44998920
复制相似问题