Matlab Assisment

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Page |1

MATLAB ASSISMENT

SUBMITTED TO :- Prof . Avinash kumar

NAME:- SAKET KUMAR KESHRI


CLASS :- M.C.A – 2ND
ROLL:- 21
SUBJECT :- IMAGE PROCESSING TECHNIQUES
Page |2

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

Q 1.write a matlab code to perform image


sampling explicitly ?
:-
Code:

%%
clc;
clear all;
clc;

%%
n=8;

img = rgb2gray(imread('Data/Concrete Crack Images/00001.jpg'));

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

subplot(1,2,2); imshow(uint8(im));title('Sampled 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.

img = rgb2gray(imread('Data/Concrete Crack Images/00001.jpg'));

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

subplot(1,2,2); imshow(uint8(im));title('Sampled 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

Q 2. Write a MATLAB code to check connectivity between neighboring


pixels ?
:-
Code :-

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

Q 3 :- Distance between two pixels :-


Using
(Euclidean, City Block, Chess Board and Quasi-Euclidean) ?
:-
Code :-

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

subplot(2, 2, 1), subimage(mat2gray(D1)), title('Euclidean Distance')

hold on, imcontour(D1)

subplot(2, 2, 2), subimage(mat2gray(D2)), title('Cityblock Distance')

hold on, imcontour(D2)

subplot(2, 2, 3), subimage(mat2gray(D3)), title('Chessboard Distance')

hold on, imcontour(D3)

subplot(2, 2, 4), subimage(mat2gray(D4)), title('Quasi-Euclidean Distance')

hold on, imcontour(D4)


P a g e | 10

Explanation :-

1. Creating a Binary Image (bw):


o bw = zeros(200, 200);
▪ This line initializes a 200x200 matrix called bw with all elements set to
zero.
▪ The purpose of this matrix is to create a binary image where certain
pixels will be set to 1 (white) to represent objects.
2. Setting Pixels in bw:
o bw(50, 50) = 1;
▪ Sets the pixel at row 50 and column 50 to 1 (white).
o bw(50, 150) = 1;
▪ Sets the pixel at row 50 and column 150 to 1 (white).
o bw(150, 100) = 1;
▪ Sets the pixel at row 150 and column 100 to 1 (white).
▪ These three lines create a simple pattern of white pixels in the bw matrix.
3. Distance Transformations:
o D1 = bwdist(bw, 'euclidean');
▪ Computes the Euclidean distance transform of the binary image bw.
▪ The result, stored in D1, represents the distance from each pixel to the
nearest white (non-zero) pixel.
o D2 = bwdist(bw, 'cityblock');
▪ Computes the cityblock (Manhattan) distance transform of bw.
▪ Similar to D1, it calculates distances to the nearest white pixel using a
different metric.
o D3 = bwdist(bw, 'chessboard');
▪ Computes the chessboard (Chebyshev) distance transform of bw.
▪ Again, it measures distances to the nearest white pixel using a different
metric.
o D4 = bwdist(bw, 'quasi-euclidean');
▪ Computes the quasi-Euclidean distance transform of bw.
▪ This method combines aspects of both Euclidean and cityblock
distances.
4. Displaying the Results:
o figure
▪ Creates a new figure window for displaying the distance transform
results.
o subplot(2, 2, 1), subimage(mat2gray(D1)), title('Euclidean
Distance')
▪ Sets up a subplot grid with 2 rows and 2 columns.
▪ Displays the Euclidean distance transform (D1) as an image
using mat2gray to normalize the values.
▪ Adds a title to this subplot.
o Similar lines follow for the other three distance transforms (D2, D3, and D4).
P a g e | 11

OUTPUT :-
P a g e | 12

Q 4 . Gray Level Transformation :-


There are three basic gray level transformation:

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 :-

1. Reading an Image (r = imread('cameraman.tif');):


o This line reads an image file named “cameraman.tif” and stores it in the
variable r.
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 r.
2. Creating a Copy of the Image (s = r;):
o This line creates a copy of the image stored in r and assigns it to the variable s.
o Both r and s now refer to the same image data in memory.
P a g e | 13

3. Setting Up Subplots (subplot(1,2,1); and subplot(1,2,2);):


o These lines set up a 1x2 grid of subplots (side by side).
o The first subplot (left side) will be used to display the original image (r), and the
second subplot (right side) will display the transformed image (s).
4. Displaying the Original Image (imshow(r);):
o This line displays the image stored in the variable r.
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.
5. Setting the Title for the First Subplot (title('Original Image');):
o This line adds a title to the first subplot (left side).
o The title is set to “Original Image.”
6. Displaying the Copied Image (imshow(s);):
o This line displays the image stored in the variable s.
o Since s is a copy of r, it will show the same image as the original.
7. Setting the Title for the Second Subplot (title('Identity Transformation');):
o This line adds a title to the second subplot (right side).
o The title is set to “Identity Transformation.”
o Note that in this case, there is no actual transformation applied; it’s just a copy of
the original image.

Output :-
P a g e | 14

2. Logarithmic Transformation

r = imread('Data/Concrete Crack Images/00001.jpg');

r = im2double(r);

c = 1;

s = c * log(r+1);

subplot(1,2,1), imshow(r), title('Original image');

subplot(1,2,2), imshow(s), title('Log Image');

Explanation :-

1. Reading an Image (r = imread('Data/Concrete Crack Images/00001.jpg');):


o This line reads an image file named “00001.jpg” from the specified path
(“Data/Concrete Crack Images/”) and stores it in the variable r.
o The imread function is commonly used in MATLAB or Octave to read image files
(such as JPEG, PNG, or TIFF) into an array.
o In this case, it loads the image data from the file “00001.jpg” and assigns it to the
variable r.
2. Converting Image to Double Precision (r = im2double(r);):
o The im2double function converts the image data in r from its original data type
(usually uint8 for images) to double precision.
o Double precision allows for more accurate mathematical operations and a wider
range of pixel values (from 0.0 to 1.0).
o The result is stored back in the variable r.
3. Setting a Scaling Factor (c = 1;):
o This line assigns the value 1 to the variable c.
o The purpose of c is to act as a scaling factor for the subsequent logarithmic
transformation.
4. Logarithmic Transformation (s = c * log(r+1);):
o The expression log(r+1) computes the natural logarithm of each pixel value in
the image r, after adding 1 to avoid taking the logarithm of zero.
o The result of this operation is stored in the variable s.
o Multiplying by c (which is 1 in this case) doesn’t change the values; it’s effectively
an identity operation.
5. Displaying the Results in Subplots:
o subplot(1,2,1), imshow(r), title('Original image');
▪ Sets up a subplot grid with 1 row and 2 columns.
▪ Displays the original image (r) in the left subplot using imshow.
▪ Adds the title “Original image” to this subplot.
o subplot(1,2,2), imshow(s), title('Log Image');
▪ Displays the transformed image (s) in the right subplot.
▪ Adds the title “Log Image” to this subplot.
P a g e | 15

Output :-

3. Power-Law Transformation

input_image = imread('Data/Concrete Crack Images/00001.jpg');

input_image = im2double(input_image);

gamma = 0.4;

power_law_image = input_image .^ gamma;

subplot(1, 2, 1), imshow(input_image), title('Original Image');

subplot(1, 2, 2), imshow(power_law_image), title('Power Law Transformed Image');

Explanation :-
P a g e | 16

1. Reading an Image (input_image = imread('Data/Concrete Crack


Images/00001.jpg');):
o This line reads an image file named “00001.jpg” from the specified path
(“Data/Concrete Crack Images/”) and stores it in the variable input_image.
o The imread function is commonly used in MATLAB or Octave to read image files
(such as JPEG, PNG, or TIFF) into an array.
o In this case, it loads the image data from the file “00001.jpg” and assigns it to the
variable input_image.
2. Converting Image to Double Precision (input_image = im2double(input_image);):
o The im2double function converts the image data in input_image from its
original data type (usually uint8 for images) to double precision.
o Double precision allows for more accurate mathematical operations and a wider
range of pixel values (from 0.0 to 1.0).
o The result is stored back in the variable input_image.
3. Setting the Gamma Value (gamma = 0.4;):
o This line assigns the value 0.4 to the variable gamma.
o The gamma value is a parameter used in power-law (gamma) transformations to
adjust the contrast of an image.
4. Power-Law Transformation (power_law_image = input_image .^ gamma;):
o The expression input_image .^ gamma raises each pixel value in the
image input_image to the power of gamma.
o This operation effectively adjusts the pixel intensities based on the specified
gamma value.
o The result of this operation is stored in the variable power_law_image.
5. Displaying the Results in Subplots:
o subplot(1, 2, 1), imshow(input_image), title('Original Image');
▪ Sets up a subplot grid with 1 row and 2 columns.
▪ Displays the original image (input_image) in the left subplot
using imshow.
▪ Adds the title “Original Image” to this subplot.
o subplot(1, 2, 2), imshow(power_law_image), title('Power Law
Transformed Image');
▪ Displays the transformed image (power_law_image) in the right subplot.
▪ Adds the title “Power Law Transformed Image” to this subplot.

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

subplot(1,4,1:2), imhist(l),title('Original Histogram');

subplot(1,4,3:4), imhist(n),title('Normalized Image');

Explanation :-

1. Reading an Image (l = imread('rice.png');):


P a g e | 18

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

subplot(1,4,1:2), imhist(i),title('Original Histogram');

subplot(1,4,3:4), imhist(k),title('Normalized Image');

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:-

I = imread('Data/Concrete Crack Images/00001.jpg');

K = histeq(I);

figure

subplot(1,4,1:2), imshow(I),title('Original Image');

subplot(1,4,3:4), imshow(K),title('Equalized Image');

figure

subplot(1,4,1:2), imhist(I),title('Original Histogram');

subplot(1,4,3:4), imhist(K),title('Equalized Histogram');

Explanation :-

1. I = imread('Data/Concrete Crack Images/00001.jpg');


o This line reads an image from the file path 'Data/Concrete Crack
Images/00001.jpg' and assigns it to the variable I. The image is loaded into
memory for further processing.
2. K = histeq(I);
o The histeq function enhances the contrast of the grayscale image stored in I. It
transforms the histogram of the input image so that the histogram of the output
image (K) becomes approximately flat. In other words, it redistributes the pixel
intensities to improve visibility and enhance details.
3. figure
o This command creates a new figure window for displaying the images side by
side.
P a g e | 23

4. subplot(1,4,1:2), imshow(I),title('Original Image');


o This line sets up a subplot within the figure. It specifies that the subplot will have
1 row and 4 columns, and the current position is the first two columns (1 and 2).
o imshow(I) displays the original image (I) in the specified subplot.
o title('Original Image') adds a title to the subplot, labeling it as the original
image.
5. subplot(1,4,3:4), imshow(K),title('Equalized Image');
o Similar to the previous line, this sets up another subplot in the same figure.
o The current position is the last two columns (3 and 4).
o imshow(K) displays the equalized image (K) in this subplot.
o title('Equalized Image') adds a title to this subplot, indicating that it
shows the image after histogram equalization.

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)

title("RGB Image with Color Cast")

imshow(Ref)

title("Reference Grayscale Image")

imshow(B)

title("Histogram Matched RGB Image")

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

You might also like