DIP file
DIP file
Theory -
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.
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.
Implementation in SCILAB:
// Read input images
I1 = imread("image1.jpg");
I2 = imread("image2.jpg");
// Perform addition
I_sum = I1 + I2;
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.
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;
3. Image Multiplication
Definition:
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
4. Image Division
Definition:
Image division is used to normalize image intensity, remove illumination effects, or compare two
images.
● 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
Viva - Voce :
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:
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:
● 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");
3. Q: What's the key difference between affine and projective transformations?
A: Affine transformations preserve parallel lines, while projective
transformations do not.
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.
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.
The 2D Fourier Transform extends the 1D DFT to two dimensions, used to analyze image
frequencies.
Scilab Code -
// Load an image
img = imread("C:\\Users\\dhruv\\Downloads\\image.jpg");
gray_img = rgb2gray(img);
imshow(gray_img);
title("Original Image");
gray_img = double(gray_img);
fft_shifted = fftshift(fft_row);
figure();
plot(abs(fft_shifted));
xlabel("Frequency");
ylabel("Magnitude");
fft_img = fft2(gray_img);
fft_shifted = fftshift(fft_img);
magnitude_spectrum = magnitude_spectrum -
min(magnitude_spectrum); // Normalize to [0,1]
magnitude_spectrum = magnitude_spectrum /
max(magnitude_spectrum);
figure();
H = zeros(rows, cols);
for x = 1:rows
for y = 1:cols
H(x, y) = 1;
else
H(x, y) = 0;
end
end
end
filtered_fft = fft_shifted .* H;
figure();
subplot(1,3,1);
title("Original Image");
subplot(1,3,2);
subplot(1,3,3);
imshow(filtered_img);
VIVA-VOCE -
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.
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:
Where:
2. Histogram Calculation:
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:
4. Bit-Plane Slicing:
● 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 –
// Contrast Stretching
min_intensity = min(img(:));
max_intensity = max(img(:));
figure();
subplot(1,2,1);
title("Original Image");
subplot(1,2,2);
// --------------------------------Histogram
Calculation----------------------------------------
figure();
subplot(1,2,1);
subplot(1,2,2);
// -----------------------Histogram
Equalization----------------------------------------------------
figure();
subplot(1,2,1);
subplot(1,2,2);
figure();
subplot(2,4,i+1);
OUTPUT :
VIVA - VOCE :
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).
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.
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.
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:
Mathematically :
Where:
Where:
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(:));
if size(img, 3) == 3 then
disp(correlation_matrix);
end
imshow(img);
title("Lena Image");
Output :
Viva Voce :
_____________________________________________________________________________
Ans: The mean represents the average pixel intensity of an image. It helps in
understanding the overall brightness of the 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:
_____________________________________________________________________________
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:
_____________________________________________________________________________
EXPERIMENT - 5
Objective - Implementation of Image Smoothing Filters (Mean and Median
filtering of an Image)
Theory -
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:
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:
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]
4. Implementation in Python
Median Filters
I=imread('nuron.jpg');
K = rgb2gray(I);
f= medfilt2(J,[3,3]);
f1=medfilt2(J,[10,10]);
figure;
i=imread('nuron.jpg');
g=rgb2gray(i);
g1=fspecial('average',[3 3]);
b1 = imfilter(g,g1);
b2=imfilter(g,g2);
figure;
I= imread('earcell.jpg');
R1=conv2(b,I);
•Mean filtering blurs the image and is good for Gaussian noise, whereas median
filtering preserves edges and is better for salt-and-pepper noise.
•A kernel is a small matrix used for convolution operations to modify pixel values
in an image.
•A larger filter size results in more smoothing, but too large a filter can excessively
blur the image.
•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 -
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');
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:
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).
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.
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.
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
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:-
img = imread('image.jpg');
img_gray = rgb2gray(img);
img_gray = double(img_gray);
function B = dct2d(A)
B = dct(A, -1);
B = dct(B', -1)';
endfunction
A = idct(B, -1);
A = idct(A', -1)';
endfunction
block_size = 8;
[m, n] = size(img_gray);
for i = 1:block_size:m
for j = 1:block_size:n
end
end
12 12 14 19 26 58 60 55;
14 13 16 24 40 57 69 56;
14 17 22 29 51 87 80 62;
// Quantization
// Reconstruct DC Coefficients
DC_reconstructed = cumsum(DPCM_DC_decoded);
// Dequantization
Dequantized_DCT = Quantized_DCT .* Q;
for i = 1:block_size:m
for j = 1:block_size:n
end
Reconstructed_img = uint8(Reconstructed_img);
figure;
imshow(Reconstructed_img);
title('Reconstructed Image');
OUTPUT:-
Viva-Voice questions
5. What is the use of differential pulse code modulation DPCM for compression?
// 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
clc;
clear;
close;
// Load an image
img = imread('blurry_image.jpg');
subplot(1, 2, 2);
imshow(restored_img);
title("Restored Image");
// Convert to grayscale
img_gray = rgb2gray(img);
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.
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.
• Pixels within a specified intensity range are set to a high intensity (white), while
all others are set to a low intensity (black).
• 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.
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
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?
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.