gpt4 book ai didi

python - Opencv-Python输入数组错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:13:18 25 4
gpt4 key购买 nike

我最近开始探索 OpenCV,对此我非常了解。我在显示原始视频帧内的缩放视频帧时遇到问题。希望这是有道理的。一切正常,但是当我尝试更改缩放视频的颜色时,出现错误。这是我的代码,希望它能 self 解释。

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()

错误如下:

frame[0:300, 0:300] = threshold ValueError: could not broadcast input array from shape (300,300) into shape (300,300,3)

如果我把它改成这样:

frame[0:300, 0:300] = face_track

它有效。但这不是我想要的。

此外,如果我改为输出阈值,cv2.imshow('frame',threshold),它也会起作用。但同样不是我想要的。

除了cv2.cvtColor之外,还有其他函数不会改变数组的形状吗?

最佳答案

将 thresh gray 转换为 BGR

cv2.cvtColor(阈值,cv2.COLOR_GRAY2BGR)

关于python - Opencv-Python输入数组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44998920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com