0

I got the contours of source image. I have drawn 4 lines to approximate these contours:

  1. from minimum width to minimum height of contour.
  2. from minimum width to maximum height of contour.
  3. from maximum width to minimum height of contour.
  4. from maximum width to maximum height of contour.

I'd like to rotate this rectangle such that it is aligned to width (i.e. x-coordinate of image).

GregC
  • 7,517
  • 2
  • 48
  • 63
user1367465
  • 1
  • 1
  • 1
  • 1
    possible duplicate of [Executing cv::warpPerspective for a fake deskewing on a set of cv::Point](http://stackoverflow.com/questions/7838487/executing-cvwarpperspective-for-a-fake-deskewing-on-a-set-of-cvpoint) – karlphillip May 01 '12 at 17:11
  • Have you tried to search on cvWarpAffine? – Mzk May 01 '12 at 12:13

1 Answers1

0

This may help you

rect = cv2.minAreaRect(yourcontour)
angle = rect[2]

if angle < -45:
    angle = (90 + angle)

# otherwise, just take the inverse of the angle to make
# it positive
else:
    angle = -angle  

# rotate the image to deskew it
(h, w) = img.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(img, M, (w, h),
    flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) 
M.Ali El-Sayed
  • 1,213
  • 16
  • 20