DIP Quiz No 2
DIP Quiz No 2
Question No 2
Question No 3
Question No 4
Question No 5
Question No 6
Question No 7
Question No 8
Code:
% Step 1: Load the cameraman image
I = imread('cameraman2.tif'); % Load image
% Step 2: Generate a filter using fspecial
fn = fspecial('average', [5 5]); % 5x5 averaging filter (can be changed to other filters)
% Step 3: Apply the filter to the image using imfilter
I_new = imfilter(I, fn); % Filtering the image with the generated mask
% Step 4: Display the original and filtered images side by side
figure;
subplot(1,2,1), imshow(I), title('Original Image'); % Display original image
subplot(1,2,2), imshow(I_new), title('Filtered Image'); % Display filtered image
Output:
Question No 9
Question No 10
Code:
% Step 1: Load the cameraman image
I = imread('cameraman.tif'); % Load the image from file
% Step 2: Display the original image
figure;
subplot(1,3,1), imshow(I), title('Original Image');
% Step 3: Create a uniform 3x3 averaging filter
fn = fspecial('average', [3 3]);
% Step 4: Apply the uniform averaging filter to the image
I_new = imfilter(I, fn);
% Step 5: Display the result of the uniform averaging filter
subplot(1,3,2), imshow(I_new), title('Uniform Average Filter');
% Step 6: Create a nonuniform 3x3 averaging filter (weighted)
fn2 = [1 2 1; 2 4 2; 1 2 1]; % Nonuniform filter with weights
fn2 = fn2 * (1/16); % Normalize the sum of weights to 1
% Step 7: Apply the nonuniform averaging filter to the image
I_new2 = imfilter(I, fn2);
% Step 8: Display the result of the nonuniform averaging filter
subplot(1,3,3), imshow(I_new2), title('Non-uniform Averaging Filter');
% Step 9: Create a Gaussian filter (with size 9x9 and standard deviation 1.5)
fn_gau = fspecial('gaussian', 9, 1.5);
% Step 10: Display the Gaussian filter as a 3D plot
figure;
bar3(fn_gau, 'b');
title('Gaussian Filter as a 3D Graph');
% Step 11: Filter the original image with the Gaussian filter
I_new3 = imfilter(I, fn_gau);
% Step 12: Display the original image, average filtered image, and Gaussian filtered image
figure;
subplot(1,3,1), imshow(I), title('Original Image');
subplot(1,3,2), imshow(I_new), title('Average Filter');
subplot(1,3,3), imshow(I_new3), title('Gaussian Filter');
Output:
Question No 11
Code:
% Load the cameraman image
I = imread('cameraman2.tif'); % Load the image
% Display the original image
figure;
subplot(1,3,1), imshow(I), title('Original Image');
% Experiment 1: Apply Gaussian filter with size 5x5 and sigma = 0.5
fn_gau1 = fspecial('gaussian', 5, 0.5); % 5x5 filter with sigma = 0.5
I_new1 = imfilter(I, fn_gau1);
% Experiment 2: Apply Gaussian filter with size 9x9 and sigma = 1.5
fn_gau2 = fspecial('gaussian', 9, 1.5); % 9x9 filter with sigma = 1.5
I_new2 = imfilter(I, fn_gau2);
% Experiment 3: Apply Gaussian filter with size 15x15 and sigma = 3
fn_gau3 = fspecial('gaussian', 15, 3); % 15x15 filter with sigma = 3
I_new3 = imfilter(I, fn_gau3);
% Display the results for different Gaussian filters
figure;
subplot(1,3,2), imshow(I_new1), title('Gaussian (5x5, σ = 0.5)');
subplot(1,3,3), imshow(I_new2), title('Gaussian (9x9, σ = 1.5)');
subplot(1,3,4), imshow(I_new3), title('Gaussian (15x15, σ = 3)');
Output:
Original Image:
Question No 12
Code:
% Load the moon image and convert it to double
I = imread('moon.tif');
Id = im2double(I); % Convert the image to double to preserve negative values
% Create the Laplacian filter with alpha = 0
f = fspecial('laplacian', 0);
% Apply the Laplacian filter to the image
I_filt = imfilter(Id, f);
% Display the original and Laplacian filtered image
figure;
subplot(2,2,1), imshow(Id), title('Original Image');
subplot(2,2,2), imshow(I_filt), title('Laplacian of Original');
Output:
Question No 13
Question No 14
Code:
>> % Load the earth1 image
I = imread('earth1.tif'); % Load as uint8
Id = im2double(I); % Convert to double for proper Laplacian filtering
% Create the Laplacian filter with fspecial
f = fspecial('laplacian', 0);
% Apply the Laplacian filter to the original uint8 image
I_filt_uint8 = imfilter(I, f); % Filtering the uint8 image
% Apply the Laplacian filter to the double image
I_filt_double = imfilter(Id, f); % Filtering the double image
% Display the results
figure;
subplot(1,2,1), imshow(I_filt_uint8), title('Filtered Image (uint8)');
subplot(1,2,2), imshow(I_filt_double, []), title('Filtered Image (double, scaled)');
% Display a scaled version of the Laplacian filtered image
subplot(2,2,3), imshow(I_filt_double, []), title('Scaled Laplacian');
% Subtract the filtered image from the original to sharpen
I_sharp = imsubtract(Id, I_filt_double);
% Display the sharpened image
subplot(2,2,4), imshow(I_sharp), title('Sharpened Image');
% Create a composite Laplacian mask
f2 = [0 -1 0; -1 5 -1; 0 -1 0]; % Composite mask for sharpening
% Apply the composite mask to the double image
I_sharp2 = imfilter(Id, f2);
% Display the results
figure;
subplot(1,2,1), imshow(Id), title('Original Image');
subplot(1,2,2), imshow(I_sharp2), title('Composite Laplacian');
Output:
Question No 15
Code:
% Load the moon image and convert it to double
I = imread('moon.tif');
Id = im2double(I);
% Standard Laplacian mask (does not account for corners)
f_standard = [0 -1 0; -1 4 -1; 0 -1 0];
% Modified Laplacian mask (accounts for corners)
f_corner = [-1 -1 -1; -1 8 -1; -1 -1 -1];
% Apply the standard Laplacian mask
I_filt_standard = imfilter(Id, f_standard);
% Apply the modified Laplacian mask
I_filt_corner = imfilter(Id, f_corner);
% Display the original, standard Laplacian, and modified Laplacian images
figure;
subplot(1,3,1), imshow(Id), title('Original Image');
subplot(1,3,2), imshow(I_filt_standard, []), title('Standard Laplacian');
subplot(1,3,3), imshow(I_filt_corner, []), title('Modified Laplacian');
Output:
Question No 16
1. Laplacian Filter
For the Laplacian filter, the second parameter represents alpha, which controls the
sharpness or shape of the Laplacian mask.
alpha:
A scalar value between −1-1−1 and 111, determining the weight of the center coefficient
in the Laplacian mask.
Adjusts the sensitivity of the filter to intensity variations:
alpha = 0: Creates the standard Laplacian mask.
alpha > 0: Increases the center coefficient, resulting in stronger sharpening.
alpha < 0: Reduces the center coefficient, which may cause smoother transitions
or less aggressive sharpening.
2. Gaussian Filter
For the Gaussian filter, the second parameter specifies the standard deviation (σ\sigmaσ) of
the Gaussian curve.
sigma:
Controls the spread of the Gaussian kernel:
A smaller σ\sigmaσ results in a narrower kernel, leading to less blurring (sharper
smoothing).
A larger σ\sigmaσ results in a wider kernel, leading to stronger blurring.
Typical values range from 0.50.50.5 to 5.05.05.0, depending on the desired blur intensity.
Example with σ=1.5:
f = fspecial('gaussian', 9, 1.5); % 9x9 kernel with σ = 1.5
3. Motion Filter
For the motion blur filter, the second parameter specifies the angle of motion in degrees:
f = fspecial('motion', len, theta);
theta:
Defines the direction of the motion blur:
theta = 0: Horizontal motion.
theta = 90: Vertical motion.
Other angles (e.g., 45°) create motion blur along the specified direction.
Example with motion at 45°:
f = fspecial('motion', 20, 45); % 20-pixel motion blur at 45°
Question No 17
1. Scaling the Sharpening Image
The sharpening image (Isharpening=I−IblurI) contains the high-frequency
components (edges and fine details). Adjusting the intensity of this image
before adding it back to the original can control the sharpening effect.
You can scale the sharpening image by a factor k to amplify or dampen the
sharpening:
Isharp=I+k⋅(I−Iblur)
Increasing K>1: Results in stronger sharpening (may cause oversharpening
and halos).
Decreasing K<1: Reduces sharpening, making it more subtle.
2. The Blurring Method and Parameters
The type of blur applied to create Iblur significantly affects the sharpening:
Gaussian Blur: Smooths edges naturally, producing gradual
sharpening.
Average Blur: Uniformly smooths all regions but may lose more
detail.
Median Blur: Preserves edges better than average or Gaussian blur.
Adjusting the blur's parameters (e.g., kernel size, σ) changes the sharpness:
Stronger Blur (Larger Kernel or σ): Leads to more aggressive
sharpening.
Weaker Blur (Smaller Kernel or σ): Results in finer sharpening.
Implementation of Unsharp Masking:
Code:
% Load the moon image and create a blurred version
I = imread('earth1.tif');
I = im2double(I); % Convert to double
I_blur = imgaussfilt(I, 2); % Apply Gaussian blur with sigma=2
% Step 1: Generate the sharpening image
I_sharpening = imsubtract(I, I_blur);
% Optional: Scale the sharpening image to control sharpening intensity
k = 1.5; % Sharpening factor (experiment with this value)
I_sharpening_scaled = I_sharpening * k;
% Step 2: Add the sharpening image to the original image
I_sharp2 = imadd(I, I_sharpening_scaled);
% Display the original and sharpened images
figure;
subplot(1,2,1), imshow(I), title('Original Image');
subplot(1,2,2), imshow(I_sharp2), title('Sharp Image');
Output:
Question No 18
Implement the sharping and with unsharping mask
Code:
% Step 1: Load the original image
I = imread('earth1.tif');
I = im2double(I); % Convert to double for accurate processing
% Step 2: Generate the unsharp masking kernel
f_unsharp = fspecial('unsharp'); % Default unsharp kernel
% Optional: Adjust the sharpening intensity by scaling the kernel
k = 1.5; % Sharpening factor
f_unsharp_scaled = f_unsharp * k;
% Step 3: Apply the unsharp mask kernel to the image
I_sharp3 = imfilter(I, f_unsharp_scaled); % Apply the scaled kernel
% Step 4: Display the original and sharpened images
figure;
subplot(1,2,1), imshow(I), title('Original Image');
subplot(1,2,2), imshow(I_sharp3), title('Sharp Image');
Output:
Question No 19
In high-boost filtering, the level of sharpening is controlled by adjusting the amplification factor
A in the high-boost mask. This factor determines the contribution of the original image to the
sharpening process.
The general formula for the high-boost mask is:
fHB=A⋅Identity Matrix−fLaplacian
A is the amplification factor, which scales the original image.
fLaplacian is the Laplacian mask that extracts high-frequency details.
How AAA Affects the Level of Sharpening:
1. When A = 1:
The high-boost filter reduces to a standard Laplacian sharpening filter.
Only the high-frequency components (edges) are added to the image, resulting in
basic sharpening.
2. When A>1:
The filter amplifies the contribution of the original image, enhancing both edges
and fine details.
Larger values of A result in stronger sharpening, but excessive values may
introduce artifacts or make the image appear unnatural.
3. When A<1:
The original image's contribution is reduced, leading to a weaker sharpening
effect.
Code:
Question No 20
Effect of A on the Output Image in High-Boost Filtering
In high-boost filtering, the parameter A (amplification factor) controls the contribution of the
original image to the final sharpened image. The high-boost filter can be expressed as.
Isharp=A⋅I−Iblur
or equivalently, using the high-boost mask fHB:
fHB=A⋅Identity Matrix−fLaplacian
Observations:
For A<1:
The output resembles a weaker version of sharpening, leaning more towards a
smoothed image.
For A = 1:
The filter generalizes to the composite Laplacian mask, providing standard
sharpening.
For A>1:
The output increasingly resembles the original image scaled by A, with enhanced
edges. Extremely high A values make the image look artificially bright or over-
sharpened.
Code:
% Load the moon image and convert to double
I = imread('earth1.tif');
I = im2double(I);
% Experiment with different values of A
A_values = [0.5, 1, 2, 5]; % A = 0.5, 1, 2, 5
figure;
for i = 1:length(A_values)
A = A_values(i); % Current value of A
f_hb = [0 -1 0; -1 (4 + A) -1; 0 -1 0]; % High-boost filter
I_sharp = imfilter(I, f_hb); % Apply the filter
Expected Results:
1. A=0.5:
Output appears softer, with reduced sharpening.
2. A=1:
Standard sharpening, equivalent to using the composite Laplacian mask.
3. A=2:
Strong sharpening; edges become more pronounced, and the image looks more
detailed.
4. A=5:
Intense sharpening; the image begins to look over-enhanced, with noticeable
artifacts and brightened regions resembling a scaled version of the original.
Question No 21
The high-boost filter becomes ineffective when the amplification factor A is so large that the
resulting sharpened image resembles the original image multiplied by a constant. This happens
because the contribution of the original image (A⋅I) dominates the high-frequency details (I−Iblur)
added by the filter.
Understanding the Transition:
The high-boost filtering equation is:
Isharp=A⋅I−Iblur
The first term, A⋅I, scales the original image.
The second term, I−Iblur, enhances edges (high-frequency components).
When A becomes very large, the effect of I−Iblurbecomes negligible in comparison to A⋅I, and the
output is dominated by the original image scaled by A.
Critical Transition Value for A:
1. For small to moderate A values (e.g., A=1 to A≈5A ):
The edge-enhancing term (I−Iblur) significantly impacts the result.
The image appears sharper, and the filter remains effective.
2. For large A values (e.g., A>10):
The I−Iblur term becomes negligible, and the image resembles IA⋅I.
The filter stops being effective as it no longer meaningfully enhances edges or
details.
Code:
% Load the moon image and convert to double
I = imread('moon.tif');
I = im2double(I);
Expected Results:
1. For A=1: The filter behaves like the composite Laplacian mask. Moderate sharpening is
observed.
2. For A=2 to 555: Strong sharpening, with pronounced edges and fine detail enhancement.
3. For A=10: The image starts to appear overly bright, with sharpening becoming less
noticeable.
4. For A≥20: The output closely resembles A⋅IA , and the filter stops being effective.
Question No 22
The Discrete Fourier Transform (DFT) of an image, such as the cameraman image, produces
a complex-valued result. The coefficients in the Fourier Transform represent the frequency
components of the image, with the following characteristics:
1. Real and Imaginary Components:
The result of the DFT includes both real and imaginary parts.
Range of Values:
The values in the DFT coefficients often range from negative to positive, with a
minimum value significantly lower than 0 and a maximum value significantly higher than
255 (the typical range for grayscale images).
The DC component (overall intensity of the image) contributes the largest positive value,
while high-frequency components may have smaller or even negative values.
Code:
% Step 1: Load the cameraman image
I = imread('cameraman.tif');
I = im2double(I); % Convert to double for accurate Fourier Transform
% Step 2: Compute the 2D Fourier Transform of the image
ft = fft2(I); % Perform the 2D Fourier Transform
% Step 3: Compute the minimum and maximum values of the FT coefficients
ft_min = min(ft(:)); % Minimum value
ft_max = max(ft(:)); % Maximum value
% Display the results
disp(['Minimum value of FT coefficients: ', num2str(ft_min)]);
disp(['Maximum value of FT coefficients: ', num2str(ft_max)]);
Output:
Question No 23
The Fourier Transform of an image produces a result in the complex number domain, where
each coefficient consists of a real part and an imaginary part. The magnitude of these complex
numbers represents the strength (amplitude) of the frequency components, which is what we
typically visualize in a Fourier spectrum.
Display of Non-Complex Values:
The imshow function in MATLAB cannot directly display complex-valued data.
By applying abs, we convert the complex Fourier coefficients into their real-valued
magnitudes, making them displayable.
Relevance of Magnitude for Visualization:
The magnitude spectrum highlights the strength of frequency components, which is the
primary information of interest in most Fourier analysis tasks.
The imaginary part or phase is not visually meaningful for understanding the spectrum.
Avoiding Misinterpretation:
If the abs function is not used, the data would need to be split into real and imaginary
parts, resulting in separate visualizations. This can complicate interpretation and would
not represent the overall frequency amplitude effectively.
Code:
% Load the cameraman image and compute the Fourier Transform
I = imread('cameraman2.tif');
I = im2double(I);
ft = fft2(I); % Compute 2D Fourier Transform
ft_shift = fftshift(ft); % Shift zero-frequency component to the center
% Display the Fourier Transform magnitude
figure;
subplot(1,2,1), imshow(abs(ft_shift), []), title('Magnitude Spectrum (with abs)');
subplot(1,2,2), imshow(real(ft_shift), []), title('Real Part Only (without abs)');
Output:
Question No 24
Code:
% Load the image and compute its 2D Fourier Transform
I = imread('cameraman.tif');
I = im2double(I);
ft = fft2(I);
ft_shift = fftshift(ft); % Shift the zero-frequency component to the center