1 year ago

#72023

test-img

Wiseman

Python CV2: minAreaRect gives different results on the same image when rotated

today I stumbled upon a problem that gave me some headaches and I couldn't figure out a solution.

I want to find the borders of an image with opencv-python / cv2. This is my code:

(T, thresh) = cv2.threshold(blurredImage, brightnessMin, 255, cv2.THRESH_BINARY_INV)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
try: hierarchy = hierarchy[0]
except: hierarchy = []
thresh = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
for contour, hier in zip(contours, hierarchy):
    (x,y,w,h) = cv2.boundingRect(contour)
    rect      = cv2.minAreaRect(contour)
    box       = cv2.boxPoints(rect)
    box       = np.int0(box)

So far nothing special.

I want to get the same result for rotated images. The borders should be the same either if the image is rotated 90 degrees or 180 degrees.

But when I rotate my image I get completely different rectangles. Here are two images for comparison:

Normal Orientation

180 degree rotation

The first image is the standard orientation.

And the second one is 180 degrees rotated (and rotated backwards for comparison).

I didn't filtered the rectangles.

I simply don't know why the results are so different. Can somebody maybe help me?

I use opencv-python 4.1.2.30. I know it's a way older version but newer versions made my whole script unusable because some things have changend and I didn't know what exactly was wrong.

My Python Version: Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32

Thanks everyone :)

python

opencv

rotation

contour

0 Answers

Your Answer

Accepted video resources