SoFunction
Updated on 2024-10-30

OpenCV Image Rotation, Panning, and Scaling Operations Code

This article is the 7th article in the Road to Getting Started with OpenCV Image Vision, which performs detailed operations such as scaling (), rotating (), and panning () of images.

1 Zoom image

Scaling is to resize the image, use the () function to realize the scaling, either proportionally or according to the specified size:
The scaling method can also be specified as linear interpolation INTER_LINEAR.

There are five types of interpolation in the placement process:

cv2.INTER_NEAREST Nearest Neighbor Interpolation cv2.INTER_LINEAR Linear Interpolation cv2.INTER_AREA Local Pixel Based Resampling, Area Interpolation cv2.INTER_CUBIC Cubic Neighborhood Based Cubic Interpolation 4x4 Pixel Neighborhood based cv2.INTER_LANCZOS4 Neighborhood Based 8x8 Pixel Neighborhood based Lanczos interpolation

import cv2
from PIL import Image
import  as plt
import numpy as np
 
# Press the green button in the pitch to run the script.
if __name__ == '__main__':
    image = ('D:/Jupyter_Notebooks/')
 
    # Conversion from RGB color space to HSV color space
    hsv = (image, cv2.COLOR_BGR2RGB)
 
    # Scale the image to the specified width, height
    res = (image, (960, 540))
    # Zoom in proportionally, e.g. double the size of the x and y axes.
    res2 = (image, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
 
    ("image", image)
    ("res2", res2)
 
    (0)
    ()

2 Flip the picture

To mirror flip an image, you can use the()function:
where parameter 2 = 0: flip vertically (along the x-axis), parameter 2 > 0: flip horizontally (along the y-axis), parameter 2 < 0: flip horizontally vertically.

import cv2
from PIL import Image
import  as plt
import numpy as np
 
# Press the green button in the pitch to run the script.
if __name__ == '__main__':
    image = ('D:/Jupyter_Notebooks/')
 
    # Conversion from RGB color space to HSV color space
    # image = (image, cv2.COLOR_BGR2RGB)
 
    dst = (image, 0)
 
 
    ("image", image)
    ("res2", dst)
 
    (0)
    ()

2.1 Vertical flip

dst = (image, 0) 

2.2 Horizontal Flip

dst = (image, 1) 

2.3 Horizontal Vertical Flip

dst = (image, -1)  

3 Panning Pictures

To pan the image, we need to define a matrix like the following, where tx,ty are the distances to be panned in the x and y directions:

The translation is done with the affine transformation function()Realized:

import cv2
from PIL import Image
import  as plt
import numpy as np
 
# Press the green button in the pitch to run the script.
if __name__ == '__main__':
    image = ('D:/Jupyter_Notebooks/')
 
    # Get the height and width of the image
    rows, cols = [:2]
 
    # Define the translation matrix, needs to be numpy float32 type x-axis translation 100, y-axis translation 500
    M = np.float32([[1, 0, 100], [0, 1, 200]])
    
    # Translation by affine transformation
    dst = (image, M, (cols, rows))
 
    # ("image", image)
    ("res2", dst)
 
    (0)
    ()

to this article on OpenCV image rotation, translation, scaling operation code is introduced to this article, more related to OpenCV image rotation content, please search for my previous articles or continue to browse the following articles hope that you will support me in the future!