【问题标题】:Get the real bounding box of a rectangle shaped mask获取矩形遮罩的真实边界框
【发布时间】:2020-08-07 18:05:58
【问题描述】:

我有这个表示矩形近似值的二进制图像(numpy 数组):

我正在尝试提取矩形的真实形状,但似乎找不到方法。 预期结果如下:

我正在使用此代码

contours,_ = cv2.findContours(numpymask.copy(), 1, 1) # not copying here will throw an error
rect = cv2.minAreaRect(contours[0]) # basically you can feed this rect into your classifier
(x,y),(w,h), a = rect # a - angle

box = cv2.boxPoints(rect)
box = np.int0(box) #turn into ints
rect2 = cv2.drawContours(img.copy(),[box],0,(0,0,255),10)

plt.imshow(rect2)
plt.show()

但我得到的结果如下,这不是我需要的:

为此,我使用 Python 和 opencv。

【问题讨论】:

  • 这不是一个边界“框”。尝试使用convexHull,然后用approxPolyDP逼近轮廓(尝试不同的逼近值,直到得到4个角)
  • 可能使用 Radon 变换来查找 4 线轮廓...绿线对应于参数 Rho-theta 空间中的最小值。

标签: python opencv image-processing bounding-box


【解决方案1】:

这是我以前玩过的东西。它应该适用于您的图像。

import imutils
import cv2
# load the image, convert it to grayscale, and blur it slightly
image = cv2.imread("test.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
# threshold the image,
thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)[1]
# find contours in thresholded image, then grab the largest
# one
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
# draw the contours of c
cv2.drawContours(image, [c], -1, (0, 0, 255), 2)

# show the output image
cv2.imshow("Image", image)
cv2.waitKey(0)

【讨论】:

    猜你喜欢
    • 2012-12-30
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多