To be 100% reproducible, the entire code (and the image used) is below .
Here are the steps in the code.
I superimpose the contours (
[[386, 330], [398, 320], [382, 300], [370, 310], [386, 330]]
) on the image. That's works fine.Rotate the image by theta (= 90) using
warpAffine
Rotate each of the points in the contour in a dummy image of exactly the same size and the same rotating function.
Find the coordinates of the rotated point.
Superimpose rotated points on the rotated image.
I expected them to lineup but they don't.
What could be the reason?
#! /usr/bin/env python
import numpy as np
import cv2
from shapely.geometry.polygon import LinearRing
theta = 90
image_path = 'image.tiff'
orig_image = cv2.imread(image_path)
wnd_str = 'unrotated'
cv2.namedWindow(wnd_str,cv2.WINDOW_NORMAL)
cv2.moveWindow(wnd_str, 0, 0)
rows,cols, channels = orig_image.shape
print 'Loaded image shape {}'.format(orig_image.shape)
blank_image = np.zeros((rows, cols, 3), np.uint8)
print 'Blank image shape {}'.format(blank_image.shape)
M = cv2.getRotationMatrix2D((rows/2, cols/2), theta, 1.0)
rot_image = cv2.warpAffine(orig_image, M, (rows, cols), flags=cv2.INTER_CUBIC+cv2.BORDER_CONSTANT)
print 'Rotated image shape {}'.format(rot_image.shape)
white = (255, 255, 255)
#contours overlayed on unrotated image
pts = [[386, 330], [398, 320], [382, 300], [370, 310], [386, 330]]
poly_pts = []
for p in pts: poly_pts.append(p)
poly_pts = np.array(poly_pts[0:-1], np.int32)
poly_pts = poly_pts.reshape((-1,1,2))
cv2.polylines(orig_image, [poly_pts], True, white, 1)
cv2.imshow(wnd_str, orig_image)
cv2.waitKey(0)
#generate contours for the rotated image
rot_poly_pts = []
for p in pts:
x, y = p
blank_image = np.zeros((rows, cols, 3), np.uint8)
blank_image[x,y] = (255, 255, 255)
blank_image_affine = cv2.warpAffine(blank_image, M, (rows, cols), flags=cv2.INTER_CUBIC+cv2.BORDER_CONSTANT)
rotated_y, rotated_x, rotated_z = np.unravel_index(blank_image_affine.argmax(), blank_image_affine.shape)
rot_poly_pts.append([rotated_x, rotated_y])
rot_poly_pts = np.array(rot_poly_pts[0:-1], np.int32)
rot_poly_pts = rot_poly_pts.reshape((-1,1,2))
cv2.polylines(rot_image, [rot_poly_pts], True, white, 1)
wnd_str = 'rotated {}'.format(theta)
cv2.namedWindow(wnd_str,cv2.WINDOW_NORMAL)
cv2.moveWindow(wnd_str, 0, 0)
cv2.imshow(wnd_str, rot_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
[1]: https://i.stack.imgur.com/pyxmq.png