【问题标题】:Opencv-Python input array errorOpencv-Python输入数组错误
【发布时间】: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


    【解决方案1】:

    将阈值灰度转换为 BGR

    cv2.cvtColor(阈值,cv2.COLOR_GRAY2BGR)

    【讨论】:

    • 很好,成功了!谢谢。你介意解释一下发生了什么吗? @bluekid
    • 彩色图像有3通道,灰度1
    【解决方案2】:

    错误是您正在对三通道矩阵(8UC3)进行单通道矩阵(8UC1)操作。

    要解决这个问题,您要么需要将单通道矩阵(灰度)转换为三通道(BGR),要么对单通道矩阵进行单通道矩阵运算。在 python openCV 中执行此操作:

    cv2.cvtColor(threshold,cv2.COLOR_GRAY2BGR)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 1970-01-01
      • 1970-01-01
      • 2016-09-30
      • 2017-05-11
      • 1970-01-01
      • 2023-03-10
      相关资源
      最近更新 更多