【问题标题】:cv2.error : OpenCV(4.5.3) Error: bad argument & overload resolution failed in cv.linecv2.error : OpenCV(4.5.3) 错误:错误参数和重载解析在 cv.line 中失败
【发布时间】:2021-10-22 17:50:41
【问题描述】:

我有一个带有摄像头的 Raspi 4 的简单项目,该项目与汽车的倒车摄像头相似,但没有传感器。这是我的代码:

import time
import cv2
import numpy as np
from picamera.array import PiRGBArray
from picamera import PiCamera

camera = PiCamera()
camera.resolution = (1080, 720) # camera resolution 
camera.framerate = 25
rawCapture = PiRGBArray(camera, size=(1080,720))
kernel = np.ones((2,2),np.uint8)
time.sleep(0.1)
for still in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    
    image = still.array
    #create a detection area
    widthAlert = np.size(image, 1) #get width of image
    heightAlert = np.size(image, 0) #get height of image
    yAlert = (heightAlert/2) + 100 #determine y coordinates for area
    cv2.line(image, (0,yAlert), (widthAlert,yAlert),(0,0,255),2) #draw a line to show area
    
    lower = [1, 0, 20]
    upper = [60, 40, 200]
    lower = np.array(lower, dtype="uint8")
    upper = np.array(upper, dtype="uint8")
    #use the color range to create a mask for the image and apply it to the image
    mask = cv2.inRange(image, lower, upper)
    output = cv2.bitwise_and(image, image, mask=mask)
    
    dilation = cv2.dilate(mask, kernel, iterations = 3)
    closing = cv2.morphologyEx(dilation, cv2.MORPH_GRADIENT, kernel)
    closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
    edge = cv2.Canny(closing, 175, 175)
    
    contours, hierarchy = cv2.findContours(closing, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    
    threshold_area = 400
    centres = []
    
    if len(contours) !=0:
        
        for x in contours:
            #find the area of each contour
            area = cv2.contourArea(x)
            #find the center of each contour
            moments = cv2.moments(x)
            #weed out the contours that are less than our threshold
            if area > threshold_area:
                
                (x,y,w,h) = cv2.boundingRect(x)
                
                centerX = (x+x+w)/2
                centerY = (y+y+h)/2
                
                cv2.circle(image,(centerX, centerY), 7, (255, 255, 255), -1)
                
                if ((y+h) > yAlert):
                    cv2.putText(image, "ALERT!", (centerX -20, centerY -20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255),2)
    
    cv2.imshow("Display", image)
    
    rawCapture.truncate(0)
    
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

我得到的错误是:

 image = still.array
    #create a detection area
    widthAlert = np.size(image, 1) #get width of image
    heightAlert = np.size(image, 0) #get height of image
    yAlert = (heightAlert/2) + 100 #determine y coordinates for area
    cv2.line(image, (0,yAlert), (widthAlert,yAlert),(0,0,255),2) #draw a line to show area

问题是: Traceback(最近一次调用最后一次):文件“/home/pi/Desktop/object_detector.py”,第 20 行,在

cv2.line(image, (0,yAlert), (widthAlert,yAlert),(0,0,255),2) #画一条线显示区域

cv2.error: OpenCV(4.5.3) 错误: (-5:Bad argument) in function 'line'

重载解析失败:

-无法解析“pt1”。索引为 1 的序列项类型错误

-无法解析“pt1”。索引为 1 的序列项类型错误

【问题讨论】:

标签: python-3.x numpy opencv


【解决方案1】:

分配给 pt1 和 pt2 的值不应该是浮点数。

所以这工作正常。

import cv2
import numpy as np

h,w=100,100
im = ~np.zeros((h,w,3), np.uint8)

cv2.line(im, (0,10), (100,100),(0,0,255),2)
cv2.imshow('line',im)
cv2.waitKey(0)

现在如果你改变这一行

cv2.line(im, (0,10), (100,100),(0,0,255),2)

到这里

cv2.line(im, (0,10.1), (100,100),(0,0,255),2)
#OR
cv2.line(im, (0,10), (100,100.1),(0,0,255),2)

第一个得到的

无法解析“pt1”。索引为 1 的序列项类型错误

第二个你会得到

无法解析“pt2”。索引为 1 的序列项类型错误

为了解决这个问题,我可以改变

cv2.line(im, (0,10.1), (100,100),(0,0,255),2)

cv2.line(im, (0,int(10.1)), (100,100),(0,0,255),2)

【讨论】:

  • 哦,我从你的解释中明白了。我会试试的:)
【解决方案2】:

它似乎必须在我的代码中插入'int':

 widthAlert = np.size(image, 1) #get width of image
heightAlert = np.size(image, 0) #get height of image
yAlert = (heightAlert/2) + 100 #determine y coordinates for area
cv2.line(image, (0,yAlert), (widthAlert,yAlert),(0,0,255),2) #draw a line to show area

到:

 widthAlert = np.size(image, 1) #get width of image
heightAlert = np.size(image, 0) #get height of image
yAlert = (heightAlert/2) + 100 #determine y coordinates for area
cv2.line(image, (0,int(yAlert), (widthAlert,yAlert),(0,0,255),2) #draw a line to show area

但后来我在 cv2.circle 中遇到了同样的问题,并且从我自己的问题 xD 中得到了同样的解决方案。 Tq @Shamshirsaz.Navid 回答了我的问题,你的解释对我很有帮助。

【讨论】:

    猜你喜欢
    • 2021-09-09
    • 2022-10-17
    • 2018-05-20
    • 1970-01-01
    • 2018-08-29
    • 2021-09-27
    • 2021-08-08
    • 1970-01-01
    • 2021-09-26
    相关资源
    最近更新 更多