cv2.error : OpenCV(4.5.3) Error: bad argument & overload resolution failed in cv.line

cv2.error : OpenCV(4.5.3) Error: bad argument & overload resolution failed in cv.line

我有一个带有摄像头的 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

问题是: 追溯(最近调用最后):文件“/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 的序列项类型错误

分配给 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)

你得到的第一个

Can't parse 'pt1'. Sequence item with index 1 has a wrong type

第二个你得到

Can't parse 'pt2'. Sequence item with index 1 has a wrong type

要解决这个问题,我可以更改

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)

看来它必须在我的代码中插入 '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 回答了我的问题,你的解释对我很有帮助。