numpy 错误代码:OpenCV(4.5.3)错误代码:cv.行中的错误参数&重载解析失败

ttvkxqim  于 2022-11-23  发布在  其他
关注(0)|答案(2)|浏览(181)

我有一个简单的项目与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)#绘制一条线以显示区域
错误代码:OpenCV(4.5.3)错误代码:函数'line'中的(-5:错误的参数)
重载解析失败:

  • 无法分析“pt 1”。索引为1的序列项的类型错误
  • 无法分析“pt 1”。索引为1的序列项的类型错误
uidvcgyl

uidvcgyl1#

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

b4wnujal2#

它似乎必须插入'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

但是后来我在www.example.com上遇到了同样的问题cv2.circle,我自己的问题xD.Tq@Shamshirsaz也有同样的解决方案。Navid回答了我的问题,你的解释对我很有帮助。

相关问题