Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 17447

How to detect rectangles and rotate them in the correct direction in image

$
0
0

I have block code like this.

def crop_image(self,img):        cnts,gray_img = self.pre_processing_img(img)        ans_blocks = []        x_old, y_old, w_old, h_old = 0, 0, 0, 0        if len(cnts) > 0:            cnts = sorted(cnts, key=self.get_x_ver1)            for i, c in enumerate(cnts):                x_curr, y_curr, w_curr, h_curr = cv2.boundingRect(c)                if w_curr * h_curr > 100000 and w_curr < h_curr:                    check_xy_min = x_curr * y_curr - x_old * y_old                    check_xy_max = (x_curr + w_curr) * (y_curr + h_curr) - (x_old + w_old) * (y_old + h_old)                    if len(ans_blocks) == 0:                        cv2.imshow("test", gray_img[y_curr:y_curr + h_curr, x_curr:x_curr + w_curr])                        cv2.waitKey(0)                        cv2.destroyAllWindows()                        ans_blocks.append(                            (gray_img[y_curr:y_curr + h_curr, x_curr:x_curr + w_curr],[x_curr,y_curr,w_curr,h_curr]))                        x_old,y_old,w_old,h_old = x_curr,y_curr,w_curr,h_curr                    elif check_xy_min > 20000 and check_xy_max > 20000:                        ans_blocks.append(                            (gray_img[y_curr:y_curr + h_curr, x_curr:x_curr + w_curr],[x_curr,y_curr,w_curr,h_curr]))                        x_old,y_old,w_old,h_old = x_curr,y_curr,w_curr,h_curr            sorted_ans_blocks = sorted(ans_blocks, key=self.get_x)            return sorted_ans_blocks

And the correct image test should be like thisenter image description here

But in some cases it returns this image depending on the input imageenter image description here

Sorry for the lack of information.This is the scanned photo (happiest case)enter image description here

And this is the input photo (the image from camera taked)enter image description hereHow can I detect misaligned images and rotate them to the correct position as I want them to be?


Viewing all articles
Browse latest Browse all 17447

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>