0% found this document useful (0 votes)
8 views4 pages

Image Processing Techniques in MATLAB

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)
8 views4 pages

Image Processing Techniques in MATLAB

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

Exp-1

colorImage = imread('C:\Users\HARISH\Pictures\[Link]'); % Replace with your image file


grayImage = rgb2gray(colorImage);
equalizedImage = histeq(grayImage);
figure;
subplot(1, 2, 1);
imshow(grayImage);
title('Original Grayscale Image 20224046');
subplot(1, 2, 2);
imshow(equalizedImage);
title('Histogram Equalized Image 20224046');
psnrValue = psnr(equalizedImage, grayImage);
fprintf('PSNR between original and equalized images: %.2f dB\n', psnrValue);
imwrite(equalizedImage, 'equalized_image.jpg');
Exp-2
clc;
clear;
close all;
image = imread("C:\Users\HARISH\Pictures\[Link]");
zoominfactor = 2;
zoomoutfactor = 0.2;
zoomInImage = imresize(image, zoominfactor, 'nearest');
zoomOutImage = imresize(image, zoomoutfactor, 'nearest');
figure;
subplot(1, 3, 1);
imshow(image);
title('Original Image Navneet Maurya (20224100)');
subplot(1, 3, 2);
imshow(zoomInImage);
title('Zoomed In Image');
subplot(1, 3, 3);
imshow(zoomOutImage);
title('Zoomed Out Image');
Exp-3
clc;
clear;
close all;
image = imread("C:\Users\HARISH\Pictures\night_image.jpeg");
gray_image = rgb2gray(image);
noisy_image = imnoise(gray_image,'salt & pepper',0.02);
figure;
subplot(2,2,1);
imshow(gray_image)
title('Original Image Navneet Maurya (20224100)')
subplot(2,2,2);
imshow(noisy_image);
title('Image Corrupted by Salt & Pepper Noise');
sigma = 1;
gaussian_filter = fspecial('gaussian',[5 5],sigma);
gaussian_filtered_image = imfilter(noisy_image,gaussian_filter,"symmetric");
median_filtered_image = medfilt2(noisy_image,[5 5]);
subplot(2,2,3);
imshow(gaussian_filtered_image)
title('Gaussian Filtered Image')
subplot(2,2,4);
imshow(median_filtered_image);
title('Median Filtered Image');
Exp-4
clc;
close all;
clear;
image = imread("C:\Users\HARISH\Pictures\[Link]");
grayImage = rgb2gray(image);
sobelX = [-1 0 1; -2 0 2; -1 0 1];
sobelY = [-1 -2 -1; 0 0 0; 1 2 1];
gradX = conv2(double(grayImage),sobelX,'same');
gradY = conv2(double(grayImage),sobelY,'same');
gradientMagnitude = sqrt(gradX.^2 + gradY.^2);
gradientMagnitude = uint8(gradientMagnitude);
inbuildEgdes = edge(grayImage,'sobel');
figure;
subplot(2,2,1);
imshow(image);
title('Original Image Arla Praveen Kumar(20224032)');
subplot(2,2,2);
imshow(grayImage);
title('Gray Scale Image');
subplot(2,2,3);
imshow(gradientMagnitude);
title('Edge Detection Using Sobel Operators (Manual)');
subplot(2,2,4);
imshow(inbuildEgdes);
title('Edge Detection Using Sobel Operators (Inbuilt)')
Exp-5
clc;
close all;
clear;
image = imread("C:\Users\HARISH\Pictures\[Link]");
grayImage = rgb2gray(image);
prewitX = [-1 0 1; -1 0 1; -1 0 1];
prewitY = [-1 -1 -1; 0 0 0; 1 1 1];
gradX = conv2(double(grayImage),prewitX,'same');
gradY = conv2(double(grayImage),prewitY,'same');
gradientMagnitude = sqrt(gradX.^2 + gradY.^2);
gradientMagnitude = uint8(gradientMagnitude);
inbuildEgdes = edge(grayImage,'prewitt');
figure;
subplot(2,2,1);
imshow(image);
title('Original Image Arla Praveen Kumar(20224032)');
subplot(2,2,2);
imshow(grayImage);
title('Gray Scale Image');
subplot(2,2,3);
imshow(gradientMagnitude);
title('Edge Detection Using Prewitt Operators (Manual)');
subplot(2,2,4);
imshow(inbuildEgdes);
title('Edge Detection Using Prewitt Operators (Inbuilt)')
Exp-6
clc;
close all;
clear;
image = imread"C:\Users\HARISH\Pictures\[Link]";
grayImage = rgb2gray(image);
laplacianFilter = [0 1 0; 1 -4 1; 0 1 0];
laplacianEdges = conv2(double(grayImage), laplacianFilter, 'same');
laplacianEdges = uint8(255 * mat2gray(abs(laplacianEdges)));
inbuiltEdges = edge(grayImage, 'log');
figure;
subplot(2,2,1);
imshow(image);
title('Original Image - Nandre Harish (20224099)');
subplot(2,2,2);
imshow(grayImage);
title('Gray Scale Image');
subplot(2,2,3);
imshow(laplacianEdges);
title('Edge Detection Using Laplacian Operator (Manual)');
subplot(2,2,4);
imshow(inbuiltEdges);
title('Edge Detection Using Laplacian (Inbuilt)');
Exp-7
clc;
clear;
close all;
color_img = imread("C:\Users\HARISH\Pictures\[Link]"); % Replace with your image file
gray_img = rgb2gray(color_img);
equalized_img = histeq(gray_img);
mse_value = immse(equalized_img, gray_img); % Mean Squared Error
psnr_value = 10 * log10(255^2 / mse_value); % PSNR formula
figure;
subplot(2,3,1);
imshow(color_img)
title('Original Colour Image - (20224032)');
subplot(2, 3, 2);
imshow(gray_img);
title('Original Grayscale Image (20224032)');
subplot(2, 3, 3);
imshow(equalized_img);
title('Histogram Equalized Image (20224032)');
subplot(2, 3, 4);
imhist(gray_img);
title('Histogram Before Equalization (20224032)');
subplot(2, 3, 5);
imhist(equalized_img);
title('Histogram After Equalization 20224032');
disp(['PSNR of the output image: ', num2str(psnr_value), ' dB']);
Exp-8
clc; clear; close all;
A = reshape(0:63, [8, 8]);
zigzag_order = zigzag_scan(A);
zigzag_matrix = zeros(8, 8);
for i = 1:numel(zigzag_order)
[row, col] = find(A == zigzag_order(i));
zigzag_matrix(row, col) = i; % Assign order index
end
disp('Reg no:- 20224099')
disp(A);
disp(zigzag_order);
imagesc(zigzag_matrix);
colormap(jet); % Use color map for better visualization
colorbar;
title('Zigzag Order of 8x8 Matrix 20224156');
axis equal;
axis off;
function zigzag_order = zigzag_scan(A)
[rows, cols] = size(A);
zigzag_order = zeros(1, rows * cols);
index = 1;
for sum_idx = 1:(rows + cols - 1)
if mod(sum_idx, 2) == 1
for i = max(1, sum_idx - cols + 1):min(rows, sum_idx)
j = sum_idx - i + 1;
zigzag_order(index) = A(i, j);
index = index + 1;
end
else
for j = max(1, sum_idx - rows + 1):min(cols, sum_idx)
i = sum_idx - j + 1;
zigzag_order(index) = A(i, j);
index = index + 1;
end
end
end
end
Exp-9
img = im2double(rgb2gray(imread("C:\Users\HARISH\Pictures\[Link]")));
[N, M] = size(img);
DCT_N = zeros(N, N);
DCT_M = zeros(M, M);
for u = 0:N-1
for x = 0:N-1
if u == 0
alpha = sqrt(1/N);
else
alpha = sqrt(2/N);
end
DCT_N(u+1, x+1) = alpha * cos(((2*x+1) * u * pi) / (2*N));
end
end
for v = 0:M-1
for y = 0:M-1
if v == 0
alpha = sqrt(1/M);
else
alpha = sqrt(2/M);
end
DCT_M(v+1, y+1) = alpha * cos(((2*y+1) * v * pi) / (2*M));
end
end
DCT_img = DCT_N * img * DCT_M';
x = DCT_img;
threshold = 1e-3;
retained_coeffs = sum(abs(DCT_img(:)) > threshold);
total_coeffs = numel(DCT_img);
compression_factor = (1 - retained_coeffs / total_coeffs) * 100;
recovered_img = DCT_N' * DCT_img * DCT_M;
DCT_recovered = dct2(recovered_img);
error_DCT = abs(DCT_img - DCT_recovered);
figure;
subplot(2,3,1);
imshow(img, []);
title('Original Image');
subplot(2,3,2);
imshow(x);
title('DCT of Original');
subplot(2,3,3);
imshow(recovered_img, []);
title(['Recovered Image (Compression: ', num2str(compression_factor, '%.2f'), '%)']);
subplot(2,3,4);
imshow(DCT_recovered);
title('DCT of Recovered');
subplot(2,3,5);
imshow(error_DCT, []);
title('DCT Error');
subplot(2,3,6);
plot(1:N, sum(error_DCT,2), 'r', 'LineWidth', 1.5);
xlabel('Row Index'); ylabel('Error Sum');
title('DCT Error across Rows'); grid on;
sgtitle('DCT and Inverse DCT of an image(Reg. No.:20224156)');
disp('Reg. No.:20224156');
disp('First 5x5 values of Original image:');
disp(img(1:5, 1:5));
disp('First 5x5 values of DCT image:');
disp(x(1:5, 1:5));
disp('First 5x5 values of reconstructed image after IDCT:');
disp(recovered_img(1:5, 1:5));
disp('First 5x5 values of absollute error between original and reconstructed image:');
disp(error_DCT(1:5, 1:5));
Exp-10
clc; clear; close all;
original_image = rgb2gray(imread("C:\Users\HARISH\Pictures\india_flag.jpg")); % Example grayscale image
original_image = im2double(original_image); % Convert to double for calculations
noisy_image = imnoise(original_image, 'gaussian', 0, 0.01); % Mean=0, Variance=0.01
gaussian_filtered_image = imgaussfilt(noisy_image, 1); % Standard deviation = 1
median_filtered_image = medfilt2(noisy_image, [3, 3]); % 3x3 window
wiener_filtered_image = wiener2(noisy_image, [3, 3]); % Adaptive noise filtering
h = fspecial('average', [3, 3]); % 3x3 averaging kernel
average_filtered_image = imfilter(noisy_image, h, 'replicate');
bilateral_filtered_image = imbilatfilt(noisy_image, 0.1, 2); % Edge-preserving filter
filters = {'Gaussian', 'Median', 'Wiener', 'Average', 'Bilateral'};
filtered_images = {gaussian_filtered_image, median_filtered_image, wiener_filtered_image, average_filtered_image, bilateral_filtered_image};
mse_values = zeros(1, length(filters));
psnr_values = zeros(1, length(filters));
mse_noise = mean((original_image-noisy_image).^2,'all');
psnr_noise = 10*log10(1/mse_noise);
for i = 1:length(filters)
mse_values(i) = mean((original_image - filtered_images{i}).^2, 'all');
psnr_values(i) = 10 * log10(1 / mse_values(i));
end
figure;
sgtitle('Denoising of Grayscale Image Using Various Filters (Reg no.:- 20224032)');
subplot(2,4,1), imshow(original_image), title('Original Image');
subplot(2,4,2), imshow(noisy_image), title(sprintf('Noisy Image\nMSE: %.5f, PSNR: %.2f dB', mse_noise, psnr_noise));
subplot(2,4,3), imshow(gaussian_filtered_image), title(sprintf('Gaussian Filter\nMSE: %.5f, PSNR: %.2f dB', mse_values(1), psnr_values(1)));
subplot(2,4,4), imshow(median_filtered_image), title(sprintf('Median Filter\nMSE: %.5f, PSNR: %.2f dB', mse_values(2), psnr_values(2)));
subplot(2,4,5), imshow(wiener_filtered_image), title(sprintf('Wiener Filter\nMSE: %.5f, PSNR: %.2f dB', mse_values(3), psnr_values(3)));
subplot(2,4,6), imshow(average_filtered_image), title(sprintf('Average Filter\nMSE: %.5f, PSNR: %.2f dB', mse_values(4), psnr_values(4)));
subplot(2,4,7), imshow(bilateral_filtered_image), title(sprintf('Bilateral Filter\nMSE: %.5f, PSNR: %.2f dB', mse_values(5), psnr_values(5)));
Exp-11
clc;
clear;
close all;
img = imread("C:\Users\HARISH\Pictures\[Link]"); % Change to your image path
R = img(:, :, 1);
G = img(:, :, 2);
B = img(:, :, 3);
R_reconstructed = zeros(size(R), 'uint8');
G_reconstructed = zeros(size(G), 'uint8');
B_reconstructed = zeros(size(B), 'uint8');
figure;
set(gcf, 'Position', [100, 100, 1200, 600]); % Adjust figure size
for i = 1:8
bit_value = 2^(i-1); % Compute bit mask
R_bit = bitand(R, bit_value);
G_bit = bitand(G, bit_value);
B_bit = bitand(B, bit_value);
R_colored = uint8(R_bit > 0) * 255;
G_colored = uint8(G_bit > 0) * 255;
B_colored = uint8(B_bit > 0) * 255;
bit_plane_colored = cat(3, R_colored, G_colored, B_colored);
subplot(3, 4, i);
imshow(bit_plane_colored);
title(['Bit Plane ', num2str(i-1)]);
if (i >= 6) % Only add bit planes 5, 6, and 7 (since MATLAB index starts at 1)
R_reconstructed = R_reconstructed + R_bit;
G_reconstructed = G_reconstructed + G_bit;
B_reconstructed = B_reconstructed + B_bit;
end
end
reconstructed_img = cat(3, R_reconstructed, G_reconstructed, B_reconstructed);
mse = mean((double(img) - double(reconstructed_img)).^2, 'all');
psnr_value = 10 * log10((255^2) / mse);
psnr_text = ['Reconstructed Image(4,5,6,7 bitplanes combined)(PSNR: ', num2str(psnr_value, '%.2f'), ' dB)']; % Format PSNR value
subplot(3, 4, 9);
imshow(img);
title('Original Image');
subplot(3, 4, 10);
imshow(reconstructed_img);
title(psnr_text); % Display PSNR in title
sgtitle('Color Bit Plane Slicing and Reconstruction (Reg no.:- 20224095)');
Exp-12
clc;
clear;
close all;
img = imread("C:\Users\HARISH\Pictures\[Link]"); % Change to your image path
gray_img = rgb2gray(img);
se = strel('disk', 5); % You can change the shape & size
dilated_img = imdilate(gray_img, se);
eroded_img = imerode(gray_img, se);
edge_img = dilated_img - eroded_img;
figure;
subplot(2, 3, 1), imshow(img), title('Original Color Image');
subplot(2, 3, 2), imshow(gray_img), title('Grayscale Image');
subplot(2, 3, 3), imshow(dilated_img), title('Dilated Image');
subplot(2, 3, 4), imshow(eroded_img), title('Eroded Image');
subplot(2, 3, 5), imshow(edge_img), title('Edge Detection (Morphological Gradient)');
sgtitle('Morphological Operations & Edge Detection (Reg no:- 20224095)');

You might also like