gpt4 book ai didi

python - cv2.minAreaRect 出错,给我 Error related to convex hull function which I haven't use

转载 作者:太空宇宙 更新时间:2023-11-03 22:46:21 25 4
gpt4 key购买 nike

在下面的代码中,我试图找出一个对象是否为绿色并在其周围绘制轮廓。有了这些信息,我还想找到形状的角并对其进行裁剪,但是我收到了与代码中未使用的 ConvexHull 函数相关的错误。我用过 python 3 和 Opencv 3.2.0

我写的代码给了我这个错误

Traceback (most recent call last):
File "/home/pi/Desktop/SelfDrivingCar/code/picamsense2.py", line 29, in <module>
points = cv2.minAreaRect(contour)
error: /home/pi/opencv/opencv-3.2.0/modules/imgproc/src/convhull.cpp:136: error: (-215) total >= 0 && (depth == CV_32F || depth == CV_32S) in function convexHull

这是我的代码

from picamera import PiCamera
import time
import cv2
import numpy as np 
camera = PiCamera()
camera.start_preview()
time.sleep(5)
camera.capture('/home/pi/Desktop/piImage/image.jpg')
camera.stop_preview()
img = cv2.imread('/home/pi/Desktop/piImage/image.jpg', 1)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower = np.array([46, 100, 100])
upper = np.array([86, 255, 255])
mask = cv2.inRange (hsv, lower, upper)
blur = cv2.GaussianBlur(mask, (7,5),0 )
erosion =cv2.erode(blur, (5,5), iterations = 3)
contour =np.array( cv2.findContours(erosion, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[1])
cv2.drawContours(img, contour, -1, (0,255,0), 3)
for cnt in contour:
    approx = cv2.approxPolyDP(cnt, 0.04*cv2.arcLength(cnt, True), True)
    if len (approx)== 3:
        print "triangle"
    elif len (approx) == 4 :
        print "quadrilateral"
    elif len(approx) > 4:
        print "circle"        
points = cv2.minAreaRect(contour)
points = cv2.boxPoints(points)
points = np.int0(points)
for p in points :
    pt = (p[0],p[1])
    print pt
print points
cv2.imshow('eroded', blur)
cv2.imshow('original', img)
while (1):
    k = cv2.waitKey(0)
    if(k == 27):
     break
cv2.destroyAllWindows()

一段时间以来,我一直在努力寻找解决办法

任何帮助将不胜感激

提前致谢

-山姆

最佳答案

张贴在这里以防其他人通过看似有效的输入得到这个错误:

在我的例子中,问题是由将切片的 numpy 数组传递给 cv2.minAreaRect 引起的功能(使用 vertex.shape = (4, 3) )

# This won't work and will raise the following error:
# error: /io/opencv/modules/imgproc/src/convhull.cpp:137: error: (-215) total >= 0 && (depth == 5 || depth == 4) in function convexHull

cv2.minAreaRect(vertices[:, :2])

我必须传递数组的副本才能使其工作:

# This works fine!
cv2.minAreaRect(vertices[:, :2].copy())

这是一个奇怪的问题,我不确定为什么第二个问题有效。

关于python - cv2.minAreaRect 出错,给我 Error related to convex hull function which I haven't use,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46755932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com