cv2.error : OpenCV(4.5.3) 错误: 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

问题是:回溯(最近一次调用):File "/home/pi/Desktop/object_detector.py",第20行,

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

cv2。错误:OpenCV(4.5.3)错误:(-5:错误参数)在函数'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)

第一个是

无法解析'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)

似乎必须在我的代码中插入'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中遇到了同样的问题。圆和我自己的问题xD的相同解。Tq @Shamshirsaz。谢谢你回答我的问题,你的解释对我很有帮助。

相关内容

最新更新