Basic Transformations
OpenCV (Open Source Computer Vision Library) is a powerful open-source library designed for real-time computer vision and image processing. It provides tools for manipulating images, detecting objects, and even working with deep learning models.
Reading and Displaying an Image
Before performing transformations, let's first load and display an image using OpenCV. We have already imported the photo. But in your local computer for simple reading and displaying the photo you should use:
import cv2
image = cv2.imread('path/to/image.jpg')
cv2.imshow('Image title', image)
Resizing an Image
Resizing is useful for scaling images up or down while maintaining aspect ratio (fx
and fy
parameters),
where fx
and fy
define the scaling factor for width and height.
resized_image = cv2.resize(image, fx=0.7, fy=0.5)
You can also specify exact pixel dimensions (dsize
parameter),
where dsize
is (new_y, new_x)
output shape.
resized = cv2.resize(image, (100, 100))
Rotating an Image
To rotate an image by a specific angle, we use cv2.getRotationMatrix2D()
and cv2.warpAffine()
.
cv2.getRotationMatrix2D(center, angle, scale)
defines the rotation matrix;cv2.warpAffine(image, matrix, output_size)
applies the transformation.
height, width = image.shape[:2]
centre = (width // 2, height // 2)
rotation_matrix = cv2.getRotationMatrix2D(centre, 180, 1)
rotated = cv2.warpAffine(image, rotation_matrix, (width, height))
Cropping an Image
Cropping extracts a specific region from an image. It's done using NumPy slicing.
The syntax image[start_y:end_y, start_x:end_x]
selects a region of interest.
cropped = image[450:500, 250:350]
Swipe to start coding
You are given an image
:
- Resize image to a
(100, 100)
shape and store it inresized
variable; - Extract
height
andwidth
from image; - Calculate
center
ofheight
andwidth
; - Create rotation matrix and stote it in
rotation_matrix
variable; - Rotate image 90 degrees clockwise and stote it in
rotated
variable; - Crop the X: 250-600 and Y: 100-450 region of the image and store in
cropped
variable.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.45
Basic Transformations
Swipe to show menu
OpenCV (Open Source Computer Vision Library) is a powerful open-source library designed for real-time computer vision and image processing. It provides tools for manipulating images, detecting objects, and even working with deep learning models.
Reading and Displaying an Image
Before performing transformations, let's first load and display an image using OpenCV. We have already imported the photo. But in your local computer for simple reading and displaying the photo you should use:
import cv2
image = cv2.imread('path/to/image.jpg')
cv2.imshow('Image title', image)
Resizing an Image
Resizing is useful for scaling images up or down while maintaining aspect ratio (fx
and fy
parameters),
where fx
and fy
define the scaling factor for width and height.
resized_image = cv2.resize(image, fx=0.7, fy=0.5)
You can also specify exact pixel dimensions (dsize
parameter),
where dsize
is (new_y, new_x)
output shape.
resized = cv2.resize(image, (100, 100))
Rotating an Image
To rotate an image by a specific angle, we use cv2.getRotationMatrix2D()
and cv2.warpAffine()
.
cv2.getRotationMatrix2D(center, angle, scale)
defines the rotation matrix;cv2.warpAffine(image, matrix, output_size)
applies the transformation.
height, width = image.shape[:2]
centre = (width // 2, height // 2)
rotation_matrix = cv2.getRotationMatrix2D(centre, 180, 1)
rotated = cv2.warpAffine(image, rotation_matrix, (width, height))
Cropping an Image
Cropping extracts a specific region from an image. It's done using NumPy slicing.
The syntax image[start_y:end_y, start_x:end_x]
selects a region of interest.
cropped = image[450:500, 250:350]
Swipe to start coding
You are given an image
:
- Resize image to a
(100, 100)
shape and store it inresized
variable; - Extract
height
andwidth
from image; - Calculate
center
ofheight
andwidth
; - Create rotation matrix and stote it in
rotation_matrix
variable; - Rotate image 90 degrees clockwise and stote it in
rotated
variable; - Crop the X: 250-600 and Y: 100-450 region of the image and store in
cropped
variable.
Solution
Thanks for your feedback!
single