1.
DISPLAY OF GRAY SCALE IMAGE
Aim:
To read and display a grayscale image and to converted an RGB image into a grayscale
image using MATLAB.
Procedure:
[Link] an grayscale image
• Use the imread() function to read a grayscale image file.
• Example img = imread('[Link]');
Input:
• Display the image using the imshow () function.
• Add a title to image using the title() function.
1
2. Converting an RGB image to grayscale:
• Read an RGB image using imread()
• Example rgbImage = imread([Link]’);
Input:
• Converted it to grayscale using the rgb2gray() function
RgbImage(): rgbImage is a 3D array (height *width*3)containing re,green,and blue
channels.
Rgb2gray(): converts the RGBi mage to a single grayscale channel by applying a
weighted sum of the RGB value (based on human eye sensitivity more weight to green,less to
blue).
2
Source code:
A) %Read a grayscale image
grayImage = imread('[Link]');
%Display the image
imshow(grayImage);
B) %Add a title
title('Grayscale Image');
rgbImage = imread([Link]');
grayImage = rgb2gray(rgbImage);
imshow(grayImage);
title('Converted Grayscale Image');
Output:
3
Result:
The MATLAB program successfully performed grayscale image converted into
rgbgray scale image.
4
2. HISTOGRAM EQUALIZATION
Aim:
To perform histogram equalization on a grayscale image using MATLAB, both with
the built-in function and by manually implementing the algorithm, and to display the original
and equalized images and their histograms.
Procedure:
Using Built-in Function (histeq)
1. Read the Grayscale Image:
• Use imread() to load the grayscale image(e.g., '[Link]') into MATLAB.
Input:
2. Perform Histogram Equalization:
• Use histeq() to complete the histogram equalized image.
3. Display Results:
• Use imshow() to display the original and equalized images.
• Use imhist() to display their histograms.
• Use title() to label each subplot for clarity.
5
Source Code:
%Read grayscale Image
grayImage=imread('[Link]');
%perform histogram equalization
equalizedImage=histeq(grayImage);
%Display original & equalized images
figure;
subplot(2,2,1);imshow(grayImage);title('Original Grayscale Image');
subplot(2,2,2);imhist(grayImage);title('Original Histogram');
subplot(2,2,3);imshow(equalizedImage);title('EqualizedImage');
subplot(2,2,4);imhist(equalizedImage);title('Equalizedhistogram');
6
Output:
Result:
The grayscale image after applying histogram equalization shows better contrast
enhancement compared to the original image.
The histogram of the equalized image is more uniformly distributed across the gray
levels, thus improving the visibility of details in both dark and bright regions.
7
3. NON-LINEAR FILTERING
Aim:
To read a grayscale image, add salt-and-pepper noise, and remove the noise using a
3×3 median filter and to demonstrate the concept of Non-Linear Filters such as median,
min, max, or custom function.
Procedure:
1. Read the input image:
• Load a grayscale image.
• example: [Link]
Input:
[Link] noise to the image:
• Introduce salt & pepper noise with a noise density of 0.05.
• Salt = white pixels (value = 255), Pepper = black pixels (value = 0).
[Link] a Non-Linear filter (Median filter):
• Use a 3×3 median filter to remove noise.
8
• The median filter replaces each pixel with the median of its neighbors.
• Very effective for removing salt & pepper noise while preserving edges.
[Link] results:
• Display original image, noisy image, and filtered image side by side using
imshow() function for comparison.
• Use imshow() function to display the images and title() function to display the
titles.
Source code:
%read a grayscale image(with some noise,ideally)
grayImage=imread(‘[Link]’);
%add salt & pepper noise
noisyImage=imnoise(grayImage,'salt & pepper',0.05);
%Apply a 3*3 median filter
filteredImage=medfilt2(noisyImage,[3 3]);
%Display results
figure;
subplot(1,3,1);imshow(grayImage);title('Original Image');
subplot(1,3,2);imshow(noisyImage);title('noisy Image');
subplot(1,3,3);imshow(filteredImage);title('Median filtered Image');
Custom Nonlinear Filter (Min, Max, or Custom Funtion)
9
Output:
Result:
The grayscale image was successfully corrupted with salt-and-pepper noise and
subsequently restored using a 3×3 median filter.
The filtering process effectively suppressed the noise while preserving the important
edge details of the image.
10
4. EDGE DETECTION USING OPERATORS
Aim:
To perform edge detection on a given image using different operators Sobel, Prewitt
and Canny in MATLAB and compare their results.
_____________________________________________________________________________
Procedure:
1. Read the input Image:
• Load the given input image into MATLAB using the imread() function.
• Example: img=imread(‘[Link]’);
Input:
2. Convert to Grayscale:
• If the image is in color (RGB), convert it to grayscale using rgb2gray().
Since edge detection works primarily on intensity values.
• Example: gray_img = rgb2gray(img);
11
3. Apply Edge Detection Operators:
• Use the built in MATLAB edge() function with different methods:
• Sobel Operator: Detects edges based on gradient magnitude.
Sobel_edge = edge(gray_img, ‘sobel’);
• The sobel operator applies two convolution masks(one for horizontal
changes, one for vertical changes).
• It computes the gradient magnitude at each pixel.
• It applies a threshold to decide whether a pixel is part of an edge.
• The result(sobel_edge) is a binary image.
➢ 1(white)→ edge pixel
➢ 0(black) → non-edge pixel
• Prewitt Operator: Similar to sobel but with a differet convolution mask..
Prewitt_edge = edge(gray_img, ‘prewitt’ );
• Prewitt operator calculates edges by finding the rate of intensity
change in the image using horizontal and vertical masks.
• It highlights regions where there is a sharp intensity change, which
usually corresponds to object boundaries.
• Prewitt_edge stress the binary output image.
➢ 1(white pixels) → detected edges
➢ 0(black pixels) → non-edges
• Canny Operator: Uses Gaussian smoothing, gradient computation and
non-maximum suppression for precise edge detection.
canny_edge = edge(gray_img, ‘canny’);
• Canny operator Applies Gaussian filter for smooths image and
reduce noise.
• It Computes Intensity gradient(magnitude & direction). Uses non-
maximum suppression for thins the edges to 1-pixel width.
12
• It Applies double thresholding (high&low thresholds) to classify
strong and weak edges.
• It performs edge tracking by hysteresis for connects weak edges to
strong ones if they are continuous.
• canny_edge stores the binary edge-detected image.
➢ 1(white pixels)→ edges detected
➢ 0(black pixels) → background
4. Display the Results:
• Use subplot( ) to display multiple results in one figure window.
• Use imshow( ) to display each edge-detected image.
• Add titles using title( ) for clear labelling.
5. Observation:
• Compare the edge detection outputs visually and note the differences in edge
sharpness, noise handling and continuity.
13
Source Code:
%Program for edge detection using various operators
%Read and convert to grayscale
img=imread(‘[Link]’);
gray_img=rgb2gray(img);
%Apply different edge detection operators
sobel_edge = edge(gray_img, ‘sobel’);
prewitt_edge = edge(gray_img, ‘prewitt’);
canny_edge = edge(gray_img, ‘canny’);
%Display results
Subplot(1,3,1);
imshow(sobel_edge);
title(‘Sobel Edge’);
Subplot(1,3,2);
imshow(prewitt_edge);
title(‘Prewitt Edge’);
Subplot(1,3,3);
imshow(canny_edge);
title(‘Canny Edge’);
14
Output:
Result:
The MATLAB program successfully performed edge detection using Sobel, Prewitt and
Canny operators.
• Sobel Edge : Produces thicker edges, suitable for detecting strong intensity changes.
• Prewitt Edge: Similar to Sobel but slightly less sensitive to noise.
• Canny Edge : Produces fine, accurate edges with good noise suppression. The
Results were displayed side-by-side for comparison.
15
5. 2-D DFT AND DCT
Aim:
To apply 2D Discrete Fourier Transform (DFT) and 2D Discrete Cosine Transform
(DCT) on a grayscale image and compare their frequency-domain representations using
MATLAB.
Procedure:
1. Read the image :
• Read the image [Link].
• Convert it to grayscale since DFT/DCT are usually applied to single-channel
images.
Input :
16
[Link] to Grayscale:
• Since frequency transforms are usually applied on single-channel data,
rgb2gray() is used.
3. Apply 2D DFT:
• fft2() computes the 2D Fourier Transform.
• fftshift() shifts the zero-frequency component to the center for better
visualization.
• log(abs(...)) is applied to improve visibility (since frequency values vary
widely).
4. Apply 2D DCT:
• dct2() computes the 2D Discrete Cosine Transform of the grayscale image.
• Again, log(abs(...)) is used to visualize values properly.
• It is widely used in image compression (e.g., JPEG).
5. Display Results:
• imshow() is used to display the transforms.
• subplot() arranges the outputs for comparison.
• Titles are added using title().
17
Source code:
%program to perform 2-D DFT and DCT
%Read and convert to grayscale
Img=imread ([Link]');
gray_img = rgb2gray(img);
%Compute 2D FFT
dft_img = fftshift(fft2(double(gray_img)));
%Compute DCT
dct_img = dct2 (double(gray_img));
%Display images
figure;
subplot(1,3,1); imshow(log(abs(dft_img)), []); title('2D DFT');
subplot(1,3,2);imshow(log(abs(dct_img)), []);title('2D DCT');
18
Output :
Result :
The Grayscale Image is displayed in the first subplot.
The 2D DFT shows high-frequency components (edges, fine details) spread out, with
low frequencies concentrated at the center.
The 2D DCT shows energy compacted in the top-left corner, as DCT is energy
compaction-oriented and widely used in image compression (like JPEG).The top-left
coefficients represent low-frequency (important image structure).
19
6. FILTERING IN FREQUENCY DOMAIN
Aim:
To perform low-pass filtering in the frequency domain using Fourier Transform and
observe the effect on the image using MATLAB.
Procedure:
1. Read Image:
• Read the input image using imread() and convert it to grayscale using
rgb2gray().
Input:
2. Fourier Transform:
• Compute the 2D Fourier Transform of the image using fft2().
• fft2():Computes the 2D Fourier Transformof an image, converting it from
the spatial domain (pixel values) to the frequency domain(frequency
components).
20
3. Shift Spectrum:
• Shift the zero-frequency component to the center using fftshift().
• fftshift():Shifts the zero-frequency component of the Fourier transform to the
center of the spectrum.
4. Design Low-Pass Filter:
• Create an Ideal Low-Pass Filter by defining a cutoff frequency D0.
• within radius D0 → set to 1 (passed).
• Pixels outside radius D0 → set to 0 (blocked).
5. Apply Filter:
• Multiply the frequency spectrum of the image with the filter mask.
6. Inverse Fourier Transform :
• Apply ifft2() and ifftshift() to reconstruct the filtered image.
• ifft2():Computes the inverse 2D Fourier Transform, converting a frequency-
domain image back to the spatial domain.
• ifftshift():Reverses the effect of fftshift(), moving the zero-frequency
component back to the top-left corner.
[Link] Results:
• Show the original grayscale image and the low-pass filtered image using
imshow().
21
Source code:
%Program for filtering in frequency domain
%Read and convert to grayscale
img=imread(‘[Link]’);
gray_img=rgb2gray(img);
%Compute FFT
F=fft2(double(gray_img));
F_shifted=fftshift(F);
%Create a low-pass filter
[M,N]=size(F_shifted);
[X,Y]=meshgrid(1:N,1:M);
centerX=round(N/2);
centerY =round(M/2);
D=sqrt((X-centerX).^2+(Y-centerY).^2);
D0=50;
%cutoff frequency
H=double(D<=D0);
%Apply filter and inverse FFT
G=F_shifted.*H;
filtered_img=real(ifft2(ifftshift(G)));
%Displayimages
figure;
subplot(1,2,1);imshow(gray_img);title('Original Images');
subplot(1,2,2);imshow(uint8(filtered_img));title('Low-Pass Filtered Images');
22
Output:
Result :
The image was successfully filtered in the frequency domain using MATLAB
• The original image retained its full details.
• The low-pass filtered image appeared smoother, with fine details and edges blurre
23
7. DISPLAY OF COLOUR IMAGES
Aim:
To read and display a colour image using MATLAB program.
Procedure:
1. Read the Image:
• Use the imread function to read a colour image file (e.g., ‘[Link]') and store
it in a variable.
Input:
2. Display the Image:
• Use the imshow function to display the image stored in the variable.
3. Set the Title:
• Use the title function to set a title for the displayed image.
4. End the program.
24
Source Code:
%program to display a color image
%read the image
img=imread(‘[Link]’);
%display the images
imshow(img);
title('Color Image');
Output:
Result:
The MATLAB program successfully performed the display of colour image.
25
8. CONVERSION BETWEEN COLOUR SPACES
Aim:
To convert an image from RGB color space into HSV and YCbCr color spaces using
MATLAB.
Procedure:
1. Read the input Image
• Load the image into MATLAB using: imread('[Link]');
Input :
2. Convert to HSV :
• Use the function rgb2hsv() to convert the image from RGB to HSV color space.
3. Convert to YCbCr :
• Use the function rgb2ycbcr() to convert the image from RGB to YCbCr color
space.
26
4. Display the results :
• Display the original RGB image, HSV image, and YCbCr image side by side
using subplot() and imshow().
Source Code :
% Program for Conversion between Colour Spaces
% Read the image
img = imread('[Link]');
% Convert to HSV
hsv_img = rgb2hsv(img);
% Convert to YCbCr
ycbcr_img = rgb2ycbcr(img);
% Display results
subplot(1, 3, 1); imshow(img); title('RGB Image');
subplot(1, 3, 2); imshow(hsv_img); title('HSV Image');
subplot(1, 3, 3); imshow(ycbcr_img); title('YCbCr Image');
27
Output :
Result :
The MATLAB program successfully converted the RGB image into HSV and YCbCr
color spaces and displayed the results.
28
9. DWT OF IMAGES
Aim:
To write a MATLAB program for performing a Discrete Wavelet Transform (DWT)
on a grayscale image using the Haar wavelet and display the approximation and detail
coefficients.
___________________________________________________________________________
Procedure:
1. Read and preprocess the image
• Load the input image ([Link]) into MATLAB.
• Convert the RGB image into a grayscale image for wavelet processing.
Input:
2. Perform Discrete Wavelet Transform
• Apply single-level 2D DWT using the Haar wavelet.
• The image will be decomposed into four sub-bands:
• LL → Approximation (low-frequency part)
• LH → Horizontal detail
• HL → Vertical detail
• HH → Diagonal detail
29
3. Display the results
• Use subplot and imshow to display the four components:
• Approximation (LL) sub-band shows the overall image with reduced
resolution
• Horizontal detail (LH) sub-band highlights horizontal edges.
• Vertical detail (HL) sub-band highlights vertical edges.
• Diagonal detail (HH) sub-band highlights diagonal edges.
Source Code:
%program for Discrete wavelet Transform(DWT)
%Read & Convert to grayscale
img=imread(‘[Link]’);
gray_img=rgb2gray(img);
%Perform single_level DWT
[LL,LH,HL,HH]=dwt2(gray_img,'haar');
%Display results
subplot(2,2,1);imshow(uint8(LL));title('Approximation(LL)');
subplot(2,2,2);imshow(uint8(LH));title('Horizontal(LH)');
subplot(2,2,3);imshow(uint8(HL));title('Vertical(HL)');
subplot(2,2,4);imshow(uint8(HH));title('Diagonal(HH)');
30
Output:
Result:
The program successfully decomposes the input grayscale image into four sub-bands
using the Discrete Wavelet Transform (DWT) with the Haar wavelet.
31