0% found this document useful (0 votes)
29 views6 pages

Chapter 2 OpenCV

Uploaded by

mo.khalid.2026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views6 pages

Chapter 2 OpenCV

Uploaded by

mo.khalid.2026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 2: Image Manipulation

In this chapter, we'll delve into image manipulation techniques using


OpenCV. We'll cover a variety of operations to modify and transform
images, including resizing, cropping, rotating, filtering, and more.
2.1 Image Resizing:
Image resizing is a common operation in computer vision. Let's
explore di erent methods to resize images using OpenCV:
import cv2

# Read an image
image = [Link]('[Link]')

# Resize the image using di erent interpolation methods


resized_nearest = [Link](image, (400, 300),
interpolation=cv2.INTER_NEAREST)
resized_linear = [Link](image, (400, 300),
interpolation=cv2.INTER_LINEAR)
resized_cubic = [Link](image, (400, 300),
interpolation=cv2.INTER_CUBIC)

# Display the original and resized images


[Link]('Original Image', image)
[Link]('Resized Nearest', resized_nearest)
[Link]('Resized Linear', resized_linear)
[Link]('Resized Cubic', resized_cubic)
[Link](0)
[Link]()
In this example, we read an image and resize it using di erent
interpolation methods:
* cv2.INTER_NEAREST: Nearest-neighbor interpolation, which uses
the closest pixel value.
* cv2.INTER_LINEAR: Bilinear interpolation, which computes the
weighted average of four nearest pixels.
* cv2.INTER_CUBIC: Bicubic interpolation, which applies cubic
interpolation to each pixel neighborhood.
* We display the original image and the resized images using the
imshow function.
2.2 Image Cropping:
Cropping allows you to extract a region of interest (ROI) from an
image. Let's demonstrate how to define cropping regions using
coordinates:
import cv2

# Read an image
image = [Link]('[Link]')
# Define the cropping region coordinates
x, y, width, height = 100, 100, 200, 200

# Crop the image


cropped_image = image[y:y+height, x:x+width]

# Display the original and cropped images


[Link]('Original Image', image)
[Link]('Cropped Image', cropped_image)
[Link](0)
[Link]()
In this code snippet, we read an image and define the cropping region
using the coordinates x, y, width, and height. We then extract the
specified region from the image using array slicing. Finally, we display
both the original image and the cropped image using the imshow
function.
2.3 Image Rotation:
Image rotation is essential for tasks such as correcting image
orientation or aligning images in a specific direction. Let's cover
rotation transformations using OpenCV:
import cv2
import numpy as np
# Read an image
image = [Link]('[Link]')

# Get image dimensions


height, width = [Link][:2]

# Define the rotation angle in degrees


angle = 45

# Calculate the rotation matrix


rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), angle,
1)

# Apply the rotation transformation


rotated_image = [Link] ine(image, rotation_matrix, (width,
height))

# Display the original and rotated images


[Link]('Original Image', image)
[Link]('Rotated Image', rotated_image)
[Link](0)
[Link]()
In this example, we read an image and define the rotation angle. We
calculate the rotation matrix using the getRotationMatrix2D function,
specifying the rotation center and angle. Then, we apply the rotation
transformation to the image using the warpA ine function. Finally, we
display both the original image and the rotated image using the
imshow function.
2.4 Image Filtering:
Image filtering techniques are used to enhance images or extract
specific features. Let's explore some common image filtering
operations using OpenCV:
import cv2

# Read an image
image = [Link]('[Link]')

# Apply di erent image filters


blurred_image = [Link](image, (5, 5))
gaussian_blurred_image = [Link](image, (5, 5), 0)
median_filtered_image = [Link](image, 5)

# Display the original and filtered images


[Link]('Original Image', image)
[Link]('Blurred Image', blurred_image)
[Link]('Gaussian Blurred Image', gaussian_blurred_image)
[Link]('Median Filtered Image', median_filtered_image)
[Link](0)
[Link]()
In this code snippet, we read an image and apply the following filters:
* blur: Applies a simple averaging filter to the image.
* GaussianBlur: Applies a Gaussian filter to the image.
* medianBlur: Applies a median filter to the image.
We display the original image and the filtered images using the
imshow function.
By the end of this chapter, you'll have a solid understanding of image
manipulation techniques in OpenCV. You'll be able to resize images,
crop regions of interest, rotate images, and apply various filters to
enhance image quality or extract useful information.

You might also like