我有这个表示矩形近似值的二进制图像(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()
Run Code Online (Sandbox Code Playgroud)
但我得到的结果如下,这不是我需要的:
为此,我使用 Python 和 opencv。