Final Report
Final Report
2.Write a program to convert an RGB image to Grayscale, HSV, and LAB color spaces and save
the results
Code:
image = imread('/MATLAB Drive/index.png');
if size(image,3) == 3
newImage = rgb2gray(image);
end
imshow(newImage);
title('Converted GrayScale Image');
yrow = 100;
xcol = 150;
pixel_value = newImage(yrow,xcol);
imshow(image);
title('RGB Image');
redvalue = image(row,col,1);
greenvalue = image(row, col,2);
bluevalue = image(row,col,3);
% Resize image
resized_img = imresize(img, 0.5);
subplot(232); imshow(resized_img);
title('Resized Image');
% Convert to grayscale
gray_img = rgb2gray(img);
subplot(223); imshow(gray_img);
title('Grayscale Image');
% Histogram Equalization
equalized_img = histeq(gray_img);
subplot(231); imshow(equalized_img);
title('Histogram Equalized Image');
% 2. Resize
resized_img = imresize(img, 0.5);
figure
imshow(resized_img)
title('Resized Image');
% Convert to grayscale
gray_img = rgb2gray(img);
figure
imshow(gray_img)
title('Grayscale Image');
% Gaussian Filtering
smoothed_img = imgaussfilt(gray_img, 2);
figure
imshow(smoothed_img)
title('Gaussian Smoothed Image');
7. Write a program for sobel and canny method.
code:
img = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg');
figure
imshow(img)
title('Original Image');
gray_img = rgb2gray(img);
figure, imshow(gray_img), title('Grayscale Image');
Edge Detection
edges_sobel = edge(gray_img, 'sobel');
edges_canny = edge(gray_img, 'canny');
figure
subplot(1,2,1)
imshow(edges_sobel)
title('Sobel Edge Detection');
subplot(1,2,2)
imshow(edges_canny)
title('Canny Edge Detection');
% Save images
imwrite(uint8(bwImg) * 255, 'Graylevel_Thresholding_thresholded.png');
% Convert logical to uint8 for saving
imwrite(holeImg, 'Graylevel_Thresholding_blend.png');
9. Write a program for Global Thresholding using Otsu's Method (Automatic
Threshold)
Code:
img = rgb2gray(imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg'));
% Show images
subplot(1, 3, 1); imshow(img); title('Original Image');
subplot(1, 3, 2); imshow(bwImg); title('Otsu Thresholded');
subplot(1, 3, 3); imshow((1-bwImg).*im2double(img)); title('Overlay');
% Plot histogram
figure;
[counts, x] = imhist(img);
bar(x, counts); hold on;
plot([otsuThresh otsuThresh], [0 max(counts)], 'r-', 'LineWidth', 2);
title('Histogram with Otsu Threshold');
xlabel('Intensity Level'); ylabel('Pixel Count');
axis([0 255 0 max(counts)]);
set(gca, 'FontSize', 14);
set(gcf, 'Color', 'white');
% Save histogram
saveas(gcf, 'Global_Thresholding_hist.png');
10.Write a program for region labeling
code:
% Load test image
img = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg');
% Region labeling
L = bwlabel(bwImg, 8); % 8-connectivity
rgbLabel = label2rgb(L, 'jet', 'k'); % Assign colors
% Show images
subplot(1, 3, 1); imshow(img); title('Grayscale Image');
subplot(1, 3, 2); imshow(bwImg); title('Binarized Image');
subplot(1, 3, 3); imshow(rgbLabel); title('Labeled Regions');
% Save images
imwrite(bwImg, 'Region_Labeling_bw.png');
imwrite(rgbLabel, 'Region_Labeling_rgbLabel.png');
% Save images
imwrite(uint8(bwImg) * 255, 'Hole_Filling_bw.png');
imwrite(uint8(filledBwImg) * 255, 'Hole_Filling_filled.png');
% Show images
subplot(1, 3, 1), imshow(img); title('Original Image');
subplot(1, 3, 2), imshow(BW1); title('Dilation by Small Disk');
subplot(1, 3, 3), imshow(BW2); title('Dilation by Larger Disk');
% Save images
imwrite(BW1, 'Dilation_disk_10.png');
imwrite(BW2, 'Dilation_disk_20.png')
13. Write a program for Image Erosion (Shrinking Objects)
code:
% Load test image
img = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg');
% Show images
subplot(1, 3, 1), imshow(img); title('Original Image');
subplot(1, 3, 2), imshow(BW1); title('Erosion by Small Disk');
subplot(1, 3, 3), imshow(BW2); title('Eorsion by Larger Disk');
% Save images
imwrite(BW1, 'Erosion_disk_3.png');
imwrite(BW2, 'Erosion_disk_7.png');
% Show results
figure,
subplot(1, 3, 1), imshow(uint8(img)); title('Original Image');
subplot(1, 3, 2), imshow(filteredImg1); title('Roberts Diagonal');
subplot(1, 3, 3), imshow(filteredImg2); title('Roberts Anti-Diagonal');
% Apply thresholds
bwEdge1 = edgeSum > 0.1;
bwEdge2 = edgeSum > 0.3;
bwEdge3 = edgeSum > 0.6;
% Save images
imwrite(im2uint8(logEdgeSum), 'Roberts_face_logEdgeSum.png');
imwrite(bwEdge1, 'Roberts_face_bwEdge1.png');
imwrite(bwEdge2, 'Roberts_face_bwEdge2.png');
imwrite(bwEdge3, 'Roberts_face_bwEdge3.png');
15. Write a program for Sobel Edge Detection (Horizontal & Vertical)
Code:
%% Sobel Edge Detection
% Apply thresholds
bwEdge1 = edgeSum > 1600;
bwEdge2 = edgeSum > 8000;
bwEdge3 = edgeSum > 12800;
% Display results
figure(4), clf;
subplot(2, 2, 1), imshow(logEdgeSum); title('Edge Magnitude (Log)');
subplot(2, 2, 2), imshow(bwEdge1); title('Threshold > 1600');
subplot(2, 2, 3), imshow(bwEdge2); title('Threshold > 8000');
subplot(2, 2, 4), imshow(bwEdge3); title('Threshold > 12800');
% Save results
imwrite(im2uint8(logEdgeSum), 'Sobel_face_logEdgeSum.png');
imwrite(bwEdge1, 'Sobel_face_bwEdge1.png');
imwrite(bwEdge2, 'Sobel_face_bwEdge2.png');
imwrite(bwEdge3, 'Sobel_face_bwEdge3.png');
16. Write a program for Prewitt Edge Detection (Horizontal & Vertical with abs
& with log scaling & thresholding)
code:
%% Prewitt - pexels-lilartsy-2058498.jpg
% Display
figure(1), clf;
subplot(1, 3, 1), imshow(uint8(img)); title('Original');
subplot(1, 3, 2), imshow(filteredImg1); title('Prewitt Vertical');
subplot(1, 3, 3), imshow(filteredImg2); title('Prewitt Horizontal');
% Save
imwrite(im2uint8(filteredImg1), 'Prewitt_bike_vertical.png');
imwrite(im2uint8(filteredImg2), 'Prewitt_bike_horizontal.png');
% Save
imwrite(im2uint8(filteredImg1), 'Prewitt_face_vertical.png');
imwrite(im2uint8(filteredImg2), 'Prewitt_face_horizontal.png');
% Load image
img = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg ');
if size(img, 3) == 3
img = rgb2gray(img);
end
img = double(img);
% Apply thresholds
bwEdge1 = edgeSum > 900;
bwEdge2 = edgeSum > 4500;
bwEdge3 = edgeSum > 7200;
% Show results
figure(3), clf;
subplot(2, 2, 1), imshow(logEdgeSum); title('Edge Magnitude (Log)');
subplot(2, 2, 2), imshow(bwEdge1); title('Threshold > 900');
subplot(2, 2, 3), imshow(bwEdge2); title('Threshold > 4500');
subplot(2, 2, 4), imshow(bwEdge3); title('Threshold > 7200');
% Save results
imwrite(im2uint8(logEdgeSum), 'Prewitt_face_logEdgeSum.png');
imwrite(bwEdge1, 'Prewitt_face_bwEdge1.png');
imwrite(bwEdge2, 'Prewitt_face_bwEdge2.png');
imwrite(bwEdge3, 'Prewitt_face_bwEdge3.png');
17. Write a program for Canny Edge Detection (Multi-step Optimal Edges)
Code:
%% Canny Edge Detection 1
% Parameters
sigmaArray = [sqrt(2), sqrt(8), sqrt(32)];
thresh = 0.3;
for i = 1:numel(sigmaArray)
sigma = sigmaArray(i);
bw = edge(img, 'canny', thresh, sigma);
subplot(2, 2, i+1), imshow(bw); title(sprintf('Canny σ = %.2f',
sigma));
% Save result
filename = sprintf('Canny_face_sigma%.2f.png', sigma);
imwrite(bw, filename);
end
% Display
figure(2), clf;
subplot(1, 2, 1), imshow(img); title('Original');
subplot(1, 2, 2), imshow(bw); title(sprintf('Canny σ = %.2f', sigma));
% Save result
filename = sprintf('Canny_bike_sigma%.2f.png', sigma);
imwrite(bw, filename);