'Find rotation angle of binary image in opencv python

I have a set of images containing top views of solar panels as seen bellow :enter image description here

In order for me to get the rotation angle of the solar panels,I trained a model to segment the images as seen bellow: enter image description here

In order to extract the angle of rotations I used cv2.findContours then fitline, fitellipse, minareareact but the angle various were not consistent

 area = cv2.contourArea(contours[i])
        if area>max_area:
            cnt = contours[i]
            max_area = area
            
    rect = cv2.minAreaRect(cnt)
    x, y = [], []
    ((centx0,centy0), (widthe0,heighte0), angle_200)= cv2.fitEllipse(cnt)

I also used PCAcompute butthe angles zere notconsistent either

Now, I am following another approach in which I apply canny edge detection to extract the edges and then extract the angles: enter image description here

Or this one

enter image description here

But even here using the different fitting techniques did not turn out to be robust.

Is there a good method to extract angles of rotations or tilt of the panels from this images?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source