0

To be 100% reproducible, the entire code (and the image used) is below .enter image description here

Here are the steps in the code.

  1. I superimpose the contours ([[386, 330], [398, 320], [382, 300], [370, 310], [386, 330]]) on the image. That's works fine.

  2. Rotate the image by theta (= 90) using warpAffine

  3. Rotate each of the points in the contour in a dummy image of exactly the same size and the same rotating function.

  4. Find the coordinates of the rotated point.

  5. 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

Here's the original image with the aligned contour enter image description here

Here's the image and superimposition after the rotation. enter image description here

auro
  • 1,079
  • 1
  • 10
  • 22

1 Answers1

1

wrapAffine produces images with clipped pixels on either side. An effective alternative to this procedure is to transpose and then flip.

dst = cv2.flip(img.transpose(1,0,2),0)

dst  = destination image;
img  = source image;

img.transpose(col,row,channel) = 1,0,2 corresponds to this order, since we want to transpose the rows and col only and keep the channel untouched.

cv2.flip = to counter the mirroring effect caused during the transpose operation. Vary the flag as 0 or 1 depending on your source image (landscape or portrait).

iled
  • 2,142
  • 3
  • 31
  • 43