Dip Unit 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Unit-II

1. List the steps involved in image processing pipelining.

a) Image Acquisition: This is the initial step where the raw image data is captured using imaging devices such as cameras or
scanners.

b) Image preprocessing: The goal of preprocessing is to enhance the quality of the image, remove unwanted artifacts, and
prepare the image for subsequent tasks such as feature extraction, object recognition, or image analysis.

i) Image enhancement: aims to improve the visual quality, clarity of an image by adjusting the brightness, contrast,
and color balance.

ii) Image restoration: used to recover or restore degraded or damaged images to improve their quality caused by
noise, blurring

iii) Image denoising: used to reduce or remove noise from an image

iv) Image segmentation: dividing an image into meaningful and distinct regions or objects. Segmentation can be
based on various criteria, such as color, intensity, texture.

c) Feature extraction: image processing refers to the process of identifying and extracting meaningful and relevant
information or features from an image. It can be used for various tasks such as image recognition, object detection, image
classification.

d) Recognition/detection: involves identifying and classifying objects or patterns of interest within an image.

2. Explain primary function of image I/O in python.

The primary function of image I/O (Input/Output) in Python is to read and write image files in various formats.

primary functions of image I/O:

a) Reading Images: Image I/O libraries allow you to read images stored in different formats such as JPEG, PNG, GIF.

b) Writing Images: Image I/O libraries also provide functionality to write or save images into various formats.

c) Data Conversion: Image I/O libraries often include functions for converting image data between different formats or
representations.

d) Error Handling: Image I/O libraries typically include error handling mechanisms to handle issues such as missing files,
unsupported formats, or corrupt image data gracefully.

3. Describe common basic image manipulation.

Image manipulation involves performing simple operations to modify the appearance or content of an image.

a) Resizing: Changing the dimensions of an image while preserving its aspect ratio.
b) Cropping: Removing unwanted portions of an image to focus on a particular subject or area of interest.

c) Rotation and Flipping: Rotating an image to change its orientation. Flipping can be horizontal or vertical and is useful for
correcting images or creating mirror effects.

d) Color Adjustment: Modifying the colors in an image by adjusting hue, saturation, and lightness (HSL), or using other color
models such as RGB or CMYK.

e) Noise Reduction: Removing unwanted noise or graininess from an image caused by factors such as low light conditions.

f) Sharpening: Enhancing the clarity and sharpness of details in an image by increasing contrast along edges.

4. Python libraries that can be used for image I/O.

Popular Python libraries for image I/O:

a) Pillow (PIL): A fork of the Python Imaging Library (PIL), Pillow is a widely used library for image processing and I/O. It
provides comprehensive support for reading and writing various image formats and offers extensive functionality for image
manipulation.

b) OpenCV: OpenCV (Open Source Computer Vision Library) is a powerful library for computer vision tasks, including image
I/O. It supports a wide range of image formats and provides efficient algorithms for image processing, analysis, and
computer vision tasks.

c) Scikit-image: Scikit-image is a Python library for image processing built on top of NumPy, SciPy, and Matplotlib. It offers a
high-level interface for common image processing tasks and includes functions for reading and writing images in different
formats.

5. Highlight the importance of image normalization.

Image normalization is a process that changes the range of pixel intensity values in an image. It's also known as contrast
stretching.

a) Improves contrast: Normalization expands a narrow range of input intensity values into a wider range of output intensity
values. This can improve the contrast in an image. For example, it can improve photographs with poor contrast due to glare.

b) Improves model training: After normalization, the image feature values can be adjusted to a similar range. This can
improve the model training effect.

c) Speeds up learning: In general, normalization speeds up the learning process.

6. Explain basic image manipulations that can be done using python.

a) Cropping: removing unwanted parts of the image. This can be done using the crop() method.
b) Resizing: Involves changing its dimensions. This can be done using the resize() method.

c) Rotating: Involves turning it around a fixed point. This can be done using the rotate() method.

d) Flipping: Involves mirroring it across a vertical or horizontal axis. This can be done using the transpose() method.

e) Converting: Involves changing its format. This can be done using the convert() method.

Code:

# Crop an image

from PIL import Image

image = Image.open("image.jpg")

cropped_image = image.crop((100, 100, 300, 300))

cropped_image.save("cropped_image.jpg")

# Resize an image

resized_image = image.resize((500, 500))

resized_image.save("resized_image.jpg")

# Rotate an image

rotated_image = image.rotate(45)

rotated_image.save("rotated_image.jpg")

# Flip an image

flipped_image = image.transpose(Image.FLIP_LEFT_RIGHT)

flipped_image.save("flipped_image.jpg")

# Convert an image

converted_image = image.convert("L")

converted_image.save("converted_image.jpg")

7. State the purpose of sampling in image formation.

Sampling is the process of selecting pixels from an original image to create a new one. The new image is called the sample
and is used to determine the resolution of an image.
Spatial resolution is the smallest detail that can be seen in an image. Sampling affects the resolution of an image by the
number of pixels in the x and y directions.

Sampling is necessary because it allows continuous visual data to be converted into a digital format. This makes it easier for
computers to process and display images accurately.

Main purpose is to determine the resolution of an image.

One of the ways in which sampling helps define an image's resolution is through downsampling and upsampling.

8. FFT & DFT stands for what?

FFT stands for Fast Fourier Transform, and DFT stands for Discrete Fourier Transform. Both are algorithms that convert
signal samples from the time domain to the frequency domain.

DFT is useful for analyzing signals to see what frequencies are present whereas FFT is used in many applications, including
image processing, audio signal processing, and spectral analysis.

DFT has less speed than the FFT whereas FFT is the faster version of DFT.

Application of DFT is Spectrum estimation, conviction, etc. whereas application of FFT is Convolution, voltage measurement,
etc.

9. How does quantization affect the quality of an image?

Quantization is a lossy compression technique that reduces image data by removing some detail information.

It does this by mapping groups of data points to a single point.

Quantization affects the detail and precision of a digital image, but does not affect the size.

The most common artifacts of quantization are banding and blocking.

Quantization is used to convert a continuous image into a digital image.

Quantization level determines the number of grey levels in the digitized image.

10. Explain the significance of fourier transform in image processing.

The Fourier transform is a mathematical operation that decomposes a function into its frequency components.

Fourier transform can be used to break down an image into its constituent sine and cosine waves.

Decomposition of an image can be used for a variety of purposes including:


a) Image filtering: The Fourier transform can be used to filter out unwanted noise from an image, or to enhance certain
features of an image.

b) Image reconstruction: The Fourier transform can be used to reconstruct an image from a compressed version of the
image.

c) Image compression: The Fourier transform can be used to compress an image by removing unnecessary frequency
components.

Applications:

a) Edge detection: The Fourier transform can be used to detect edges in an image by identifying the frequencies that
correspond to sharp changes in intensity.

b) Noise reduction: The Fourier transform can be used to reduce noise in an image by filtering out the frequencies that
correspond to random noise.

c) Image compression: The Fourier transform can be used to compress an image by removing the frequencies that
correspond to less important details.

11. How is image resolution related to the sampling frequency.

The sampling frequency determines the distance between the midpoint of one pixel to the midpoint of an adjacent pixel.

Increasing the sampling frequency results in a smaller sampling and pixel pitch, which improves the spatial resolution of the
digital image.

The spatial resolution of a digital image is determined by the distance between pixels, known as the sampling interval, and
the accuracy of the digitizing device.

It is measured in pixcels per mm(pixels/mm).

The resolution of an image isn't determined by its file format but by its pixel dimensions and DPI.

12. Explain difference between spatial domain and frequency domain in image processing.

The spatial domain refers to the image plane itself, while the frequency domain involves transforming the image to the
frequency domain.

Spatial domain: Deals with the image itself, representing an image by the intensities at each pixel. Pixels are treated
independently.

Frequency domain: Deals with the rate at which the pixel values are changing in the spatial domain. Represents a signal as a
superposition of sinusoids of various amplitudes, frequencies, and phases. A given sinusoid covers the whole image.

13. Write the basic steps involved in image formation using FFT & DFT.
The basic steps involved in image formation using FFT (Fast Fourier Transform) and DFT (Discrete Fourier Transform) are :-

i. Acquiring time data: Use a DAQ (Data acquisition system) system to acquire time-domain data from sensors.

ii. Sampling input time data into FFT time blocks: Divide the time-domain data into equal-length blocks called FFT time
blocks.

iii. Transforming the FFT time blocks from the time domain to the frequency domain: The DFT is a complex-valued function
of frequency, which represents the frequency components of the signal in the FFT time block.

iv. Calculating the instantaneous power spectra: The magnitude of the DFT is a real-valued function of frequency, which
represents the power of each frequency component in the FFT time block.

v. Averaging the power spectra over a specified number of spectra or a time duration: To reduce noise, average the power
spectra over a specified number of spectra or a time duration.

14. Name some common image file format that use quantization.

a. JPEG (Joint Photographic Experts Group): JPEG is a widely used image compression format that utilizes quantization to
reduce the amount of data required to represent an image while maintaining visual quality.

b. PNG (Portable Network Graphics): PNG is a lossless compression format, it involves quantization during the compression
process.

c. GIF (Graphics Interchange Format): GIF is a lossless compression format that supports animation. It involves quantizing
pixel values during the encoding process to reduce the size of the data stream.

d. TIFF (Tagged Image File Format): TIFF supports various compression schemes, including lossless and lossy compression
methods.

15. Role of the discreet fourier transform in image processing.

The Discrete Fourier Transform (DFT) plays a significant role in image processing, offering various capabilities and
applications. Some key roles of the DFT in image processing:

a. Frequency Analysis: The DFT allows images to be represented in the frequency domain, where spatial information is
transformed into frequency components.

b. Filtering: Filtering in the frequency domain is a fundamental technique in image processing. The DFT facilitates filtering
operations by allowing image data to be manipulated in the frequency domain. Filters can be designed to enhance specific
frequency components, suppress noise, or remove unwanted features from images.

c. Compression: The DFT provides a basis for image compression techniques such as JPEG (Joint Photographic Experts
Group). By transforming images into the frequency domain and applying quantization and encoding techniques.

d. Feature Extraction: Features such as edges, corners, and texture patterns can be detected by analyzing the magnitude
and phase information of frequency components.
e. Image Restoration: The DFT can be utilized in image restoration tasks to recover or enhance degraded images.

f. Convolution and Filtering: Convolution operations, such as smoothing or sharpening, can be efficiently performed in the
frequency domain using the DFT.

g. Pattern Recognition: The DFT aids in pattern recognition tasks by extracting discriminative features from images.

16. How can you display an image using python?-2 marks

You can display an image using Python with various libraries such as Matplotlib, OpenCV, PIL (Python Imaging Library), or
using built-in functions in some IDEs like Jupyter Notebook.

a. Using PIL:

from PIL import Image

# Open the image

img = Image.open('image.jpg')

# Display the image

img.show()

b. Using OpenCV:

import cv2

# Load the image

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

# Display the image

cv2.imshow('Image', img)

17. How does python's image I/O library help in handling different image format?

Python's image I/O (Input/Output) libraries, such as OpenCV, Pillow (PIL), play a crucial role in handling different image
formats by read, write, and manipulate images. Here's how these libraries help in handling different image formats:
a. Read and Write Support: These libraries offer functions to read and images from various file formats, including JPEG,
PNG, BMP, GIF, TIFF, and many others.

b. Format Conversion: Python's image I/O libraries facilitate format conversion by allowing users to load images in one
format and save them in another.

c. Image Manipulation: These libraries offer a wide range of image manipulation functions, including resizing, rotation,
cropping, color space conversion, filtering, and enhancement.

d. Cross-Platform Compatibility: Python's image I/O libraries are designed to be cross-platform, meaning they can be used
on different operating systems (e.g., Windows, macOS, Linux) without significant modifications.

e. Performance Optimization: Some libraries, such as OpenCV, are optimized for performance, offering efficient algorithms
and data structures for image processing tasks. This optimization enhances the speed and efficiency of handling images,
especially when dealing with large datasets or real-time applications.

18. Create a simple image processing pipeline using python.

a. Load the image: Read an input image from a file ('input_image.jpg') using OpenCV's cv2.imread() function.

b. Convert to grayscale: Convert the loaded color image to grayscale using cv2.cvtColor() function.

c. Apply Gaussian blur: Apply Gaussian smoothing to the grayscale image to reduce noise and remove small details using
cv2.GaussianBlur() function.

d. Perform edge detection: Use the Canny edge detector to detect edges in the blurred image using cv2.Canny() function.

e. Display the images: Display the original and processed images using cv2.imshow() function.

f. Save the processed image: Save the processed image (edges) to a file ('edges_image.jpg') using cv2.imwrite() function.

import cv2

# Step 1: Load the image

image = cv2.imread('input_image.jpg')

# Step 2: Convert the image to grayscale

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

# Step 3: Apply Gaussian blur to the grayscale image

blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)

# Step 4: Perform edge detection using the Canny edge detector

edges = cv2.Canny(blurred_image, threshold1=30, threshold2=100)


# Step 5: Display the original and processed images

cv2.imshow('Original Image', image)

cv2.imshow('Edges', edges)

cv2.waitKey(0)

cv2.destroyAllWindows()

# Step 6: Save the processed image

cv2.imwrite('edges_image.jpg', edges)

19. How do you apply a basic image manipluation like resizing or rotating an image in python?

To apply basic image manipulations like resizing or rotating an image in Python, you can use libraries like OpenCV or PIL
(Python Imaging Library).

from PIL import Image

# Open the image

image = Image.open('input_image.jpg')

# Resize the image to a specific width and height

resized_image = image.resize((new_width, new_height))

# Rotate the image by a specified angle (in degrees)

# Use resample=Image.BICUBIC for high-quality rotation

angle = 45

rotated_image = resized_image.rotate(angle, resample=Image.BICUBIC)

# Display the resized and rotated images

resized_image.show()

rotated_image.show()
20. Describe the process of image formation based on sampling and quantization.

How does it relate to the discreet fourier transform and FFT?

The process of image formation based on sampling and quantization involves converting a continuous image into a digital
representation suitable for storage, transmission, and processing on computers.

a. Sampling:

Sampling refers to the process of capturing discrete samples of an analog signal at regular intervals in space or time. In the
context of image formation, sampling involves selecting discrete points from the continuous spatial domain of the image.

Each sample represents the intensity or color value of the image at a specific location, forming a grid of pixels.

b. Quantization:

Quantization involves mapping the continuous range of pixel intensities (analog values) to a finite set of discrete levels.

Quantization reduces the amount of data required to represent the image, making it suitable for storage and transmission.

c. Relation to the DFT and FFT:

The Discrete Fourier Transform (DFT) and its fast variant, the Fast Fourier Transform (FFT), play crucial roles in
understanding the relationship between image formation, sampling, and quantization.

The DFT allows images to be represented in the frequency domain, where spatial information is transformed into frequency
components.

The DFT can be applied to this discrete sequence to analyze the frequency content of the sampled and quantized image.

The FFT is a computationally efficient algorithm for calculating the DFT, making it practical for processing large amounts of
image data.

22. Explain the concept of image enhancement based on pixel transformation. How does it differ from image smoothening
technique?

a. Image Enhancement Based on Pixel Transformation:

Pixel transformation techniques involve applying mathematical operations or functions to individual pixels or groups of
pixels in an image.

Contrast stretching expands the range of pixel intensities to utilize the full dynamic range of the image, enhancing the
contrast between dark and light areas.

b. Image Smoothing Techniques:

Image smoothening techniques, on the other hand, focus on reducing noise or sharp transitions in an image to create a
more visually pleasing or coherent appearance.

Smoothing operations typically involve applying spatial filters or convolution kernels to blur the image and reduce
high-frequency components.

Common smoothing filters include Gaussian blur, median filter, and bilateral filter.
c. Difference between Image Enhancement and Smoothing:

Image enhancement aims to improve the overall quality, appearance, or interpretability of an image by adjusting its pixel
values based on specific criteria or objectives.

Image smoothening techniques primarily focus on reducing noise or sharp transitions in an image to create a more visually
pleasing or coherent appearance, without necessarily enhancing specific features or qualities of the image.

23. Explain the concept of image derivative using gradient. How do gradients help in detecting edges and other features in
an image.

a. Gradient Calculation:

In image processing, gradients are typically calculated using convolution operations with derivative kernels or filters.

The gradient in the x-direction is computed by convolving the image with a kernel that represents the derivative of the
intensity function with respect to the x-coordinate.

Similarly, the gradient in the y-direction (vertical gradient) is computed using a kernel that represents the derivative with
respect to the y-coordinate.

b. Edge Detection:

Gradients play a crucial role in edge detection by highlighting regions of significant intensity variation or rapid change in
pixel values.

At an edge or boundary between different image regions, the intensity changes abruptly, resulting in high gradient
magnitudes.

Edge detection algorithms use gradient information to locate these abrupt intensity transitions and identify potential edge
pixels.

c. Feature Extraction:

Gradients also help in extracting other image features, such as corners, corners, and texture patterns.

Corners and corners represent points in the image where the gradient magnitude is high in multiple directions, indicating
significant intensity changes.

d. Gradient Properties:

The magnitude of the gradient indicates the steepness or rate of change of intensity at a pixel, with higher magnitudes
corresponding to sharper transitions or edges.

The orientation of the gradient represents the direction of the intensity change, indicating the orientation of edges or
features in the image.

24. How do morphological operations like erosion, dilation, opening and closing help in image enhancement and feature
extraction.
These operations are based on mathematical morphology concepts and involve the manipulation of the binary or grayscale
representations of images.

a. Erosion:

Erosion is a morphological operation that shrinks or erodes the boundaries of objects in an image.

Erosion is useful for removing small objects, isolating individual components, or separating connected objects in an image.

In image enhancement, erosion can help in noise reduction by removing small isolated pixels or thin structures that are
considered noise.

b. Dilation:

Dilation is the opposite of erosion and involves expanding or dilating the boundaries of objects in an image.

Dilation is useful for closing small gaps between objects, joining broken parts of objects, or enlarging objects in an image.

In image enhancement, dilation can help in filling holes, connecting nearby regions, or increasing the prominence of
features.

c. Opening:

Opening is a combination of erosion followed by dilation and is used to remove small objects or smooth object boundaries
in an image.

Opening is effective in eliminating noise, removing small unwanted structures, or separating overlapping objects in an
image.

In image enhancement, opening can help in preprocessing steps to improve the quality of images before further analysis or
feature extraction.

d. Closing:

Closing is the opposite of opening and is used to fill small gaps, bridge narrow breaks, or connect nearby object regions in
an image.

Closing is effective in smoothing object boundaries, joining broken parts of objects, or filling in missing regions in an image.

In image enhancement, closing can help in preprocessing steps to improve object connectivity and feature extraction.

You might also like