Skip to content Skip to sidebar Skip to footer

How To Convert The C++ Code To Python For Automatic Image Rotation Using OpenCV?

I want to do the following: Rotate the Incoming Image to align it perfectly with the Template Image. Use cv2.substrate() to compare the two aligned images & print out the dif

Solution 1:

I was thinking of coming up with a solution after I got your comment. My answer may not be perfect but hope it gives some idea to a better solution.

Perform a contour operation of the image you intend to find rotation for. Fit an ellipse around the contour you have obtained. Now based on the obtained ellipse you can come to a conclusion whether the image is vertical, horizontal or inclined in any other direction.

-If your contour object is broad, the major axis of the ellipse fit will be horizontal.

-If your contour object is thin and tall, the major axis of the ellipse fit will be vertical.

Now if the obtained ellipse fit is neither vertical nor horizontal, you will need to perform an orientation alignment.

Hope it helps!!!!

EDIT

I guess you want to rotate your image. You can use the getRotationMatrix2D() function available in the OpenCV library (snippet from here):

(x, y) = img.shape[:2]
center = (y / 2, x / 2)

Mat = cv2.getRotationMatrix2D(center, 90, 1.0)
rotate = cv2.warpAffine(img, Mat, (y, x))
cv2.imwrite("rotated.jpg", rotate)
  • 1st parameter : Initially you obtain the center of the image.
  • 2nd parameter : Rotate the image around this center with an angle of your choice.
  • 3rd parameter : This is the scale. It decides how big or small you want your image to be.

Here is your original image:

enter image description here

This is the rotated image obtained:

enter image description here


Post a Comment for "How To Convert The C++ Code To Python For Automatic Image Rotation Using OpenCV?"