boxPoints返回负y值

时间:2017-11-13 19:44:18

标签: python python-3.x opencv

我正在尝试在简单的图像上绘制最小的框轮廓。

Original Image

但是,从boxPoints()函数返回的一个点给出了负y值。绘制此轮廓时,此角落将从图像中拉出。

Resulting Outline

我不确定为什么它会返回负y值。我使用的是图像的二进制版本,因此最大的轮廓应该是postIt音符的轮廓。其中一个角也没有正确拾取。不太确定如何解决这个问题。请参阅以下代码:

def contours(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    gray = cv2.dilate(gray, None)  # fill some holes
    gray = cv2.dilate(gray, None)
    gray = cv2.erode(gray, None)  # dilate made our shape larger, revert that
    gray = cv2.erode(gray, None)

    # first threshold the image to create a binary image of black and white
    ret, thresh = cv2.threshold(gray, 127, 255, 0)

    # contours output var is a list of each coordinate in the contour
    # use CHAIN_APPROX_SIMPLE to pick only necessary contour points rather than all points on the contour
    img,cnt,hier = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    # get the min area rect
    rect = cv2.minAreaRect(cnt[0])
    print(rect)
    box = cv2.boxPoints(rect)
    print(box)
    # convert all coordinates floating point values to int
    box = np.int32(box)
    cv2.drawContours(image, [box], 0, (0, 0, 255))
    cv2.imshow('Corners1', image)
    cv2.waitKey(0)

打印的minAreaRect和boxPoints如下:

((182.83834838867188, 139.0049591064453), (208.65762329101562, 247.1478271484375), -31.165145874023438)
[[ 157.5166626   298.73544312]
 [  29.61603546   87.25617981]
 [ 208.16003418  -20.7255249 ]
 [ 336.06066895  190.7537384 ]]

1 个答案:

答案 0 :(得分:2)

看起来您正在使用此库中的框函数。您可以在生成的图像上看到所有角度都是90度,因此您需要做的是使多边形不是矩形。负y值是这个直角框的结果。