Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to straighten a rotated rectangle area of an image using OpenCV in Python?

The following picture will tell you what I want.

I have the information of the rectangles in the image (width, height, center point and rotation degree). Now, I want to write a script to cut them out and save them as an image, but straighten them as well. As in, I want to go from the rectangle shown inside the image to the rectangle that is shown outside.

I am using OpenCV Python. Please tell me a way to accomplish this.

Kindly show some code as examples of OpenCV Python are hard to find.

Example Image

like image 865
hakunami Avatar asked Jul 24 '12 08:07

hakunami


People also ask

How do you change the angle of an image in Python?

The imutils. rotate() function is used to rotate an image by an angle in Python.


1 Answers

You can use the warpAffine function to rotate the image around a defined center point. The suitable rotation matrix can be generated using getRotationMatrix2D (where theta is in degrees).

Start ImageAfter finding the desired rectangle

You then can use Numpy slicing to cut the image.

Rotated Image Result

import cv2 import numpy as np  def subimage(image, center, theta, width, height):     '''     Rotates OpenCV image around center with angle theta (in deg)    then crops the image according to width and height.    '''     # Uncomment for theta in radians    #theta *= 180/np.pi     shape = ( image.shape[1], image.shape[0] ) # cv2.warpAffine expects shape in (length, height)     matrix = cv2.getRotationMatrix2D( center=center, angle=theta, scale=1 )    image = cv2.warpAffine( src=image, M=matrix, dsize=shape )     x = int( center[0] - width/2  )    y = int( center[1] - height/2 )     image = image[ y:y+height, x:x+width ]     return image 

Keep in mind that dsize is the shape of the output image. If the patch/angle is sufficiently large, edges get cut off (compare image above) if using the original shape as--for means of simplicity--done above. In this case, you could introduce a scaling factor to shape (to enlarge the output image) and the reference point for slicing (here center).

The above function can be used as follows:

image = cv2.imread('owl.jpg') image = subimage(image, center=(110, 125), theta=30, width=100, height=200) cv2.imwrite('patch.jpg', image) 
like image 140
rroowwllaanndd Avatar answered Sep 27 '22 20:09

rroowwllaanndd