Matlab Assisment
Matlab Assisment
Matlab Assisment
MATLAB ASSISMENT
INDEX
S.no Experiment name Page no
1 Image sampling explicitly 3-5
Connectivity between neighbour pixels
2 6-8
3 Distance between two pixels 9-11
4 Gray Level Transformation 12-16
5 Histogram normalization 17-21
6 Histogram equalization 22-23
7 Histogram matching 24-27
Page |3
%%
clc;
clear all;
clc;
%%
n=8;
a=size(img);
w=a(2);
h=a(1);
im=zeros(100);
for i=1:n:h
for j=1:n:w
for k=0:n-1
for l=0:n-1
im(i+k,j+l)=img(i,j);
end
end
end
end
subplot(1,2,1);
imshow(uint8(img));title('Original Image');
Explanation :-
clc;
clear all;
clc;
:- These lines clear the command window and workspace to start with a clean slate, ensuring
no previous variables or outputs interfere with the current execution.
Page |4
n=8;
:- This line initializes a variable n with the value 8. This variable is used to define the size of
the sampling grid.
:- This line reads an image file named '00001.jpg' from the specified directory
('Data/Concrete Crack Images/') and converts it to grayscale using the rgb2gray function. The
resulting grayscale image is stored in the variable img.
a=size(img);
:- This line calculates the size of the grayscale image img and stores it in the variable a. The
size is returned as a two-element row vector where the first element is the number of rows
(height) and the second element is the number of columns (width) in the image.
w=a(2);
h=a(1);
:- These lines extract the width (w) and height (h) of the image from the size vector a and
store them in separate variables.
im=zeros(100);
:- This line initializes a 100x100 matrix of zeros and assigns it to the variable im. This matrix
will store the sampled image.
for i=1:n:h
:- This line starts a loop that iterates over the rows of the image (img) with a step size of n. It
starts from 1 and goes up to the height of the image (h).
for j=1:n:w
:- This line starts a nested loop that iterates over the columns of the image (img) with a step
size of n. It starts from 1 and goes up to the width of the image (w).
for k=0:n-1
for l=0:n-1
im(i+k,j+l)=img(i,j);
end
end
:- These nested loops iterate over a small grid of size n by n within the image. For each pixel
position (i+k, j+l) in this grid, the intensity value of the corresponding pixel (i, j) in the
original image (img) is copied to the sampled image (im).
Page |5
subplot(1,2,1);
imshow(uint8(img));title('Original Image');
:- These lines create a figure with two subplots side by side. In the first subplot, the original
grayscale image (img) is displayed with the title 'Original Image'. In the second subplot, the
sampled image (im) is displayed with the title 'Sampled Image'. The uint8 function is used to
convert the sampled image to an 8-bit unsigned integer format.
Page |6
BW = imread('cameraman.tif');
imshow(BW);
CC = bwconncomp(BW);
numPixels = cellfun(@numel,CC.PixelIdxList);
[biggest,idx] = max(numPixels);
BW(CC.PixelIdxList{idx}) = 0;
imshow(BW);
Page |7
Explanation :-
1. BW = imread('cameraman.tif');
o This line reads an image file named “cameraman.tif” and stores it in the
variable BW.
o The imread function is commonly used in MATLAB or Octave to read image files
(such as TIFF, JPEG, or PNG) into an array.
o In this case, it loads the image data from the file “cameraman.tif” and assigns it
to the variable BW.
2. imshow(BW);
o This line displays the image stored in the variable BW.
o The imshow function is used to visualize images in MATLAB or Octave.
o When you run this line, it will open a window showing the “cameraman.tif” image.
3. CC = bwconncomp(BW);
o This line computes the connected components (connected regions) in the binary
image BW.
o The bwconncomp function identifies connected regions of non-zero pixels
(usually representing objects) in a binary image.
o It returns a structure CC containing information about the connected
components.
4. numPixels = cellfun(@numel,CC.PixelIdxList);
o This line calculates the number of pixels in each connected component.
o The cellfun function applies the @numel function (which returns the number of
elements in an array) to each cell in the CC.PixelIdxList.
o The result is an array numPixels where each element corresponds to the size
(number of pixels) of a connected component.
5. [biggest,idx] = max(numPixels);
o This line finds the largest connected component (the one with the most pixels)
and its index.
o The max function returns the maximum value in the numPixels array along with
its index.
o The largest connected component size is stored in biggest, and its index is
stored in idx.
6. BW(CC.PixelIdxList{idx}) = 0;
o This line sets all pixels belonging to the largest connected component to zero
(i.e., removes them from the image).
o It uses the index idx to access the pixel indices of the largest component
in CC.PixelIdxList.
o By setting these pixels to zero, we effectively remove the largest connected
component from the binary image.
7. imshow(BW);
o Finally, this line displays the modified binary image BW after removing the largest
connected component.
Page |8
OUTPUT :-
Page |9
bw = zeros(200, 200);
bw(50, 50) = 1;
bw(50, 150) = 1;
bw(150, 100) = 1;
D1 = bwdist(bw, 'euclidean');
D2 = bwdist(bw, 'cityblock');
D3 = bwdist(bw, 'chessboard');
D4 = bwdist(bw, 'quasi-euclidean');
figure
Explanation :-
OUTPUT :-
P a g e | 12
1. Linear Transformation
2. Logarithmic Transformation
3. Power-Law Transformation
:-
1. Linear Transformation
r = imread('cameraman.tif');
s = r;
subplot(1,2,1);
imshow(r);
title('Original Image');
subplot(1,2,2);
imshow(s);
title('Identity Transformation');
Explanation :-
Output :-
P a g e | 14
2. Logarithmic Transformation
r = im2double(r);
c = 1;
s = c * log(r+1);
Explanation :-
Output :-
3. Power-Law Transformation
input_image = im2double(input_image);
gamma = 0.4;
Explanation :-
P a g e | 16
Output :-
P a g e | 17
Q 5 . Histogram normalization:-
1. rice.png
2. cameraman.tif
:-
Code:-
1. rice.png
l = imread('rice.png');
Max = max(l(:));
Min = min(l(:));
m = (l-Min)./(Max - Min);
n = m.*Max;
figure
subplot(1,2,1),imshow(l),title('Original Image');
subplot(1,2,2),imshow(n),title('Normalized Image');
figure
Explanation :-
o This line reads an image file named “rice.png” and stores it in the variable l.
o The imread function is commonly used in image processing libraries (such as
OpenCV or MATLAB) to read image files (such as PNG, JPEG, or TIFF) into an
array.
o In this case, it loads the pixel values from the file “rice.png” and assigns them to
the variable l.
2. Finding Maximum and Minimum Pixel Intensities (Max = max(l(:)); and Min =
min(l(:));):
o These lines calculate the maximum and minimum pixel intensities in the image.
o The max function finds the highest pixel value in the entire image (regardless of
its position), and the result is stored in Max.
o Similarly, the min function finds the lowest pixel value, and the result is stored
in Min.
3. Normalizing the Image (m = (l-Min)./(Max - Min);):
o This line performs a pixel-wise normalization of the image.
o The expression (l-Min)./(Max - Min) computes the normalized pixel values
for each pixel in the image.
o It subtracts the minimum value (Min) from each pixel and then divides the result
by the range of pixel values (Max - Min).
o The result is stored in the variable m.
4. Scaling the Normalized Image (n = m.*Max;):
o This line scales the normalized image back to the original intensity range.
o It multiplies each pixel value in the normalized image (m) by the maximum pixel
intensity (Max).
o The result is stored in the variable n.
5. Displaying the Results in Subplots:
o figure
▪ Creates a new figure window for displaying the images.
o subplot(1,2,1),imshow(l),title('Original Image');
▪ Sets up a subplot grid with 1 row and 2 columns.
▪ Displays the original image (l) in the left subplot using imshow.
▪ Adds the title “Original Image” to this subplot.
o subplot(1,2,2),imshow(n),title('Normalized Image');
▪ Displays the scaled (normalized) image (n) in the right subplot.
▪ Adds the title “Normalized Image” to this subplot.
Output :-
P a g e | 19
2.cameraman.tif
i = imread('cameraman.tif');
Max = max(i(:));
Min = min(i(:));
j = (i-Min)./(Max - Min);
k = j.*Max;
figure
P a g e | 20
subplot(1,2,1),imshow(i),title('Original Image');
subplot(1,2,2),imshow(k),title('Normalized Image');
figure
Explanation :-
1. i = imread('cameraman.tif');
o This line reads an image file named 'cameraman.tif' and assigns it to the
variable i.
o The image is loaded into memory, and its pixel values are stored in the matrix i.
2. Max = max(i(:));
o The max function computes the maximum value of all pixel intensities in the
image i.
o The (:) notation converts the 2D matrix i into a 1D vector, allowing us to find
the maximum value across all pixels.
o The result is stored in the variable Max.
3. Min = min(i(:));
o Similarly, the min function computes the minimum value of all pixel intensities in
the image i.
o The result is stored in the variable Min.
4. j = (i-Min)./(Max - Min);
o This line calculates the normalized version of the image.
o It subtracts the minimum value (Min) from each pixel intensity and divides the
result by the range (Max - Min).
o The result is stored in the matrix j.
5. k = j.*Max;
o Finally, the normalized image j is multiplied by the maximum value (Max) to bring
it back to the original intensity range.
o The result is stored in the matrix k.
6. figure
o This command creates a new figure window for displaying plots or images.
7. subplot(1,2,1),imshow(i),title('Original Image');
o This line sets up a subplot arrangement with 1 row and 2 columns.
o The first subplot (left side) displays the original image i using imshow.
o The title of this subplot is set to “Original Image”.
8. subplot(1,2,2),imshow(k),title('Normalized Image');
o The second subplot (right side) displays the normalized image k.
o Again, the title of this subplot is set to “Normalized Image”.
Output :-
P a g e | 21
P a g e | 22
Q 6 . Histogram equalization :-
:-
Code:-
K = histeq(I);
figure
figure
Explanation :-
Output :-
P a g e | 24
Q 7 . Histogram matching :-
:-
Code:-
A = imread("westconcordaerial.png");
imhist(A);
Ref = imread("westconcordorthophoto.png");
imhist(Ref);
size(A)
size(Ref)
B = imhistmatch(A,Ref);
imhist(B);
imshow(A)
imshow(Ref)
imshow(B)
Explanation :-
P a g e | 25
1. A = imread("westconcordaerial.png");
o This line reads an image from the file named "westconcordaerial.png" and
assigns it to the variable A. The image is loaded into memory for further
processing.
2. imhist(A);
o The imhist function computes and displays the histogram of the grayscale
image stored in A. A histogram represents the distribution of pixel intensities in
an image.
3. Ref = imread("westconcordorthophoto.png");
o Similarly, this line reads another image from the file
named "westconcordorthophoto.png" and assigns it to the variable Ref.
4. imhist(Ref);
o Here, the histogram of the grayscale reference image (Ref) is computed and
displayed.
5. size(A)
o The size function returns the dimensions of the image A. In this case, it provides
the size of A in terms of rows, columns, and color channels (3 for RGB).
6. size(Ref)
o Similarly, this line returns the dimensions of the reference image Ref.
7. B = imhistmatch(A,Ref);
o The imhistmatch function adjusts the histogram of the 2-D grayscale or
truecolor image A such that it approximately matches the histogram of the
reference image Ref.
o The resulting image B takes on the characteristics of A, but its histogram is
matched to that of Ref.
8. imhist(B);
o Displays the histogram of the histogram-matched image B.
9. imshow(A)
o Displays the original RGB image A with any color cast it may have.
10. title("RGB Image with Color Cast")
o Adds a title to the displayed image A, labeling it as the “RGB Image with Color
Cast.”
11. imshow(Ref)
o Displays the reference grayscale image Ref.
12. title("Reference Grayscale Image")
o Adds a title to the displayed reference image Ref.
13. imshow(B)
o Displays the histogram-matched RGB image B.
14. title("Histogram Matched RGB Image")
o Adds a title to the displayed histogram-matched image B.
Output :-
P a g e | 26
\
P a g e | 27