[go: up one dir, main page]

0% found this document useful (0 votes)
7 views55 pages

DIP file

The document outlines various image processing techniques using SCILAB, including basic arithmetic operations (addition, subtraction, multiplication, division) and geometric transformations (rotation, scaling, translation). It also discusses Fourier Transform applications for analyzing image frequencies, contrast stretching, histogram equalization, and bit-plane slicing for enhancing image visibility. Each section includes theoretical explanations, practical applications, and SCILAB code implementations.
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)
7 views55 pages

DIP file

The document outlines various image processing techniques using SCILAB, including basic arithmetic operations (addition, subtraction, multiplication, division) and geometric transformations (rotation, scaling, translation). It also discusses Fourier Transform applications for analyzing image frequencies, contrast stretching, histogram equalization, and bit-plane slicing for enhancing image visibility. Each section includes theoretical explanations, practical applications, and SCILAB code implementations.
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
You are on page 1/ 55

EXPERIMENT - 1A

Objective - Introduction to basic arithmetic operations on Images using


SCILAB.

Theory -

Introduction to Image Processing in SCILAB

Image processing is a technique used to manipulate and analyze digital images using
mathematical operations. SCILAB, an open-source computing software, provides various
functions to perform image processing tasks, including arithmetic operations. These operations
are fundamental in modifying image brightness, contrast, and combining images for different
effects.

Basic Arithmetic Operations on Images

Arithmetic operations on images involve pixel-wise manipulation of image intensities. The four
primary operations are:

1.​ Addition
2.​ Subtraction
3.​ Multiplication
4.​ Division

Each of these operations can be applied to either a single image (with a scalar value) or between
two images of the same dimensions.

1. Image Addition

Definition:

Image addition is performed by adding corresponding pixel values of two images or by adding a
constant value to an image.

Purpose and Applications:

●​ Increasing image brightness.


●​ Blending two images.
●​ Image enhancement.

Implementation in SCILAB:
// Read input images
I1 = imread("image1.jpg");
I2 = imread("image2.jpg");

// Convert images to grayscale (if needed)


I1 = rgb2gray(I1);
I2 = rgb2gray(I2);

// Perform addition
I_sum = I1 + I2;

// Display the result


imshow(I_sum);

2. Image Subtraction

Definition:

Image subtraction is performed by subtracting the pixel values of one image from another or by
subtracting a constant from an image.

Purpose and Applications:

●​ Detecting differences between two images.


●​ Motion detection.
●​ Edge detection.

Implementation in SCILAB:
// Read input images
I1 = imread("image1.jpg");
I2 = imread("image2.jpg");

// Convert to grayscale
I1 = rgb2gray(I1);
I2 = rgb2gray(I2);
// Perform subtraction
I_diff = I1 - I2;

// Display the result


imshow(I_diff);

3. Image Multiplication

Definition:

Image multiplication involves multiplying each pixel value by a constant or performing


element-wise multiplication of two images.

Purpose and Applications:

●​ Adjusting image contrast.


●​ Applying masks to images.
●​ Enhancing or suppressing certain features.

Implementation in SCILAB:
// Read input image
I1 = imread("image1.jpg");

// Convert to grayscale
I1 = rgb2gray(I1);

// Multiply by a scalar
I_mul = I1 * 1.5; // Increasing brightness

// Display the result


imshow(I_mul);

4. Image Division

Definition:
Image division is used to normalize image intensity, remove illumination effects, or compare two
images.

Purpose and Applications:

●​ Normalizing images.
●​ Shadow removal.
●​ Image enhancement.

Implementation in SCILAB:
// Read input image
I1 = imread("image1.jpg");

// Convert to grayscale
I1 = rgb2gray(I1);

// Divide by a scalar
I_div = I1 / 2; // Reducing brightness

// Display the result


imshow(I_div);

Viva - Voce :

Q1: What is image addition, and where is it used?​


A: Image addition is the process of adding pixel values of two images or a constant to an image.
It is used in brightness enhancement and blending images.​

Q2: How does image subtraction help in motion detection?​


A: By subtracting two consecutive frames, moving objects appear as differences, making motion
detection possible.​

Q3: Why do we use image multiplication in image processing?​


A: Image multiplication adjusts contrast, applies masks, and enhances specific features in an
image.​
Q4: What is the purpose of image division in image normalization?​
A: Image division helps normalize brightness variations and remove shadows from an image.​

Q5: Why do we convert images to grayscale before performing arithmetic operations?​


A: Grayscale images reduce computational complexity and ensure accurate arithmetic
operations by working with single-channel intensity values.​
EXPERIMENT - 1
Objective - Create a program to demonstrate Geometric transformations-
Image rotation, scaling, and translation.

Theory -

Geometric transformations alter the spatial relationship between pixels in an image. They can be
used for various purposes, such as image registration, object recognition, and creating special
effects. The three fundamental geometric transformations are rotation, scaling, and translation.
These transformations can be represented mathematically using matrices.

1. Rotation:

Rotation involves turning an image around a specific point (usually the center). The angle of
rotation determines the amount of turning. A positive angle indicates counter-clockwise rotation.

●​ Mathematical Representation: For a point (x, y) in the image, the rotated point (x', y') can
be calculated using the following equations:

x' = x * cos(θ) - y * sin(θ)


y' = x * sin(θ) + y * cos(θ)

●​ where θ is the rotation angle in radians.


●​ Matrix Representation: The rotation can also be represented by a 2x2 matrix:

[ x' ] [ cos(θ) -sin(θ) ] [ x ]


[ y' ] = [ sin(θ) cos(θ) ] * [ y ]

●​ Homogeneous Coordinates: For more complex transformations (including translation),


it's often convenient to use homogeneous coordinates. A point (x, y) is represented as (x,
y, 1). The rotation matrix in homogeneous coordinates becomes:

[ x' ] [ cos(θ) -sin(θ) 0 ] [ x ]


[ y' ] = [ sin(θ) cos(θ) 0 ] * [ y ]
[1 ] [ 0 0 1] [1]

2. Scaling:

Scaling changes the size of the image. It can be uniform (same scaling factor in both x and y
directions) or non-uniform (different scaling factors in x and y).
●​ Mathematical Representation:​

x' = x * Sx
y' = y * Sy

●​ where Sx and Sy are the scaling factors in the x and y directions, respectively.
●​ Matrix Representation (Homogeneous Coordinates):

[ x' ] [ Sx 0 0 ] [ x ]
[ y' ] = [ 0 Sy 0 ] * [ y ]
[1 ] [ 0 0 1] [1]

3. Translation:

Translation moves the image from one location to another.

●​ Mathematical Representation:

x' = x + Tx
y' = y + Ty

●​ where Tx and Ty are the translation amounts in the x and y directions, respectively.
●​ Matrix Representation (Homogeneous Coordinates):

[ x' ] [ 1 0 Tx ] [ x ]
[ y' ] = [ 0 1 Ty ] * [ y ]
[1 ] [0 0 1 ] [1]
Scilab Code -

// Load an image
img = imread("C:\Users\dhruv\Downloads\gpt interest.jpg");
imshow(img);
title("Original Image");

[rows, cols, channels] = size(img);

// ---------------- Rotation ----------------


angle = 45;
rotated_img = imrotate(img, angle, "bilinear");
figure(); imshow(rotated_img);
title("Rotated Image (45 degrees)");

// ---------------- Scaling ----------------


scale_factor = 1.5;
scaled_img = imresize(img, scale_factor, "bilinear");
figure();
imshow(scaled_img);
title(sprintf("Scaled Image (%.1fx)", scale_factor));

// ---------------- Translation ----------------


shift_x = 50;
shift_y = 30;
translation_matrix = [1, 0, shift_x; 0, 1, shift_y; 0, 0, 1];
translated_img = imtransform(img, translation_matrix, ‘affine’);
figure();
imshow(translated_img);
title("Translated Image (50px right, 30px down)");
OUTPUT -
VIVA - VOCE

1.​ Q: What's the purpose of image warping?​


A: Image warping geometrically transforms an image, often to correct
distortions, align images, or create special effects.​

2.​ Q: How do you represent a 2D point for transformations in homogeneous


coordinates?​
A: A 2D point (x, y) is represented as (x, y, 1) in homogeneous coordinates.​

3.​ Q: What's the key difference between affine and projective transformations?​
A: Affine transformations preserve parallel lines, while projective
transformations do not.​

4.​ Q: Why is interpolation necessary during image transformations?​


A: Interpolation fills in pixel values at non-integer coordinates after
transformation, preventing holes or artifacts in the image.​

5.​ Q: What's the effect of a shear transformation on an image?​


A: A shear transformation distorts the image by slanting or skewing it along
one or both axes.
EXPERIMENT - 2
Objective - Display of FFT (1-D & 2-D) of an image and apply Two-dimensional
Fourier transform to represent the content of an image using the discrete Fourier
transform (DFT) and masking with DFT.

Theory - ​
The Fourier Transform is a mathematical tool used to transform a function from the spatial
domain to the frequency domain. It decomposes an image into its sine and cosine components.
The Fast Fourier Transform (FFT) is an efficient algorithm to compute the Discrete Fourier
Transform (DFT) of an image.

1. 1D Fourier Transform (FFT)

The 1D Fourier Transform converts a 1D signal (such as a row or column of an image) from the
spatial domain into the frequency domain.

Mathematical Representation of 1D DFT: where:

●​ is the input signal,


●​ is the transformed signal in the frequency domain,
●​ is the number of points in the signal.

2. 2D Fourier Transform (FFT)

The 2D Fourier Transform extends the 1D DFT to two dimensions, used to analyze image
frequencies.

Mathematical Representation of 2D DFT: where:

●​ is the input image,


●​ is the transformed image in the frequency domain,
●​ are the dimensions of the image.

3. Masking in the Frequency Domain

●​ Low-pass Filtering (LPF): Retains low-frequency components, blurring the image.


●​ High-pass Filtering (HPF): Removes low-frequency components, enhancing edges.
●​ Band-pass Filtering: Keeps only selected frequency bands.
Applying a mask in the frequency domain allows enhancement or suppression of specific
frequency components before applying the inverse FFT to reconstruct the modified image.

Scilab Code -

// Load an image

img = imread("C:\\Users\\dhruv\\Downloads\\image.jpg");

gray_img = rgb2gray(img);

imshow(gray_img);

title("Original Image");

// ---------------- 1D FFT ----------------

gray_img = double(gray_img);

row = gray_img(100, :); // Select a row from the image

fft_row = fft(row); // Compute 1D FFT

fft_shifted = fftshift(fft_row);

figure();

plot(abs(fft_shifted));

xlabel("Frequency");

ylabel("Magnitude");

title("1D FFT of a Row");

// ---------------- 2D FFT ----------------

fft_img = fft2(gray_img);
fft_shifted = fftshift(fft_img);

magnitude_spectrum = log(1 + abs(fft_shifted)); // Log scaling


for better visualization

magnitude_spectrum = magnitude_spectrum -
min(magnitude_spectrum); // Normalize to [0,1]

magnitude_spectrum = magnitude_spectrum /
max(magnitude_spectrum);

figure();

imshow(magnitude_spectrum); // Properly normalized image

title("2D FFT (Magnitude Spectrum)");

// ---------------- Masking with DFT ----------------

// ---------------- Circular Low-Pass Filter ----------------

// Define cutoff frequency (radius of the circle)

cutoff = min(rows, cols) / 6;

H = zeros(rows, cols);

for x = 1:rows

​ for y = 1:cols

​ dist = sqrt((x - cx)^2 + (y - cy)^2);

​ if dist < cutoff then

​ H(x, y) = 1;

​ else

​ H(x, y) = 0;
​ end

​ end

end

filtered_fft = fft_shifted .* H;

filtered_fft = ifftshift(filtered_fft); // Shift back

filtered_img = real(ifft(ifft(filtered_fft, 1), 2));

filtered_img = filtered_img - min(filtered_img);

filtered_img = filtered_img / max(filtered_img);

// ---------------- Display Results ----------------

figure();

subplot(1,3,1);

imshow(gray_img / max(gray_img)); // Normalize original image

title("Original Image");

subplot(1,3,2);

imshow(H); // Show the circular low-pass filter

title("Circular Low-Pass Filter");

subplot(1,3,3);

imshow(filtered_img);

title("Filtered Image (Low-Pass)");


OUTPUT -


VIVA-VOCE -

Q: What is the primary purpose of applying the Fourier Transform to an image?

A: The primary purpose is to decompose the image into its constituent frequencies, allowing for
analysis and manipulation of these frequencies in the frequency domain.

Q: In the provided Scilab code, what kind of input is given to the 1D FFT function (fft)?

A: A single row of the grayscale image, specifically the 100th row, is given as input to the 1D
FFT function.

Q: What does the 2D Fourier Transform (fft2) achieve when applied to an image in the Scilab
code?

A: The 2D FFT transforms the entire grayscale image from the spatial domain into the 2D
frequency domain, representing the amplitude and phase of different spatial frequencies present
in the image.

Q: In the frequency domain representation of an image, what do the values at different


frequencies generally correspond to in the spatial domain?

A: Low frequencies correspond to slow changes in intensity across the image (like smooth
areas), while high frequencies correspond to rapid changes in intensity (like edges and fine
details).

Q: What is the purpose of the fftshift function used after calculating the 2D FFT in the Scilab
code?

A: The fftshift function rearranges the frequency domain representation so that the
zero-frequency component (DC component) is located at the center of the spectrum, making it
easier to visualize.
EXPERIMENT - 3
Objective - Write a Program of Contrast stretching of a low contrast image,
Histogram, and Histogram Equalization and Display of bit planes of an Image

Theory -

1. Contrast Stretching:

Contrast stretching is a simple image enhancement technique. It involves


expanding the range of pixel values in an image. If an image has a narrow range of
pixel values, contrast stretching enhances its visibility by transforming its pixel
intensity values to cover a wider range.

Formula: The transformation function for contrast stretching is given by:

S=(I−Imin)(Imax−Imin)×(L−1)S = \frac{(I - I_{min})}{(I_{max} - I_{min})}


\times (L - 1)S=(Imax​−Imin​)(I−Imin​)​×(L−1)

Where:

●​ SSS is the stretched intensity,


●​ III is the original intensity,
●​ IminI_{min}Imin​and ImaxI_{max}Imax​are the minimum and maximum
intensities of the original image,
●​ LLL is the maximum possible intensity value (255 for an 8-bit image).

2. Histogram Calculation:

A histogram represents the distribution of pixel intensity values in an image. The


x-axis of the histogram shows the pixel intensity levels, and the y-axis shows the
frequency of each intensity value in the image.

By plotting the histogram, you can analyze the brightness distribution of the image
and determine whether it needs enhancement, like in the case of contrast stretching
or equalization.

3. Histogram Equalization:
Histogram equalization is an image enhancement technique used to improve the
contrast of an image. It works by redistributing the pixel values in such a way that
the resulting histogram is approximately uniform.

This is done by calculating the cumulative distribution function (CDF) of the pixel
intensities and then mapping the original pixel values to new values based on the
CDF.

Steps:

●​ Compute the histogram of the image.


●​ Compute the cumulative distribution function (CDF) from the histogram.
●​ Map the intensity values using the CDF to obtain an enhanced image.

4. Bit-Plane Slicing:

Bit-plane slicing is a technique used to extract individual bits (from 0 to 7) of the


image. In an 8-bit image, the pixel values are represented using 8 bits, and each bit
represents a different level of detail. By isolating each bit plane, we can visualize
the contribution of individual bits to the overall image.

The process of bit-plane slicing involves:

●​ Extracting each bit (from the least significant bit to the most significant bit).
●​ Visualizing the bit planes to understand how each bit contributes to the
image's overall intensity.

Each bit plane will look like a binary image, where the pixels in the plane will be
either black (0) or white (255), depending on the bit value at that position.

Scilib code –

// Load the image

img = imread('low_contrast_image.jpg'); // Reading the image

img = rgb2gray(img); // Convert to grayscale if the image is in color


img = double(img); // Convert to double for processing

// Contrast Stretching

min_intensity = min(img(:));

max_intensity = max(img(:));

stretched_img = (img - min_intensity) * (255 / (max_intensity -


min_intensity)); // Contrast Stretching

stretched_img = uint8(stretched_img); // Convert to uint8 for image display

// ------------------------Display original and contrast-stretched


images----------------

figure();

subplot(1,2,1);

imshow(uint8(img)); // Original Image

title("Original Image");

subplot(1,2,2);

imshow(stretched_img); // Contrast Stretched Image

title("Contrast Stretched Image");

// --------------------------------Histogram
Calculation----------------------------------------
figure();

subplot(1,2,1);

histplot(256, img(:)); // Histogram of Original Image

title("Histogram of Original Image");

subplot(1,2,2);

histplot(256, stretched_img(:)); // Histogram of Stretched Image

title("Histogram of Stretched Image");

// -----------------------Histogram
Equalization----------------------------------------------------

equalized_img = histeq(uint8(img)); // Perform histogram equalization

figure();

subplot(1,2,1);

imshow(equalized_img); // Display Equalized Image

title("Histogram Equalized Image");

subplot(1,2,2);

histplot(256, equalized_img(:)); // Histogram of Equalized Image

title("Histogram of Equalized Image");


// ------------------------- Display Bit
Planes---------------------------------------------------

figure();

for i = 0:7 // Loop through all 8 bits

​ bit_plane = bitand(uint8(img), 2^i); // Extract the ith bit plane

​ subplot(2,4,i+1);

​ imshow(bit_plane * 255); // Display the ith bit plane (scaled for


visibility)

​ title("Bit Plane " + string(i));

OUTPUT :
VIVA - VOCE :

Q1: What is contrast stretching?

A1:​
Contrast stretching is an image enhancement technique that expands the pixel intensity range of
an image to improve visibility, especially for images with low contrast. It stretches pixel values
to cover the full range (0-255 for 8-bit images).

Q2: How does histogram equalization improve image quality?

A2:​
Histogram equalization redistributes the pixel intensities of an image so that the histogram
becomes uniform, enhancing the contrast. It reveals hidden details, especially in images with
poor contrast.

Q3: What is the purpose of bit-plane slicing?

A3:​
Bit-plane slicing extracts and displays the individual bit planes (from 0 to 7) of an image. It
helps visualize the contribution of each bit in representing the image's overall intensity.
Q4: Why do we convert an image to grayscale for contrast stretching and histogram
equalization?

A4:​
Converting an image to grayscale simplifies processing because it reduces the complexity of
color channels (RGB) to just one intensity channel, making operations like contrast stretching
and histogram equalization more efficient.

Q5: What does the histogram of an image represent?

A5:

A histogram represents the distribution of pixel intensity values in an image. It shows how many
pixels fall into each intensity level, helping analyze the brightness and contrast of the image.


EXPERIMENT - 4
Objective - Computation of Mean, Standard Deviation, Correlation
coefficient of the given Image

Theory -

1.​ Mean of an image : The mean of an image represents the average intensity
value of all pixels. It gives an overall brightness measure.

For a grayscale image with dimensions M×N, the mean is calculated as:

Where:

●​ I(i,j) is the intensity of the pixel at position (i,j).


●​ M and N are the image dimensions.
2.​ Standard Deviation of an Image : Standard deviation measures how much
pixel intensities deviate from the mean, indicating image contrast.

Mathematically :

Where:

●​ μ is the mean intensity.


●​ I(i,j) is the intensity of the pixel at (i,j).
3.​ Correlation Coefficient (For Color Images) : The correlation coefficient
measures the relationship between different color channels (Red, Green,
Blue) of an image.
For two pixel intensity arrays X and Y:

Where:

●​ μX and μY​are the means of channels X and Y.


●​ Xi and Yi are pixel intensities.

The correlation coefficient ranges between -1 to 1:

●​ 1 → Perfectly correlated (bright and dark areas match across


channels).
●​ 0 → No correlation.
●​ -1 → Perfect negative correlation.

Code :

img = imread("lena.png");

if size(img, 3) == 3 then

R = img(:,:,1);

G = img(:,:,2);

B = img(:,:,3);

gray_img = rgb2gray(img);

else

gray_img = img;

end
mean_val = mean(gray_img(:));

std_val = stdev(gray_img(:));

disp("Mean of the grayscale image: " + string(mean_val));

disp("Standard Deviation of the grayscale image: " + string(std_val));

if size(img, 3) == 3 then

correlation_matrix = corr([R(:), G(:), B(:)]);

disp("Correlation Coefficient Matrix:");

disp(correlation_matrix);

end

imshow(img);

title("Lena Image");

Output :
Viva Voce :

_____________________________________________________________________________

Q1. What is the significance of computing the mean of an image?

Ans: The mean represents the average pixel intensity of an image. It helps in
understanding the overall brightness of the image.

●​ A high mean (closer to 255) indicates a brighter image.


●​ A low mean (closer to 0) indicates a darker image.

_____________________________________________________________________________

Q2. Why do we calculate the standard deviation of an image?

Ans: The standard deviation measures how much the pixel intensities vary from
the mean. It helps in understanding the contrast of an image.
●​ A higher standard deviation means greater contrast (sharp edges and more
variations).
●​ A lower standard deviation means less contrast (flat or smooth regions).

_____________________________________________________________________________

Q3. What is the purpose of computing the correlation coefficient in a color image?

Ans: In RGB images, different color channels (Red, Green, Blue) may have a
relationship with each other.​
The correlation coefficient tells how strongly these color channels are related:

●​ 1.0 → Perfect correlation (Channels behave similarly).


●​ 0.0 → No correlation (Independent channels).
●​ -1.0 → Perfect negative correlation (Channels behave oppositely)

_____________________________________________________________________________

Q4. What are the practical applications of mean, standard deviation, and
correlation in image processing?

Ans: These statistical properties are widely used in computer vision and image
analysis:

●​ Mean:
○​ Used for brightness adjustment in images.
○​ Helps in thresholding for image segmentation.
●​ Standard Deviation:
○​ Used in edge detection (high contrast areas have high standard
deviation).
○​ Helps in image texture analysis.
●​ Correlation Coefficient:
○​ Helps in color correction and image fusion.
○​ Used in feature extraction for pattern recognition.

_____________________________________________________________________________
Q5. Explain the outputs of Mean, Standard Deviation, and Correlation Coefficient
for the Lena image.

Ans:

●​ Mean of the Image : A value of ~127.5 (close to the midpoint of 0-255)


suggests that Lena's image is well-balanced, neither too dark nor too bright.
●​ Standard Deviation : A value of ~73.8 means there is a good range of
intensity values, indicating that the image has sharp edges and detailed
textures.
●​ Correlation Coefficient Matrix : Values between 0.89 to 0.95 indicate a
strong correlation between the color channels, meaning the intensity patterns
in Red, Green, and Blue channels are quite similar.

_____________________________________________________________________________
EXPERIMENT - 5
Objective - Implementation of Image Smoothing Filters (Mean and Median
filtering of an Image)

Theory -

1. Introduction to Image Smoothing

Image smoothing is a technique used in digital image processing to reduce noise


and unwanted details in an image. It enhances image quality by averaging pixel
values or replacing them with statistical values from neighboring pixels. Two
common filtering techniques for image smoothing are Mean Filtering and Median
Filtering.

2. Mean Filtering

Mean filtering is a linear filter used for noise reduction by averaging the pixel
values in a defined kernel (filter window). It replaces each pixel value with the
mean of its neighboring pixels, thereby reducing sharp variations.

Characteristics:

•Blurs the image.

•Reduces noise but also removes fine details.

•Suitable for images with Gaussian matrix

3. Median Filtering
Median filtering is a non-linear filter that replaces each pixel with the median value
of the pixels in the neighborhood. It is highly effective in removing
salt-and-pepper.

Characteristics:

•Removes impulse noise while preserving edges.

•Does not blur the image significantly.

•Suitable for images with salt-and-pepper noise.

Example Kernel (3×3 Window Calculation):

Original pixels in the window: [12, 15, 14, 17, 30, 25, 10, 20, 18]

Sorted values: [10, 12, 14, 15, 17, 18, 20, 25, 30]

Median value: 17 (New pixel value)

4. Implementation in Python

Median Filters

I=imread('nuron.jpg');

K = rgb2gray(I);

J= imnoise(K ,'salt & pepper',0.05);

f= medfilt2(J,[3,3]);

f1=medfilt2(J,[10,10]);

subplot(3,2,1); imshow(I); title('Original Image');


subplot(3,2,2); imshow(K); title('Gray Image');

subplot(3,2,3); imshow(J); title('Noise added Image');

subplot(3,2,4); imshow(f); title('3x3 Image');

subplot(3,2,5); imshow(f1); title('10x10 Image');

%Mean Filter and Average Filter

figure;

i=imread('nuron.jpg');

g=rgb2gray(i);

g1=fspecial('average',[3 3]);

b1 = imfilter(g,g1);

subplot(2,2,1); imshow(i); title('Original Image');

subplot(2,2,2); imshow(g); title('Gray Image');

subplot(2,2,3); imshow(b1); title('3x3 Image');

g2= fspecial('average',[10 10]);

b2=imfilter(g,g2);

subplot(2,2,4); imshow(b2); title('10x10 Image');

%Implementation of filter using Convolution

figure;

I= imread('earcell.jpg');

I=I(:,:,1); subplot(2,2,1); imshow(I); title('Original Image');

a=[0.001 0.001 0.001; 0.001 0.001 0.001; 0.001 0.001 0.001];


R=conv2(a,I);

subplot(2,2,2); imshow(R); title('Filtered Image');

b=[0.005 0.005 0.005; 0.005 0.005 0.005; 0.005 0.005 0.005];

R1=conv2(b,I);

subplot(2,2,3); imshow(R1); title('Filtered Image 2');


Output:
Viva-Voice questions

Q1.What is image smoothing?

•Image smoothing is a technique used to reduce noise and detail in an image to


enhance quality.

Q2.What are the differences between Mean and Median filtering?

•Mean filtering blurs the image and is good for Gaussian noise, whereas median
filtering preserves edges and is better for salt-and-pepper noise.

Q3.What is the kernel in filtering?

•A kernel is a small matrix used for convolution operations to modify pixel values
in an image.

Q4.What happens when we increase the filter size?

•A larger filter size results in more smoothing, but too large a filter can excessively
blur the image.

Q5.Why is the median filter non-linear?

•Because it sorts the pixel values and selects the median instead of applying a
linear mathematical operation.
EXPERIMENT - 6
Objective - Implementation of image sharpening filters and Edge
Detection using Gradient Filters.

Theory -

1. Image Sharpening Filters

●​ Image sharpening focuses on improving the clarity of edges and details in an


image.
●​ High-pass filters are commonly used for sharpening. These emphasize
high-frequency components (edges and fine details) while suppressing
low-frequency areas (smooth regions).
●​ Examples of sharpening techniques include:
○​ Unsharp masking: Enhances contrast at edges.
○​ Laplacian filtering: A second-order derivative filter that highlights
edges.

2. Edge Detection Using Gradient Filters

●​ Edge detection identifies points in an image where intensity changes


significantly, often marking boundaries of objects.
●​ Gradient-based methods use derivatives to measure the rate of intensity
change.
●​ Popular edge detection techniques include:
○​ Sobel operator: Uses convolutional kernels to calculate gradients in
the x and y directions.
○​ Prewitt operator: Similar to Sobel but with a simpler kernel
configuration.
○​ Canny edge detection: Combines gradient calculation,
non-maximum suppression, and thresholding for optimal edge
detection.
i=imread('cancercell.jpg');
subplot(4,2,1); imshow(i);
title('Original Image');

g=rgb2gray(i);
subplot(4,2,2); imshow(g); title('Gray Image');

f=fspecial('laplacian',0.05);
im=imfilter(g,f);
subplot(4,2,3); imshow(im); title('Laplacian ');

s=edge(g, 'sobel');
subplot(4,2,4); imshow(s); title('Sobel');

p=edge(g, 'prewitt');
subplot(4,2,5); imshow(p); title('Prewitt');

r=edge(g, 'roberts');
subplot(4,2,6); imshow(r); title('Roberts');

[BW,thresh,gv,gh]=edge(g,'sobel',[],'horizontal');
[BW1,thresh1,gv1,gh1]=edge(g,'sobel',[],'vertical');

subplot(4,2,7); imshow(BW); title('Sobel


Horizontal'); subplot(4,2,8);
imshow(BW); title('Sobel Vertical');
OUTPUT -
Viva-Voice questions

1. What is Image Sharpening? Why is it important?

Image sharpening is a technique used in image processing to enhance the edges and fine
details of an image. It increases the contrast at edges, making the image appear clearer
and crisper. It is important for:

●​ Enhancing image quality in medical imaging, satellite images, and digital


photography.
●​ Improving visual perception by highlighting important details.

2.What are Gradient Filters in Edge Detection?

Gradient filters are used in image processing to detect the intensity changes (edges) by
calculating the gradient of pixel values.

●​ They measure the rate of change of pixel intensity at each point in the image.
●​ High gradient values indicate sharp intensity changes (edges).

3.What is the difference between image sharpening and image smoothing?

Image Sharpening: Enhances the edges and fine details by increasing the contrast
between adjacent pixels. It highlights features.​
Image Smoothing: Reduces noise and smoothens the image by averaging pixel values,
making the image blurry.

4.What is the Laplacian Filter? How is it different from Gradient Filters?

The Laplacian filter is a second-order derivative filter used for edge detection.​
It detects rapid changes in intensity and highlights regions of rapid intensity change.

5.What are the applications of Edge Detection in real-world scenarios?

Medical Imaging: Detecting boundaries of tumors or organs.​


Computer Vision: Identifying objects, shapes, and features.​
Robotics: Autonomous navigation using edge detection for path recognition.​
Facial Recognition: Detecting facial boundaries.​
Text Recognition: Identifying character edges in Optical Character Recognition (OCR)
EXPERIMENT - 7
Objective - Implementation of Image Compression by DCT, DPCM,
HUFFMAN coding.

Theory -
Image compression is a crucial aspect of image processing that reduces the amount of
data required to represent an image. The combination of Discrete Cosine Transform
(DCT), Differential Pulse Code Modulation (DPCM), and Huffman coding is a powerful
approach to achieve efficient image compression. Below is a high-level overview and a
basic implementation of these techniques in Python.

Overview of Techniques

1.​ Discrete Cosine Transform (DCT):


●​ DCT transforms the image from the spatial domain to the frequency
domain. It separates the image into parts of differing importance
(low-frequency and high-frequency components).
●​ The low-frequency components are more significant for human perception,
while high-frequency components can often be discarded or quantized more
aggressively.
2.​ Differential Pulse Code Modulation (DPCM):
●​ DPCM encodes the difference between the current pixel and a predicted
pixel value (usually the previous pixel). This reduces the amount of data
needed to represent the image.
3.​ Huffman Coding:
●​ Huffman coding is a lossless data compression algorithm that assigns
variable-length codes to input characters, with shorter codes assigned to
more frequent characters. This is used to compress the quantized DCT
coefficients.

Scilab Implementation
1.​ Load the Image: Read the image and convert it to grayscale.
2.​ Apply DCT: Perform DCT on the image.
3.​ Quantization: Quantize the DCT coefficients.
4.​ DPCM: Apply DPCM to the quantized coefficients.
5.​ Huffman Coding: Encode the DPCM output using Huffman coding.

CODE:-

// Image Compression in Scilab using DCT, DPCM, and Huffman Coding

// Load Image and Convert to Grayscale

img = imread('image.jpg');

img_gray = rgb2gray(img);

img_gray = double(img_gray);

// Define DCT Function

function B = dct2d(A)

B = dct(A, -1);

B = dct(B', -1)';

endfunction

// Define Inverse DCT Function


function A = idct2d(B)

A = idct(B, -1);

A = idct(A', -1)';

endfunction

// Perform DCT on 8x8 Blocks

block_size = 8;

[m, n] = size(img_gray);

DCT_img = zeros(m, n);

for i = 1:block_size:m

for j = 1:block_size:n

block = img_gray(i:i+block_size-1, j:j+block_size-1);

DCT_img(i:i+block_size-1, j:j+block_size-1) = dct2d(block);

end

end

// Quantization Matrix (JPEG Standard)


Q = [16 11 10 16 24 40 51 61;

12 12 14 19 26 58 60 55;

14 13 16 24 40 57 69 56;

14 17 22 29 51 87 80 62;

18 22 37 56 68 109 103 77;

24 35 55 64 81 104 113 92;

49 64 78 87 103 121 120 101;

72 92 95 98 112 100 103 99];

// Quantization

Quantized_DCT = round(DCT_img ./ Q);

// Apply DPCM on DC coefficients

DC_values = Quantized_DCT(1:block_size:end, 1:block_size:end);

DPCM_DC = diff([0; DC_values(:)]);

// Huffman Encoding for DPCM DC Coefficients

[code, dict] = huffEnc(DPCM_DC);


// Huffman Decoding (Decompression)

DPCM_DC_decoded = huffDec(code, dict);

// Reconstruct DC Coefficients

DC_reconstructed = cumsum(DPCM_DC_decoded);

Quantized_DCT(1:block_size:end, 1:block_size:end) = DC_reconstructed;

// Dequantization

Dequantized_DCT = Quantized_DCT .* Q;

// Apply Inverse DCT to Reconstruct Image

Reconstructed_img = zeros(m, n);

for i = 1:block_size:m

for j = 1:block_size:n

block = Dequantized_DCT(i:i+block_size-1, j:j+block_size-1);

Reconstructed_img(i:i+block_size-1, j:j+block_size-1) = idct2d(block);


end

end

// Convert to Integer and Display

Reconstructed_img = uint8(Reconstructed_img);

figure;

imshow(Reconstructed_img);

title('Reconstructed Image');

OUTPUT:-
Viva-Voice questions

1. What is Image Compression? What is the need of image compression?


To store these images, and make them available over network (e.g. the internet),
compression techniques are needed. Image compression addresses the problem of
reducing the amount of data required to represent a digital image. The underlying basis of
the reduction process is the removal of redundant data.

2. What are different types of compression?


Compression can be divided into two categories, as Lossless and Lossy compression. In
lossless compression, the reconstructed image after compression is numerically identical
to the original image. In lossy compression scheme, the reconstructed image contains
degradation relative to the original.
1 Lossless coding techniques a. Run length encoding b. Huffman encoding c. Arithmetic
encoding d. Entropy coding e. Area coding
2 Lossy coding techniques a. Predictive coding b. Transform coding (FT/DCT/Wavelets)

3. Why DCT is used in image compression?


The DCT can be used to convert the signal (spatial information) into numeric data
("frequency" or "spectral" information) so that the image's information exists in a
quantitative form that can be manipulated for compression.

4. What is the basic principle of Huffman coding?


The main idea in Huffman coding is to assign each character with a variable length code.
The length of each character is decided on the basis of its occurrence frequency. The
character that occurred most time would have the smallest code and the character that
occurred least in the data would have the largest code.

5. What is the use of differential pulse code modulation DPCM for compression?

DPCM is a predictive coding technique used to encode analog signals digitally by


sampling the signal predicting the next sample value based on previous samples,
quantizing the difference between the actual and predicted values, and transmitting the
quantized difference. In DPCM, the audio signal can be predicted from previous
samples. Delta modulation (DM) is a modification of DPCM.
Experiment - 8
Objective - Implementation of Image restoration Techniques.
Theory -
Image restoration is a technique used to recover an original image that has been
degraded due to factors like noise, blur, or distortion. It aims to reconstruct or
enhance an image by removing unwanted effects.

Common Image Degradations

1.​ Noise – Caused by electronic interference, sensor limitations, or


transmission errors.
2.​ Blur – Due to motion, defocus, or optical system imperfections.
3.​ Loss of Resolution – Caused by compression, downsampling, or distortions.

Image Restoration Methods

1.​ Inverse Filtering – Attempts to reverse the degradation function.


2.​ Wiener Filtering – A deconvolution technique that balances inverse
filtering and noise reduction.
3.​ Median Filtering – Removes noise while preserving edges.
4.​ Least Squares Filtering – Optimizes the restoration by
minimizing error.
A. Noise Removal using Median Filtering
clc;
clear;
close;

// Load SIP module


exec('SCI/modules/vision/macros/load.sci', -1);

// Read the degraded image


img = imread('noisy_image.jpg');

// Convert to grayscale if needed


img_gray = rgb2gray(img);

// Apply Median Filtering


restored_img = medfilt2(img_gray, [3 3]);

// Display results
subplot(1, 2, 1);
imshow(img_gray);
title("Noisy Image");

subplot(1, 2, 2);
imshow(restored_img);
title("Restored Image");

Output →
Input image Restored Image

B. Image Deblurring using Wiener Filtering

clc;
clear;
close;
// Load an image
img = imread('blurry_image.jpg');

// Convert to grayscale if needed


img_gray = rgb2gray(img);

// Apply Wiener Filter


restored_img = wiener2d(img_gray, [5 5]);

// Display the results


subplot(1, 2, 1);
imshow(img_gray);
title("Blurred Image");

subplot(1, 2, 2);
imshow(restored_img);
title("Restored Image");

C. Inverse Filtering for motion Blur.


clc;
clear;
close;

// Read a blurred image


img = imread('motion_blur.jpg');

// Convert to grayscale
img_gray = rgb2gray(img);

// Define a motion blur kernel (PSF)


h = fspecial('motion', 15, 45);

// Apply inverse filtering


restored_img = deconvblind(img_gray, h, 10);

// Display the results


subplot(1, 2, 1);
imshow(img_gray);
title("Motion Blurred Image");

subplot(1, 2, 2);
imshow(restored_img);
title("Restored Image");

Output →
Viva Voice Questions
1.​ What is Image Restoration, and how does it differ from Image
Enhancement?​
Expected Answer: Image restoration aims to recover an image that has been
degraded due to noise, blur, or distortions using mathematical models. Image
enhancement, on the other hand, focuses on improving image appearance
without considering the degradation model.​

2.​ Explain the role of the Wiener filter (wiener2d) in image restoration.​
Expected Answer: The Wiener filter is used to remove noise and blur by
estimating the original image based on statistical properties. It minimizes the
mean square error between the restored and original images.​

3.​ What is the function of medfilt2 in Scilab, and when is it used?​


Expected Answer: medfilt2 applies a median filter to remove
salt-and-pepper noise from an image. It replaces each pixel with the median
value of its surrounding neighborhood, preserving edges better than a mean
filter.​

4.​ How does the fspecial function help in image restoration?​


Expected Answer: fspecial is used to create various types of filters
(Gaussian, motion blur, etc.), which are then applied to images for
deblurring or noise removal. It helps generate convolution kernels for image
processing tasks.
5.​ What are the common types of noise that affect digital images, and how
can they be removed?
Expected Answer: Common types of noise include Gaussian noise,
salt-and-pepper noise, and Poisson noise. They can be removed using filters
like the Wiener filter (for Gaussian noise), median filter (medfilt2 for
salt-and-pepper noise), and adaptive filters for different noise types.
Experiment - 9

Objective- Implementation of Image Intensity slicing technique for image


enhancement.

Theory-
Image Intensity Slicing is a technique used in image enhancement to highlight
specific intensity ranges within an image. This method is particularly useful for
medical imaging, remote sensing, and pattern recognition, where certain intensity
values need to be emphasized.

There are two common approaches to intensity slicing:

1. Binary Intensity Slicing

• Pixels within a specified intensity range are set to a high intensity (white), while
all others are set to a low intensity (black).

• Useful for segmentation tasks, where specific regions need to be extracted.

2. Gray-Level Intensity Slicing

• Instead of converting pixels to only two levels, the pixels within the specified
range are enhanced while maintaining the rest of the image.

• This method enhances certain features while preserving overall image details.

Characteristics-
• Helps in emphasizing important details within an image.

• Useful for detecting objects with specific intensity values.

• Binary slicing is effective for segmentation, while gray-level slicing enhances


visibility without losing details.

• Computationally simple and widely used in medical and satellite imaging.


This technique is an effective way to enhance specific regions in an image, making
it easier for analysis and interpretation.

Implementation in Scilab -

i=imread('earcell.jpg');
subplot(3,2,1);imshow(i); title(‘Original Image’);
l=im2double(i);
level=graythresh(l);
BW = im2bw(l,level);
subplot(3,2,2); imshow(BW); title('Image graythresh');
level1=0.2*BW;
subplot(3,2,3); imshow(level1); title('0.2 Slice');
level2=0.4*BW;
subplot(3,2,4); imshow(level2);title('0.4 Slice');
level3=0.6*BW;
subplot(3,2,5); imshow(level3);title('0.6 Slice');
level4=0.8*BW;
subplot(3,2,6); imshow(level4); title('0.8 Slice');
Output-

Viva-voice questions

Q1: What is Image Intensity Slicing?

Ans: It is an image enhancement technique that highlights specific intensity ranges


to enhance important details in an image.

Q2: What are the two types of intensity slicing?

Ans: The two types are Binary Intensity Slicing (where selected intensity ranges
are set to a fixed value) and Gray-Level Intensity Slicing (where selected
intensity ranges are enhanced without losing other details).
Q3: Where is intensity slicing commonly used?

Ans: It is used in medical imaging, remote sensing, and pattern recognition to


highlight specific structures or objects.

Q4: How does Binary Intensity Slicing differ from Gray-Level Intensity
Slicing?

Ans: In Binary Intensity Slicing, pixels inside the range are set to a fixed value
(e.g., white), while others are set to another fixed value (e.g., black). In
Gray-Level Slicing, pixels inside the range are enhanced, but the rest of the image
is still visible.

Q5: What is the main advantage of Image Intensity Slicing?

Ans: It helps in enhancing important details within an image, making object


detection and analysis easier.

You might also like