0% found this document useful (0 votes)
9 views

Image processing file

Image preprocessing is essential for converting raw image data into a usable format for computer vision applications, involving techniques like resizing, grayscaling, noise reduction, normalization, binarization, and contrast enhancement. Python libraries such as OpenCV and Pillow are commonly used for loading and manipulating images, allowing for conversions between color spaces and standardizing image dimensions. Normalizing pixel values and applying filters like Gaussian and median blur further enhance image quality for machine learning tasks.

Uploaded by

Zareen khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Image processing file

Image preprocessing is essential for converting raw image data into a usable format for computer vision applications, involving techniques like resizing, grayscaling, noise reduction, normalization, binarization, and contrast enhancement. Python libraries such as OpenCV and Pillow are commonly used for loading and manipulating images, allowing for conversions between color spaces and standardizing image dimensions. Normalizing pixel values and applying filters like Gaussian and median blur further enhance image quality for machine learning tasks.

Uploaded by

Zareen khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Image processing file

What Is Image Preprocessing and Why Is It Important?

Image preprocessing is the process of manipulating raw image data into a usable and
meaningful format. It allows you to eliminate unwanted distortions and enhance
specific qualities essential for computer vision applications. Preprocessing is a crucial
first step to prepare your image data before feeding it into machine learning models.

There are several techniques used in image preprocessing:


• Resizing: Resizing images to a uniform size is important for machine learning
algorithms to function properly. We can use OpenCV’s resize() method to resize
images.
• Grayscaling: Converting color images to grayscale can simplify your image data and
reduce computational needs for some algorithms. The cvtColor() method can be used
to convert RGB to grayscale.
• Noise reduction: Smoothing, blurring, and filtering techniques can be applied to
remove unwanted noise from images. The GaussianBlur () and medianBlur () methods
are commonly used for this.
• Normalization: Normalization adjusts the intensity values of pixels to a desired
range, often between 0 to 1. This can improve the performance of machine learning
models. Normalize () from scikit-image can be used for this.
• Binarization: Binarization converts grayscale images to black and white by
thresholding. The threshold () method is used to binarize images in OpenCV.
• Contrast enhancement: The contrast of images can be adjusted using histogram
equalization. The equalizeHist () method enhances the contrast of images.
With the right combination of these techniques, you can significantly improve your
image data and build better computer vision applications. Image preprocessing allows
you to refine raw images into a format suitable for the problem you want to solve.

Loading and Converting Images With Python Libraries

To get started with image processing in Python, you’ll need to load and convert your
images into a format the libraries can work with. The two most popular options for this
are OpenCV and Pillow.

Loading images with OpenCV: OpenCV can load images in formats like PNG, JPG,
TIFF, and BMP. You can load an image with:

import cv2
image = cv2.imread(path/to/image.jpg')
This will load the image as a NumPy array. The image is in the BGR color space, so
you may want to convert it to RGB.

Loading images with Pillow: Pillow is a friendly PIL (Python Image Library) fork. It
supports even more formats than OpenCV, including PSD, ICO, and WEBP. You can
load an image with:

from PIL import Image


image = Image.open('path/to/image.jpg')

The image will be in RGB color space.

Converting between color spaces: You may need to convert images between color
spaces like RGB, BGR, HSV, and Grayscale. This can be done with OpenCV or
Pillow. For example, to convert BGR to Grayscale in OpenCV, use:

gray = cv2.cvtColor (image, cv2.COLOR_BGR2GRAY)

Or to convert RGB to HSV in Pillow:

image = image.convert('HSV')

With these foundational skills, you’ll be ready to move on to more advanced


techniques like resizing, filtering, edge detection, and beyond. The possibilities are
endless! What image processing project will you build?

Resizing and Cropping Images to Standard Dimensions

Resizing and cropping your images is an important first step in image preprocessing.
Images come in all shapes and sizes, but machine learning algorithms typically require
a standard size. You’ll want to resize and crop your images to square dimensions, often
224x224 or 256x256 pixels.
In Python, you can use the OpenCY or Pillow library for resizing and cropping. With
OpenCV, use the resize() function. For example:

import cv2
img = cv2.imread ('original.jpg')
resized = cV2.resize(img, (224, 224))

This will resize the image to 224x224 pixels.

To crop an image to a square, you can calculate the center square crop size and use
crop() in OpenCV with the center coordinates. For example:

Code

from PIL import Image

# Open an image

img = Image.open("example.jpg")

# Define the cropping box (left, upper, right, lower)

crop_box = (100, 100, 400, 400)

# Crop the image

cropped_img = img.crop(crop_box)

# Show the cropped image

cropped_img.show()
# Save the cropped image

cropped_img.save("cropped_example.jpg")

Import Numpy as mp

numpy as np is a common way to import the NumPy library in Python. NumPy is a


powerful library used for numerical computing, especially for working with arrays
and matrices.

import numpy as np

# Create a NumPy array

arr = np.array([1, 2, 3, 4, 5])

print(arr)

Definition of cv2.waitKey(0)

cv2.waitKey(0) is an OpenCV function that waits indefinitely for a key press before
continuing execution.

Normalising Pixel Values for Consistent Brightness

When working with image data, it’s important to normalize the pixel values to have a
consistent brightness and improve contrast. This makes the images more suitable for
analysis and allows machine learning models to learn patterns independent of lighting
conditions.

Rescaling Pixel Values: The most common normalization technique is rescaling the
pixel values to range from 0 to 1. This is done by dividing all pixels by the maximum
pixel value (typically 255 for RGB images). For example:

import cv2
Img = cv2.imread ('image.jpg')
normalized = img / 255.0
Gaussian Blur:
The Gaussian blur filter reduces detail and noise in an image. It “blurs” the image by
applying a Gaussian function to each pixel and its surrounding pixels. This can help
smooth edges and details in preparation for edge detection or other processing
techniques.

code

import cv2

# Load the image

img = cv2.imread("image.jpg")

# Apply Gaussian Blur with a 5x5 kernel

blurred = cv2.GaussianBlur(img, (5,5), 0)

# Show the blurred image

cv2.imshow("Blurred Image", blurred)

cv2.waitKey(0)

cv2.destroyAllWindows()

explain
cv2.GaussianBlur(src, ksize, sigmaX)
# cv2.GaussianBlur(img, (5,5), 0)

 src → Input image


 ksize → Kernel size (width, height); must be odd (e.g., (3,3), (5,5), etc.)
 sigmaX → Standard deviation in the X direction (set 0 to let OpenCV decide)
MedianBlur
The median blur filter is useful for removing salt and pepper noise from an image. It
works by replacing each pixel with the median value of its neighboring pixels. This
can help smooth out isolated noisy pixels while preserving edges.

cv2.medianBlur(src, ksize)

 src → Input image


 ksize → Kernel size (must be an odd number, e.g., 3, 5, 7)

Example:

python
CopyEdit
import cv2
# Load the image
img = cv2.imread("image.jpg")
# Apply Median Blur with a 5x5 kernel
median_blurred = cv2.medianBlur(img, 5)
# Show the blurred image
cv2.imshow("Median Blurred Image", median_blurred)
cv2.waitKey(0)
cv2.destroyAllWindows()

You might also like