0% found this document useful (0 votes)
13 views15 pages

Final Report

The document contains a series of MATLAB programs that perform various image processing tasks, including extracting image properties, converting color spaces, accessing pixel values, and applying different filtering and edge detection techniques. Each section provides code snippets for specific operations such as histogram equalization, Gaussian filtering, and region labeling, along with instructions for saving the processed images. The programs utilize functions like imread, imshow, and imwrite to manipulate and display images effectively.

Uploaded by

Tasneem Nadira
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)
13 views15 pages

Final Report

The document contains a series of MATLAB programs that perform various image processing tasks, including extracting image properties, converting color spaces, accessing pixel values, and applying different filtering and edge detection techniques. Each section provides code snippets for specific operations such as histogram equalization, Gaussian filtering, and region labeling, along with instructions for saving the processed images. The programs utilize functions like imread, imshow, and imwrite to manipulate and display images effectively.

Uploaded by

Tasneem Nadira
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/ 15

1.

Write a program to Extract Image Properties (Metadata & Dimensions)


Code:
img = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg');% Read the
image from the drive
imageInof = imfinfo('/MATLAB Drive/pexels-lilartsy-2058498.jpg'); %
get image information
disp(imageInof);
[height,width,numChannels] = size(img);
fprintf('Image dimension: %d %d\n',height,width);
fprintf('Number of channels:%d\n',numChannels);
redChannel = img(:,:,1);
greenChannel =img(:,:,2);
blueChannel =img(:,:,3)
imshow(blueChannel);
title('blue Channel');
imwrite(img,'/MATLAB Drive/Output_image.png');

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');

% Save and display the original image


imwrite(image, '\MATLAB Drive\original.jpg');
subplot(221); imshow(image); title('Original Image');

% Convert to grayscale and save


grayscale = rgb2gray(image);
imwrite(grayscale, '\MATLAB Drive\grayscale.jpg');
subplot(222); imshow(grayscale); title('Grayscale Image');

% Convert to HSV and save as an RGB representation for visualization


hsvImage = rgb2hsv(image);
hsvAsRGB = hsv2rgb(hsvImage); % Convert to RGB format for saving
imwrite(hsvAsRGB, '\MATLAB Drive\hsvimage.jpg');
subplot(223); imshow(hsvAsRGB); title('HSV Image');

% Convert to LAB and save as an RGB representation for visualization


labImage = rgb2lab(image); % Use RGB input for LAB conversion
% LAB cannot be saved directly, so normalize and convert to RGB for saving
labNormalized = (labImage + [0, 128, 128]) ./ [100, 255, 255];
labAsRGB = lab2rgb(labNormalized); % Convert normalized LAB to RGB
imwrite(labAsRGB, '\MATLAB Drive\\labimage.jpg');
subplot(224); imshow(labAsRGB); title('LAB Image');
3. Write a program to Access Pixel Values in RGB Images
Code:
image = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg');
imshow(image);
title('Orginal RGB Image');

if size(image,3) == 3
newImage = rgb2gray(image);
end
imshow(newImage);
title('Converted GrayScale Image');

yrow = 100;
xcol = 150;
pixel_value = newImage(yrow,xcol);

disp(['The pixel value at pint: (',num2str(yrow),',',num2str(xcol),') =',


num2str(pixel_value)])

4. Write a program to Extract RGB Channels from an Image


Code:
image = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg');

imshow(image);
title('RGB Image');

row = 100; % Row index (Y-coordinate)


col = 150; % Column index (X-coordinate)
% Access the pixel values

redvalue = image(row,col,1);
greenvalue = image(row, col,2);
bluevalue = image(row,col,3);

% Display the pixel values


disp(['Pixel value at (', num2str(row), ',', num2str(col), '):']);
disp(['Red: ', num2str(redvalue)]);
disp(['Green: ', num2str(greenvalue)]);
disp(['Blue: ', num2str(bluevalue)]);
5. Write a program for Histogram Equalization (Contrast Adjustment)
Code:
img = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg');

% 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');

6. Write a program for Gaussian Filtering (Noise Smoothing)


Code:
img = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg');
figure
imshow(img)
title('Original 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');

8. Write a program for Gray-level Thresholding (Manual Threshold)


Code:
img = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg');
% Convert to grayscale
grayImg = rgb2gray(img);
% Thresholding
level = 105;
bwImg = grayImg < level; % Binary mask
holeImg = grayImg .* uint8(bwImg); % Masked grayscale image
% Show images
subplot(1, 3, 1); imshow(grayImg); title('Grayscale Image');
subplot(1, 3, 2); imshow(bwImg); title('Thresholded Image');
subplot(1, 3, 3); imshow(holeImg); title('Binary Map × Grayscale');

% 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'));

% Perform Otsu thresholding


level = graythresh(img);
otsuThresh = round(level * 255);
bwImg = imbinarize(img, level); % use imbinarize instead of im2bw

% 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');

% Save binary thresholded image


imwrite(bwImg, 'Global_Thresholding_bw.png');

% 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');

% Convert to grayscale if image is RGB


if size(img, 3) == 3
img = rgb2gray(img);
end

% Binarize image using Otsu's method (note: invert foreground)


level = graythresh(img);
bwImg = ~imbinarize(img, level); % foreground = white (objects),
background = black

% 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');

11. Write a program for Hole Filling in Binary Images


code:
% Load test image
img = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg');

% Convert to grayscale if needed


if size(img, 3) == 3
img = rgb2gray(img);
end

% Binarize using a fixed threshold


level = 105;
bwImg = img < level; % Foreground = white (logical 1), Background =
black (logical 0)

% Fill holes in the binary image


filledBwImg = imfill(bwImg, 'holes');
% Show images
subplot(1, 2, 1), imshow(bwImg); title('Original Binary Image');
subplot(1, 2, 2), imshow(filledBwImg); title('Filled Binary Image');

% Save images
imwrite(uint8(bwImg) * 255, 'Hole_Filling_bw.png');
imwrite(uint8(filledBwImg) * 255, 'Hole_Filling_filled.png');

12. Write a program for Image Dilation (Expanding Objects)


Code:
% Load test image
img = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg');

% Perform dilation with small disk


se1 = strel('disk', 10);
BW1 = imdilate(img,se1);

% Perform dilation with larger disk


se2 = strel('disk', 20);
BW2 = imdilate(img, se2);

% 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');

% Perform dilation with small disk


se1 = strel('disk', 3);
BW1 = imerode(img,se1);

% Perform dilation with larger disk


se2 = strel('disk', 7);
BW2 = imerode(img, se2);

% 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');

14. Write a program for Roberts Edge Detection (Diagonal Gradients)


Code:
% Load and convert to grayscale
img = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg');
if size(img, 3) == 3
img = rgb2gray(img);
end
img = double(img);

% Apply Roberts filters


h1 = [1, 0; 0, -1];
h2 = [0, 1; -1, 0];
filteredImg1 = imfilter(img, h1, 'replicate');
filteredImg2 = imfilter(img, h2, 'replicate');

% Normalize and visualize


filteredImg1 = log(abs(filteredImg1) + 1);
filteredImg1 = filteredImg1 / max(filteredImg1(:));
filteredImg2 = log(abs(filteredImg2) + 1);
filteredImg2 = filteredImg2 / max(filteredImg2(:));

% 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');

% Save filtered results


imwrite(im2uint8(filteredImg1), 'Roberts_face_vertical.png');
imwrite(im2uint8(filteredImg2), 'Roberts_face_horizontal.png');

%% Edge Magnitude and Thresholding

% Recalculate edge magnitude


edgeSum = filteredImg1.^2 + filteredImg2.^2;
logEdgeSum = log(edgeSum + 1);
logEdgeSum = logEdgeSum / max(logEdgeSum(:));

% Apply thresholds
bwEdge1 = edgeSum > 0.1;
bwEdge2 = edgeSum > 0.3;
bwEdge3 = edgeSum > 0.6;

% Show thresholded results


figure,
subplot(2, 2, 1), imshow(logEdgeSum); title('Edge Magnitude (Log)');
subplot(2, 2, 2), imshow(bwEdge1); title('Threshold > 0.1');
subplot(2, 2, 3), imshow(bwEdge2); title('Threshold > 0.3');
subplot(2, 2, 4), imshow(bwEdge3); title('Threshold > 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

clear, clc, close all

% Load and preprocess


img = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg');
if size(img, 3) == 3
img = rgb2gray(img);
end
img = double(img);

% Apply Sobel filters


h = fspecial('sobel');
filteredImg1 = imfilter(img, h', 'replicate'); % Vertical edges
filteredImg2 = imfilter(img, h, 'replicate'); % Horizontal edges

% Compute edge magnitude and log-normalize


edgeSum = filteredImg1.^2 + filteredImg2.^2;
logEdgeSum = log(edgeSum + 1);
logEdgeSum = logEdgeSum / max(logEdgeSum(:));

% 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

clear, clc, close all

% Load and convert image to grayscale if needed


img = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg');
if size(img, 3) == 3
img = rgb2gray(img);
end
img = double(img);

% Create Prewitt filter


h = fspecial('prewitt');

% Vertical edges (h')


filteredImg1 = imfilter(img, h', 'replicate');
filteredImg1 = abs(filteredImg1);
filteredImg1 = filteredImg1 / max(filteredImg1(:));

% Horizontal edges (h)


filteredImg2 = imfilter(img, h, 'replicate');
filteredImg2 = abs(filteredImg2);
filteredImg2 = filteredImg2 / max(filteredImg2(:));

% 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');

%% Prewitt - berndsface.png (log scaled)

clear, clc, close all

% Load and convert image to grayscale if needed


img = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg ');
if size(img, 3) == 3
img = rgb2gray(img);
end
img = double(img);

% Create Prewitt filter


h = fspecial('prewitt');

% Vertical edges (h')


filteredImg1 = imfilter(img, h', 'replicate');
filteredImg1 = log(abs(filteredImg1) + 1);
filteredImg1 = filteredImg1 / max(filteredImg1(:));

% Horizontal edges (h)


filteredImg2 = imfilter(img, h, 'replicate');
filteredImg2 = log(abs(filteredImg2) + 1);
filteredImg2 = filteredImg2 / max(filteredImg2(:));
% Display
figure(2), clf;
subplot(1, 3, 1), imshow(uint8(img)); title('Original');
subplot(1, 3, 2), imshow(filteredImg1); title('Prewitt Vertical
(log)');
subplot(1, 3, 3), imshow(filteredImg2); title('Prewitt Horizontal
(log)');

% Save
imwrite(im2uint8(filteredImg1), 'Prewitt_face_vertical.png');
imwrite(im2uint8(filteredImg2), 'Prewitt_face_horizontal.png');

%% Prewitt Edge Detection with Thresholding

clear, clc, close all

% Load image
img = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg ');
if size(img, 3) == 3
img = rgb2gray(img);
end
img = double(img);

% Create Prewitt kernels


h = fspecial('prewitt');

% Apply filters in vertical and horizontal directions


filteredImg1 = imfilter(img, h', 'replicate'); % Vertical edges
filteredImg2 = imfilter(img, h, 'replicate'); % Horizontal edges

% Compute edge magnitude and normalize (log scale)


edgeSum = filteredImg1.^2 + filteredImg2.^2;
logEdgeSum = log(edgeSum + 1);
logEdgeSum = logEdgeSum / max(logEdgeSum(:));

% 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

clear, clc, close all

% Load and convert image to grayscale if needed


img = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg');
if size(img, 3) == 3
img = rgb2gray(img);
end

% Parameters
sigmaArray = [sqrt(2), sqrt(8), sqrt(32)];
thresh = 0.3;

% Plot original and filtered images


figure(1), clf;
subplot(2, 2, 1), imshow(img); title('Original');

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

%% Canny Edge Detection - 2

% Load and convert image to grayscale if needed


img = imread('/MATLAB Drive/pexels-lilartsy-2058498.jpg');
if size(img, 3) == 3
img = rgb2gray(img);
end

% Use first sigma value from above


sigma = sigmaArray(1);
bw = edge(img, 'canny', thresh, sigma);

% 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);

You might also like