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 this
But in some cases it returns this image depending on the input image
Sorry for the lack of information.This is the scanned photo (happiest case)
And this is the input photo (the image from camera taked)How can I detect misaligned images and rotate them to the correct position as I want them to be?