Image Processing #3 - Rotate

Sample codes are in my Repo.

Rotate image

Rotation using cv2.warpAffine




import argparse
import cv2
import numpy as np


parser = argparse.ArgumentParser(description="OpenCV Example")
parser.add_argument("--file", type=str, required=True, help="filename of the input image to process")
parser.add_argument("--angle", type=int, required=True, help="rotate angle(degree)")
args = parser.parse_args()

img = cv2.imread(args.file, cv2.IMREAD_COLOR)
height, width, channels = img.shape
print("image   H:%d W:%d, Channel:%d"%(height, width, channels))
cv2.imshow('original', img)

matrix = cv2.getRotationMatrix2D((width/2, height/2), args.angle, 1)
img = cv2.warpAffine(img, matrix, (width, height))
height, width, channels = img.shape
print("rotate image   H:%d W:%d, Channel:%d"%(height, width, channels))

cv2.imshow('rotate', img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Run the code.



python exam4.py --file=biden.png --angle=90
image   H:727 W:320, Channel:3
rotate image   H:727 W:320, Channel:3




Rotation using numpy.rot90

If you want to rotate the image 90 degrees and want to keep the original image size, numpy.rot90 function is useful. The width and height values ​​of the rotated image will be swapped.



import argparse
import cv2
import numpy as np


def rotate(img):
    rot = np.rot90(img)
    return rot

parser = argparse.ArgumentParser(description="OpenCV Example")
parser.add_argument("--file", type=str, required=True, help="filename of the input image to process")
args = parser.parse_args()

img = cv2.imread(args.file, cv2.IMREAD_COLOR)
height, width, channels = img.shape
print("image   H:%d W:%d, Channel:%d"%(height, width, channels))
cv2.imshow('original', img)


img_translation = rotate(img)

cv2.imshow('translate', img_translation)

cv2.waitKey(0)
cv2.destroyAllWindows()

Run the code.


python exam4-numpy.py --file=biden.jpg
image   H:727 W:320, Channel:3




You can download the source codes here(https://github.com/raspberry-pi-maker/OpenCV)






댓글

이 블로그의 인기 게시물

Image Processing #7 - OpenCV Text

OpenCV Installation - Rasbian Buster, Jessie, DietPi Buster

Playing YouTube videos using OpenCV