1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| images=[image] titles=['init'] for angle1 in list(range(0, 360, 45)): rotated = imutils.rotate(image.copy(), angle1) rotated=cv2.cvtColor(rotated,cv2.COLOR_RGB2GRAY) try: _, contours, hierarchy = cv2.findContours(rotated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) except ValueError: contours, hierarchy = cv2.findContours(rotated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) rotatedRect = cv2.minAreaRect(contours[0])
angle = rotatedRect[-1] w, h = rotatedRect[1] x,y = rotatedRect[0] cv2.putText(rotated, 'angle:' + str(int(angle)), (100, 25), cv2.FONT_HERSHEY_SIMPLEX, 1, 255) cv2.putText(rotated, '(h,w):({0},{1})'.format(int(h), int(w)), (80, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, 255) images.append(rotated) titles.append(str(angle1))
show_images(images,titles=titles)
|