python - Rotating images doesn't work -
below there part of code flips images simple line:
for in range(num_images): im = cv2.imread(roidb[i]['image']) if roidb[i]['hflipped']: im = im[:, ::-1, :] if roidb[i]['vflipped']: im = im[::-1, :, :]
the hflipped
means flipping images horizontally , vflipped
means flipping them vertically. want add part rotate each image 90 degrees anticlockwise.
i have tried 2 options, neither of them works:
1)
if roidb[i]['rotated']: im = im.rotate(im, 90)
2)
num_rows, num_cols = im.shape[:2] if roidb[i]['rotated']: rotation_matrix = cv2.getrotationmatrix2d((num_cols/2, num_rows/2), 90, 1) img_rotation = cv2.warpaffine(im, rotation_matrix, (num_cols, num_rows))
is there way rotate images similar flipping? or there better way?
thanks
because python wrappers opencv use numpy
backend, consider using numpy.rot90
designed rotate matrices multiples of 90 degrees. works colour images horizontal , vertical dimensions rotated independently per channel. second parameter multiple k
specifies how many multiples of 90 degrees rotate image. positive values perform anti-clockwise rotation while negative values perform clockwise rotation. in case, specify second parameter k=1
rotate 90 degrees anti-clockwise. coincidentally, default value second parameter k=1
, can omit it. if wanted rotate clockwise though, specify k=-1
.
adding logic code follows:
import numpy np # setup code goes here... # ... # ... # main code in question in range(num_images): im = cv2.imread(roidb[i]['image']) if roidb[i]['hflipped']: im = im[:, ::-1, :] if roidb[i]['vflipped']: im = im[::-1, :, :] if roidb[i]['rotated']: # new im = np.rot90(im, k=1) # rotates 90 degrees anti-clockwise #im = np.rot90(im) # rotates 90 degrees anti-clockwise #im = np.rot90(im, k=-1) # rotates 90 degrees clockwise # ... # rest of code follows
Comments
Post a Comment