0% found this document useful (0 votes)
19 views

Computer Vision

This document contains the code and explanations for 6 MATLAB functions related to image processing and computer vision. The functions include converting images between color and grayscale, computing histograms, applying histogram equalization, and using lookup tables to transform pixel intensities.

Uploaded by

Did you KNOW
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Computer Vision

This document contains the code and explanations for 6 MATLAB functions related to image processing and computer vision. The functions include converting images between color and grayscale, computing histograms, applying histogram equalization, and using lookup tables to transform pixel intensities.

Uploaded by

Did you KNOW
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Chartage University

Higher Institute of applied Sciences and


Technology of Mateur

Computer Vision
Report
Realized by: Bejaoui Ahmed

Semester 2 / 2024
Functions

1/ Color2gray
function I = color2gray(C)
[h, w, ~] = size(C);
I = zeros(h, w, 'uint8');

for i = 1:h
for j = 1:w
I(i, j) = ( C(i, j, 1) + 0.5870
* C(i, j, 2) + 0.1140 * C(i, j, 3))/3;
end
end
I=uint8(I);
end

This MATLAB function converts a color image represented in RGB format into a grayscale image.

Input Arguments:

C: A 3-dimensional array representing the input color image in RGB format. The dimensions are (height, width, 3), where the
third dimension corresponds to the color channels (Red, Green, and Blue). Each color channel contains intensity values
ranging from 0 to 255.

Output Arguments:

 I: A 2-dimensional array representing the output grayscale image. The dimensions are the same as the input color image
(height, width). Each pixel in the grayscale image contains a single intensity value representing the brightness.
Algorithm:

1. Retrieve the height (h) and width (w) of the input color image C.
2. Initialize an output grayscale image I as a 2-dimensional array of zeros with dimensions (h, w).
3. Iterate over each pixel (i, j) in the input color image.
4. Calculate the luminance value for each pixel using the formula:
5.
6. I(i, j) = (C(i, j, 1) + 0.5870 * C(i, j, 2) + 0.1140 * C(i, j, 3))/3;
7. Where R, G, and B are the red, green, and blue intensity values of the pixel, respectively.
8. Assign the calculated luminance value to the corresponding pixel (i, j) in the output grayscale image I.
9. Convert the output grayscale image I to an unsigned 8-bit integer (uint8) data type.

2/ Gray2Color
function C = gray2color(I)
[h, w] = size(I);

for i = 1:h
for j = 1:w
C(i, j, 1) = I(i, j);
C(i, j, 2) = I(i, j);
C(i, j, 3) = I(i, j);
end
end
end

This MATLAB function converts a grayscale image into a color image.

Input Arguments:

 I: A 2-dimensional array representing the input grayscale image. The dimensions are (height, width), and each pixel contains a
single intensity value representing the brightness.
Output Arguments:

 C: A 3-dimensional array representing the output color image in RGB format. The dimensions are (height, width, 3), where the
third dimension corresponds to the color channels (Red, Green, and Blue). Each color channel contains intensity values ranging
from 0 to 255.

Algorithm:

1. Retrieve the height (h) and width (w) of the input grayscale image I.
2. Initialize an output color image C as a 3-dimensional array of zeros with dimensions (h, w, 3).
3. Iterate over each pixel (i, j) in the input grayscale image.
4. Assign the intensity value of the pixel to all three color channels (Red, Green, and Blue) of the corresponding pixel (i, j) in the
output color image C.
5. Repeat steps 3-4 for all pixels in the grayscale image.
6. Return the output color image C.

3/ Histogram
function H = Histogram(I)
[h,w]=size(I);
for l=1:256;
H(l)=0;
end
I2=double(I);
for i=1:h;
for j=1:w;
H(I2(i,j)+1)=H(I2(i,j)+1)+1;
end
end
end

This MATLAB function computes the histogram of a grayscale image. The histogram shows the frequency of occurrence of each
pixel intensity value in the image.
Input Arguments:

 I: A 2-dimensional array representing the input grayscale image. The dimensions are (height, width), and each pixel contains a
single intensity value representing the brightness.

Output Arguments:

 H: A 1-dimensional array representing the histogram of the input grayscale image. The array H has 256 elements, where H(i)
stores the frequency of occurrence of intensity i-1 in the image.

Algorithm:

1. Retrieve the height (h) and width (w) of the input grayscale image I.
2. Initialize an array H of size 256 to store the histogram. Each element H(i) represents the frequency of occurrence of intensity i-1
in the image. Initialize all elements of H to 0.
3. Convert the input grayscale image I to double precision (double) to handle intensity values beyond the range [0, 255].
4. Iterate over each pixel (i, j) in the input grayscale image.
5. Increment the histogram bin corresponding to the intensity value of the pixel (i, j) by 1. Since MATLAB uses 1-based indexing,
the intensity values are incremented by 1 in the histogram.
6. Repeat steps 4-5 for all pixels in the grayscale image.
7. Return the computed histogram H.

4/inHistogram
function IH = inHistogram(H)
IH=zeros(256,256);
m=max(H);
for j=1:256
n=round((200*H(j))/m);
if(n>0)
for i=256:-1:256-n
IH(i,j)=255;
end
end
end
IH=uint8(IH);
end
This MATLAB function generates an intensity histogram image based on the histogram data provided. The resulting image
visualizes the intensity distribution using grayscale intensity levels, with brighter regions indicating higher frequency of
occurrence.

Input Arguments:

H: A 1-dimensional array representing the histogram of a grayscale image. The array H should have 256 elements, where H(i)
stores the frequency of occurrence of intensity i-1 in the image.

Output Arguments:

IH: A 2-dimensional array representing the intensity histogram image. The dimensions are (256, 256), where each pixel
represents a grayscale intensity level. Brighter pixels indicate higher frequency of occurrence of that intensity level.

Algorithm:

Initialize a 2-dimensional array IH of size (256, 256) to store the intensity histogram image.
Find the maximum frequency m in the input histogram H.
Iterate over each intensity level j from 1 to 256.
Calculate the normalized height n of the column corresponding to intensity level j. Normalize the frequency to fit within the range
[0, 200] for visualization purposes.
For each intensity level, set the top n rows in the corresponding column of IH to the maximum intensity value (255), representing
the frequency of occurrence of that intensity level.
Repeat steps 3-5 for all intensity levels.
Convert the intensity histogram image IH to an unsigned 8-bit integer (uint8) data type.
5/ HistogramEqualization
function IE = HistogramEqualization(I)
H=Histogram(I);
HC(1)=H(1);
for k=2:256
HC(k)=HC(k-1)+H(k);
end
Alpha=255/HC(256);
for k=1:256
LUT(K)=uint8(round(Alpha*HC(k)));
end
IE=uint8(TransformationLUT(I,LUT));

end

Input Arguments:

 I: A 2-dimensional array representing the grayscale input image. Each element in the array stores the intensity value of a pixel in
the range [0, 255].

Output Arguments:

 IE: A 2-dimensional array representing the histogram-equalized grayscale image. Each element in the array stores the intensity
value of a pixel in the range [0, 255].

Algorithm:

1. Compute the histogram of the input image using the Histogram function, which returns the frequency distribution of pixel
intensities.
2. Compute the cumulative histogram (CH) of the input image by accumulating the histogram values.
3. Calculate the scaling factor Alpha by dividing 255 by the maximum value of the cumulative histogram (HC(256)).
4. Construct a lookup table (LUT) to map the input pixel intensities to their histogram-equalized values. This is done by applying
the scaling factor to each cumulative histogram value and rounding the result to the nearest integer.
5. Use the constructed LUT to transform the input image using the TransformationLUT function, which applies the histogram-
equalization transformation to each pixel intensity.
6. Convert the resulting image to an unsigned 8-bit integer data type using the uint8 function to ensure that pixel values are within
the valid range [0, 255].
7. Return the histogram-equalized image IE.

6/ TransformationLUT
function IT = TransformationLUT(I,LUT)
[h,w]=size(I);
I2=double(I);
for i=1:h;
for j=1:w;
IT(i,j)=LUT(I2(i,j)+1);
end
end

end

This function applies a lookup table (LUT) transformation to a grayscale image. It takes a grayscale image I and a lookup table
LUT as input and returns the transformed image IT.

Input Arguments:

 I: A 2-dimensional array representing the grayscale input image. Each element in the array stores the intensity value of a pixel in
the range [0, 255].
 LUT: A 1-dimensional array representing the lookup table. The array LUT maps the input pixel intensities to their transformed
values. The index of the array corresponds to the input intensity value, and the value at each index represents the transformed
intensity value.

Output Arguments:

 IT: A 2-dimensional array representing the transformed grayscale image. Each element in the array stores the intensity value of a
pixel in the range [0, 255].

Algorithm:
1. Get the dimensions of the input image I.
2. Convert the input image I to a double-precision format to allow for accurate indexing.
3. Iterate over each pixel of the input image.
4. For each pixel, use the intensity value as an index to access the corresponding transformed intensity value from the lookup
table LUT.
5. Assign the transformed intensity value to the corresponding pixel in the output image IT.
6. Repeat steps 3-5 for all pixels in the input image.
7. Return the transformed image IT.

7/ Negative
function IN = Negative(I)
[h,w]=size(I);
I2=double(I);
for i=1:h;
for j=1:w;

IN(i,j)=255-I2(i,j);
end
end
IN=uint8(IN);

End

This function computes the negative of a grayscale image. It takes a grayscale image I as input and returns the negative image IN.

Input Arguments:

 I: A 2-dimensional array representing the grayscale input image. Each element in the array stores the intensity value of a pixel in
the range [0, 255].

Output Arguments:

 IN: A 2-dimensional array representing the negative grayscale image. Each element in the array stores the intensity value of a
pixel in the range [0, 255].
Algorithm:

1. Get the dimensions of the input image I.


2. Convert the input image I to a double-precision format to allow for accurate arithmetic operations.
3. Iterate over each pixel of the input image.
4. For each pixel, subtract the intensity value from 255 to compute the negative intensity value.
5. Assign the negative intensity value to the corresponding pixel in the output image IN.
6. Repeat steps 3-5 for all pixels in the input image.
7. Convert the resulting image to an unsigned 8-bit integer data type using the uint8 function to ensure that pixel values are
within the valid range [0, 255].
8. Return the negative image IN.

7/ Contraststreching
function IR = contraststreching(I,a,b)
I2=double(I);
Imin=min(I2(:));
Imax=max(I2(:));
Alpha=(b-a)/(Imax-Imin);
Beta=a-Alpha*Imin;
[h,w]=size(I);
for i=1:h
for j=1:w
IR(i,j)=round(Alpha*I2(i,j)+Beta);
end
end
IR=uint8(IR);

end

The contraststreching function applies contrast stretching to an input grayscale image I by linearly scaling its pixel values to a
new range defined by a and b. This technique is used to enhance the contrast of an image by spreading out the intensity values
over a wider range.

Input Arguments:

 I: Input grayscale image (2D matrix).


 a: Lower bound of the desired output intensity range after contrast stretching.
 b: Upper bound of the desired output intensity range after contrast stretching.
Output Argument:

 IR: Contrast-stretched image (2D matrix of the same size as I) with pixel values scaled to the range [a, b].

Algorithm:

1. Convert the input image I to a double precision representation (I2) to handle computations with fractional values.
2. Find the minimum (Imin) and maximum (Imax) pixel values in the double image I2.
3. Calculate the transformation parameters Alpha and Beta:
4. Iterate over each pixel (i, j) of the input image:
i. Compute the contrast-stretched pixel value using the linear transformation:
ii. Ensure the result is within the range [a, b].
5. Convert the resulting contrast-stretched image IR back to uint8 format to represent pixel values as integers in the range
[0, 255].
8/ Binarization
FUNCTION IB = BINARIZATION(I, SEUIL)
LUT = ZEROS(256, 1);
FOR K = 1:SEUIL
LUT(K) = 0;
END
FOR K = SEUIL+1:256
LUT(K) = 255;
END

IB = TRANSFORMATIONLUT(I, LUT);
END

The Binarization function converts a grayscale image into a binary image based on a specified threshold value.

Input Arguments:
 I: The input grayscale image to be binarized.
 Seuil: The threshold value used for binarization. It determines the intensity value that separates black and white pixels.
Outputs Arguments:

 IB: The binary image obtained after binarization, where pixel values are either 0 or 255.

Algorithm:

1. Apply Lookup Table Transformation:


 Use the TransformationLUT function to apply the lookup table transformation to the input image I.
 This transformation replaces each pixel intensity value in I with its corresponding value in the LUT, effectively binarizing
the image based on the specified threshold.
2. Output Binarized Image:
 Return the binarized image IB as the output of the function.

9/ Noise
function N = Noise(I,p)

[h,w]=size(I);
N=I;
for i=1:h
for j=1:w
v=round(rand(1)*p);
if v==0
N(i,j)=0;
else
if v==p
N(i,j)=255
end
end
end
N=uint8(N);
end

The Noise function generates salt-and-pepper noise in a given grayscale image I with a specified probability p.

Inputs Arguments:

 I: The input grayscale image.


 p: The probability of occurrence for the salt or pepper noise. It determines the likelihood of a pixel being corrupted.
Outputs Arguments:

 N: The noisy image generated by adding salt-and-pepper noise to the input image.

Algorithm:

1. Create an empty matrix N of the same size as the input image I to store the noisy image.
2. Iterate through each pixel (i, j) in the input image I.
3. Generate a random integer v between 0 and p.
4. If v is equal to 0, set the corresponding pixel in N to 0 (pepper noise).
5. If v is equal to p, set the corresponding pixel in N to 255 (salt noise).
6. If v is neither 0 nor p, leave the corresponding pixel in N unchanged.
7. Convert the noisy image N to the appropriate data type (e.g., uint8) to ensure valid pixel values.
8. Return the noisy image N as the output of the function.

10/ Convol
function IF = Convol(IO,M)
I=double(IO);
[n,m]=size(M);
[h,w]=size(I);
IF=zeros(h,w);
d=round(n/2);
for x=d:h-d+1
for y=d:w-d+1
s=0;
for i=-d+1:d-1
for j=-d+1:d-1
s=s+I(x+i,y+j)*M(i+d,j+d);
end
end
IF(x,y)=s
end
end
end

The Convol function applies 2D convolution to an input grayscale image using a specified convolution Filter.
Inputs Arguments:

 IO: The input grayscale image to be convolved.


 M: The convolution kernel or filter.

Outputs Arguments:

 IF: The convolved image obtained after applying the convolution operation.

Algorithm:

1. Convert the input image IO to double precision to allow for floating-point arithmetic.
2. Create an empty matrix IF of the same size as the input image to store the convolved image.
3. Iterate through each pixel (x, y) in the input image IO.
4. For each pixel, iterate through each element (i, j) in the convolution kernel M.
5. Compute the weighted sum of pixel values under the kernel by multiplying each pixel value in the neighborhood of
(x, y) with the corresponding kernel coefficient and accumulating the results.
6. Assign the computed sum to the corresponding pixel (x, y) in the output image IF.
7. Return the convolved image IF as the output of the function.

11/ Gradient
function IG = Gradient(I,Wx,Wy)
Ix=Convol(I,Wx);
Iy=Convol(I, Wy);
[h,Wy]=size(I);
for i=1:h
for j=1:w
IG(i,j)=sqrt(I*(i,j)*Ix(i,j)+Iy(i,j)*Iy(i,j));
end
end

end

The Gradient function computes the magnitude of the gradient of an input image I using convolution with the horizontal (Wx)
and vertical (Wy) derivative kernels.
Input Arguments:

I: Input grayscale image (2D matrix).

Wx: Kernel for computing the horizontal derivative (2D matrix).

Wy: Kernel for computing the vertical derivative (2D matrix).

Output Argument:

 IG: Gradient magnitude of the input image I (2D matrix of the same size as I).

Algorithm:

1. Use the Convol function (assuming it correctly performs convolution) to compute the horizontal derivative (Ix) and vertical
derivative (Iy) of the input image I using the kernels Wx and Wy respectively.
2. Initialize an output matrix IG of zeros with the same dimensions as I to store the gradient magnitude.
3. Iterate over each pixel (i, j) of the image:
4. Compute the gradient magnitude at pixel (i, j) using the formula:
5. Store the computed magnitude in the corresponding location in IG.
6. Ensure that dimensions and data types are handled properly throughout the function to avoid errors.

12/ SOBEL
function Is = SOBEL(I)

Wx=(1/4)*[-1 0 1 ;-2 0 2; -1 0 1];


Wy=(1/4)*[-1 -2 -1 ; 0 0 0; 1 2 1];
Is=Gradient(I,Wx,Wy);
Is=uint8(Is)

end
The SOBEL function applies the Sobel edge detection operator to an input grayscale image I using specific Sobel kernels for
horizontal (Wx) and vertical (Wy) derivatives. It then computes the gradient magnitude using the Gradient function.

Input Argument:
 I: Input grayscale image (2D matrix).

Output Argument:

 Is: Image representing the gradient magnitude after Sobel edge detection (2D matrix of the same size as I).

Algorithm:

1. Define the specific Sobel kernels Wx and Wy for computing the horizontal and vertical derivatives respectively.
2. Use the Gradient function with the defined Sobel kernels (Wx and Wy) to compute the gradient magnitude of the
input image I.
3. Convert the resulting gradient magnitude image Is to uint8 format to represent pixel values as integers in the range
[0, 255].

13/ medianfilter
function F = medianfilter(I)
[h,w]=size(I);
F=zeros(h,w);
for x=2:h-1
for y=2w-1
M(1:3,1:3)=I(x-1:x+1,y-1:y+1);
F(x,y)=median(M(:));
end
end
F=uint8(F)

end

The medianfilter function applies a median filter to an input grayscale image I to reduce noise. The median filter replaces each
pixel value with the median value of its neighboring pixels within a defined window size.
Input Argument:

 I: Input grayscale image (2D matrix).

Output Argument:

 F: Filtered image after applying the median filter (2D matrix of the same size as I).

Algorithm:

1. Initialize an output matrix F of zeros with the same dimensions as the input image I to store the filtered image.
2. Iterate over each pixel (x, y) of the input image I, excluding the boundary pixels:
 Extract a local neighborhood (3x3 window) centered at pixel (x, y) from the input image I.
 Compute the median value of the pixel values in the neighborhood.
 Replace the pixel value at (x, y) in the output matrix F with the computed median value.
3. Edge pixels are not processed in the filtering loop due to the use of a 3x3 window.
4. Convert the resulting filtered image F to uint8 format to represent pixel values as integers in the range [0, 255].

You might also like