Some blob pixels outside the min area rectangle

Hello,
Don’t know if it is running as it should. I have blob pixels out of the min area rectangle. For me all pixels should be enclosed in the rectangle. And I’m don’t really understand the meaning of

Unlike blob.corners() the min area rectangle corners do not necessarily lie on the blob.

in the blob.min_corners() function description.
Image with a rectangle defined by min_corners():
MinAreaRectangle.png
Code:

import image, time

imgs = image.Image("/stream-c1.pgm",copy_to_fb=True)

imgs.binary([(30,255)])
imgs.open(4)
blob = imgs.find_blobs([(128,255)], merge=True)[0]

imgs.flush()
time.sleep_ms(1000)
imgs.draw_edges(blob.min_corners(), color=(128,128,128))
imgs.flush()
time.sleep_ms(1000)

Test image in joined stream-c1.zip

I don’t have that behavior with OpenCV with the following code:

import numpy as np
import cv2

img = cv2.imread('stream-c1.pgm',0)

ret, img_thresh = cv2.threshold(img,30,255,cv2.THRESH_BINARY)

kernelsize = 9
kernel = np.ones((kernelsize,kernelsize),np.uint8)
img_open = cv2.morphologyEx(img_thresh, cv2.MORPH_OPEN, kernel)

cnts,hierarchy = cv2.findContours(img_open, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnt = cnts[0]
rect = cv2.minAreaRect(cnt)
print(rect)
box = np.int0(cv2.boxPoints(rect))

img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
img_open = cv2.cvtColor(img_open, cv2.COLOR_GRAY2BGR)
img = cv2.drawContours(img, [box], 0, (128,255,128), 1)
img_open = cv2.drawContours(img_open, [box], 0, (128,255,128), 1)
img = cv2.drawContours(img, cnts, -1, (255,128,128), 1)

cv2.imshow('img',img)
cv2.imshow('img_bin',img_open)
cv2.waitKey(0)
cv2.destroyAllWindows()

stream-c1.zip (13.3 KB)

Hi, the way out minRect code works is not as complete as OpenCV’s one. We just collect a list of 20 points on the blob as we are tracking it. So, the minRect doesn’t reflect all the points. In particular, out method doesn’t work well on shapes that don’t have great features.

We may upgrade our support to be as good as OpenCV’s in the future. This requires storing the complete edge contour however.