Life2Coding
How to Rotate Images at Any Degree usingTrackbar in OpenCV Python

This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply them in various aspects using some great examples. Then the output will be visualized along with the comparisons.

We will also discuss the basic of image processing and provide the detail explanation related to the OpenCV functions.

Requirements:

First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV with Python 3

Goals:

The goal is to make you understand how to rotate images at any degree with trackbar. OpenCV python library enables to create trackbar easily to change any opencv function parameters.

Documentation:

getTrackbarPos()

retval=cv.getTrackbarPos(trackbarname, winname)

Returns the trackbar position.

Parameters
trackbarname Name of the trackbar.
winname Name of the window that is the parent of the trackbar.

getRotationMatrix2D()

retval=cv.getRotationMatrix2D(center, angle, scale)

Calculates an affine matrix of 2D rotation.

Parameters
center Center of the rotation in the source image.
angle Rotation angle in degrees. Positive values mean counter-clockwise rotation (the coordinate origin is assumed to be the top-left corner).
scale Isotropic scale factor.

warpAffine()

dst=cv.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])

Applies an affine transformation to an image.

Parameters
src input image.
dst output image that has the size dsize and the same type as src .
M \(2\times 3\) transformation matrix.
dsize size of the output image.
flags combination of interpolation methods (see InterpolationFlags) and the optional flag WARP_INVERSE_MAP that means that M is the inverse transformation ( \(\texttt{dst}\rightarrow\texttt{src}\) ).
borderMode pixel extrapolation method (see BorderTypes); when borderMode=BORDER_TRANSPARENT, it means that the pixels in the destination image corresponding to the “outliers” in the source image are not modified by the function.
borderValue value used in case of a constant border; by default, it is 0.

imshow()

None=cv.imshow(winname, mat)

Displays an image in the specified window.

Parameters
winname Name of the window.
mat Image to be shown.

imread()

retval=cv.imread(filename[, flags])

Loads an image from a file.

Parameters
filename Name of file to be loaded.
flags Flag that can take values of cv::ImreadModes

namedWindow()

None=cv.namedWindow(winname[, flags])

Creates a window.

Parameters
winname Name of the window in the window caption that may be used as a window identifier.
flags Flags of the window. The supported flags are: (cv::WindowFlags)

waitKey()

retval=cv.waitKey([, delay])

Waits for a pressed key.

Parameters
delay Delay in milliseconds. 0 is the special value that means “forever”.

destroyAllWindows()

None=cv.destroyAllWindows()

Destroys all of the HighGUI windows.

Steps:

  • Open the image using cv2.imread()
  • Get the height and width of the image using image.shape[:2]
  • Create a trackbar for changing degree of rotation using cv2.createTrackbar()
  • Get the rotation martrix using cv2.getRotationMatrix2D()
  • Apply the rotation to the image using cv2.wardAffline()
  • Display all the rotated image using cv2.imshow()
  • Exit window and destroy all windows using cv2.destroyAllWindows()

Example Code:

import cv2

def funcRotate(degree=0):
    degree = cv2.getTrackbarPos('degree','Frame')
    rotation_matrix = cv2.getRotationMatrix2D((width / 2, height / 2), degree, 1)
    rotated_image = cv2.warpAffine(original, rotation_matrix, (width, height))
    cv2.imshow('Rotate', rotated_image)

if __name__== '__main__':

    original=cv2.imread("./hanif.jpg",1)
    height, width = original.shape[:2]
    cv2.namedWindow('Frame')
    degree=0
    cv2.createTrackbar('degree','Frame',degree,360,funcRotate)
    funcRotate(0)
    cv2.imshow('Frame',original)
    cv2.waitKey(0)

cv2.destroyAllWindows()

Output:

5cc10edc4c7e0 How to Rotate Images at Any Degree usingTrackbar in OpenCV Python
life2coding_icon [] How to Rotate Images at Any Degree usingTrackbar in OpenCV Python

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.