rotate_rect()

home > kero > Documentation

Given a bounding box inside an image, this function returns the bounding box of the rotated image.

kero.ImageProcessing.photoBox.py

def rotate_rect(coord, theta, img_width, img_height):
  return [xmin0,ymin0,xmax0,ymax0]
coord List of integers, coord = [xmin,ymin,xmax,ymax] is the coordinates of the rectangles in pixel. [xmin, ymin] is the top-left corner of the image while [xmax, ymax] is the bottom-right corner.
theta Float. Angle of rotation in radians.
img_width Integer. Image width in number of pixels.
Img_height Integer. Image height in number of pixels.
return coord_out List of integers, coord_out = [xmin,ymin,xmax,ymax]  is the rotated coordinates.

Example usage 1

Both images boxrotate.png and boxrotate2.png can be downloaded here.  The results for both images are partially shown here. The original bounding box is in green. The images are then rotated, and the red bounding boxes are the new bounding boxes computed from the green boxes and the angles of rotation.

boxrot.png

import numpy as np
import kero.ImageProcessing.photoBox as kip
import cv2, os

filename_set= []
coord_set = []

filename = "boxrotate.png"
xmin = 42
ymin = 40
xmax = 145 # 200
ymax = 125 # 250
coord = [xmin,ymin,xmax,ymax]
filename_set.append(filename)
coord_set.append(coord)

filename = "boxrotate2.png"
coord = [66,58,244,193]
filename_set.append(filename)
coord_set.append(coord)

count = 1
foldercount = 1
for filename in filename_set:
	if not os.path.exists(''.join(("result",str(foldercount)))):
		os.mkdir(''.join(("result",str(foldercount))))
	coord = coord_set[foldercount-1]
	gg=cv2.imread(filename)

	img_height, img_width, _ = gg.shape

	cv2.rectangle(gg, (coord[0], coord[1]), (coord[2],coord[3]),(0,255,0),3)

	theta_set = np.linspace(10,350,20)
	theta_set = [x*np.pi/180 for x in theta_set]
	for theta in theta_set:
		gg2=cv2.imread(filename)
		rows,cols,_ = gg2.shape
		M = cv2.getRotationMatrix2D((cols/2,rows/2),theta*180/np.pi,1)
		dst = cv2.warpAffine(gg2,M,(cols,rows))
		[xmin0,ymin0,xmax0,ymax0] = kip.rotate_rect(coord, theta, img_width, img_height)
		cv2.rectangle(dst, (xmin0, ymin0), (xmax0,ymax0),(0,0,255),3)
		cv2.imwrite(''.join(("result",str(foldercount),"/rotate",str(count),".png")), dst)
		cv2.imwrite(''.join(("result",str(foldercount),"/orign.png")),gg)
		count = count+1
	foldercount = foldercount + 1

kero version: 0.4.2 and above