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.
The imutils. rotate() function is used to rotate an image by an angle in Python.
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).
You then can use Numpy slicing to cut the image.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With