lstDIP Lab Manjj-A)
lstDIP Lab Manjj-A)
lstDIP Lab Manjj-A)
INFORMATION TECHNOLOGY
BHOPAL
LAB MANUAL
Submitted By:
Submitted To:
• Scaling
• Rotation
• Shrinking
• Zooming
8. Write and execute programs for image frequency 10-10-2024
domain filtering
• Spartial resolution
• Intensity resolution
Aim:
Commands:
Color
Basic Display
Image Filtering
Contrast Adjustment
localtonemap Render HDR image for viewing while enhancing local contrast
Image Arithmetic
imsubtract Subtract one image from another or subtract constant from image
Image Segmentation and Analysis
Image Segmentation
activecontour Segment image into foreground and background using active contours
(snakes)
imseggeodesic Segment image into two or three regions using geodesic distance-
based color segmentation
Image Transforms
Theory:
Conclusion:
Program for reading and displaying image using MATLAB is executed.
Tutorial:
1.) Write a MATLAB command for the following
Aim:
Theory:
image1=imread('YOGESHIMG.jp');
image2=rgb2gray(image1);
[r c d]=size(image1);
z=zeros(r,c); tempr=image1;
tempr(:,:,2)=z;
tempr(:,:,3)=z;
imshow(tempr)
tempg=image1;
tempg(:,:,1)=z;
tempg(:,:,3)=z;
imshow(tempg)
tempb=image1;
tempb(:,:,1)=z;
tempb(:,:,2)=z;
imshow(tempb)
Result:
Conclusion:
Tutorial:
1.) Write MATLAB program to display grayscale image using image
“cameraman.tif”.
Theory:
Histogram is bar-graph used to profile the occurrence of each gray level in the
image.Mathematically it is represented as h(rk)=nk.Where rk is kth grey level
and nk is number of pixel having that greylevel.Histogram can tell us whether
image was scannedproperly or not. It gives us idea about tonal distribution
in the image.Histogram equalization can be applied to improveappearance of
the image.Histogram also tells us about objects in the image. Object in an
image have similar gray levels so histogram helps us to select threshold value
for object detection.Histogram can be used for image segmentation.
MATLAB CODE:
close all;
clear all;
myimage = imread('YOGESHIMG.jpg');
if(size(myimage,3) == 3)
myimage = rgb2gray(myimage);
end
subplot(2, 2, 1);
imshow(myimage);
title('Original Image');
subplot(2, 2, 3);
imhist(myimage);
newimage = histeq(myimage);
2, 2);
imshow(newimage);
title('Histogram Equalized Image'); subplot(2, 2, 4);
imhist(newimage);
Result:
Conclusion:
Tutorial:
1.) Take your own photograph in dark area. Improve its appearance using
histogram equalization technique.(Copy and paste your original image,
its histogram, equalized image and its histogram)
EXPERIMENT NO. 5
Theory:
Arithmetic operations like image addition, subtraction, multiplication
and division is possible. If there is two images I1 and I2 then addition of
image can be given by:
MATLAB CODE:
clc close
all
I1 = imread('YOGESHIMG.jp'); I2 =
imread('Background.png'); subplot(2, 2,
1);
imshow(I1);
title('Original image I1');
subplot(2, 2,2); imshow(I2);
title('Original image I2'); I=I1+I2; %
Addition of two images subplot(2, 2, 3);
imshow(I);
title('Addition of image I1+I2'); I=I1-I2;
%Subtraction of two images subplot(2,2,4);
imshow(I);
title('Subtraction of image I1-I2');figure; subplot(2, 2,
1);
imshow(I1);
title('Original image I1'); I=I1+50;
subplot(2, 2, 2);
imshow(I);
title('Bright Image I'); I=I1-
100;
subplot(2, 2, 3); imshow(I);
title('Dark Image I');
M=false(size(I1));
% Set a rectangular region to 1 (true)
M(50:150, 50:150) = true;
%Type casting before multiplication
I=uint8(I1).*uint8(M);
subplot(2, 2, 4); imshow(I);
title('Masked Image I');
Result:
b
Conclusion:
Tutorial:
256
128
EXPERIMENT NO. 6
Aim:To write and execute programs for image logical operations
MATLAB Code:
% Read and convert Image A and B to grayscale
myimageA = imread('YOGESHIMG.jp');
binaryImageA= rgb2gray(myimageA);
myimageB = imread('Background.png');
binaryImageB= rgb2gray(myimageB);
Conclusion:
Tutorial:
1.) Prepare any two images of size 256x256 in paint. Save it in JPEG
format 256 gray levels. Perform logical NOR, NAND operations
between two images. Write program and paste your results.
EXPERIMENT NO. 7
MATLAB Code:
%To rotate given image using standard Matlab function
imrotate() %To transform image using standard Matlab
function imtransform() % using shearing matrix.
x=imread('YOGESHIMG.jpg');
x=rgb2gray(x);
subplot(2,2,1);
imshow(x);
title('Orignial Image');
y=imrotate(x,45,'bilinear','crop');
subplot(2,2,2); imshow(y);
subplot(2,2,3); imshow(y);
y=imrotate(x,-45,'bilinear','crop');
subplot(2,2,4);
imshow(y);
imread('ProfilePhoto.png');
tform = maketform('affine',[1 0 0; .5 1 0; 00
1]);
y = imtransform(x,tform);
figure;
subplot(2,2,1); imshow(x);
title('Orignial Image');
subplot(2,2,2);
imshow(y);
title('Shear in X direction');
y = imtransform(x,tform);
subplot(2,2,3); imshow(y);
title('Shear in Y direction');
y = imtransform(x,tform);
Tutorial:
1.) In above program, modify matrix for geometric transformation and use
imtransform() function for modified matrix. Show the results and your
conclusions.
EXPERIMENT NO. 8
Aim: Write and execute programs for image frequency domain filtering
• Spartial resolution
• Intensity resolution
Theory:
In spatial domain, we perform convolution of filter mask with image data. In
frequency domain, we perform multiplication of Fourier transform of image
data with filter transfer function.
Spatial resolution refers to the number of pixels in an image and
determines the level of detail captured.
Higher spatial resolution provides more detail, whereas lower spatial
resolution can make the image appear pixelated.
MATLAB Code:
Conclusion:
1. Spatial Resolution: Lowering spatial resolution decreases the level of detail in
the image. This can be observed in both the filtered and unfiltered images, where
lower resolutions result in less clarity.
2. Intensity Resolution: Reducing intensity resolution impacts the contrast and
depth, with lower resolutions appearing posterized. Frequency domain filtering
applied to these images demonstrates the limitations in detail that arise with
lower intensity levels.
Tutorial:
1. Experiment with different cutoff frequencies in the low-pass filter to observe
changes in detail for various resolutions.
2. Analyze how different levels of down-sampling and quantization affect the clarity
and frequency representation of images.
EXPERIMENT NO. 9
Aim: Write and execute programs for image frequency domain filtering
Theory:
clear all;
% Read the image, resize it to 256 x 256
% Convert it to grey image and display it
myimg=imread('YOGESHIMG.jpg');
if(size(myimg,3)==3)
myimg=rgb2gray(myimg);
end
myimg = imresize(myimg,[256 256]);
myimg=double(myimg); subplot(2,2,1);
imshow(myimg,[]),title('Original Image');
[M,N] = size(myimg); % Find size
%Preprocessing of the
image
for x=1:M for
y=1:N
myimg1(x,y)=myimg(x,y)*((1)^(x+y));
end
end
end
end
Pass mask');
subplot(2,1,2);imshow(mybandpassmask); title('Band
Pass mask');
Result:
Conclusion:
MATLAB program for frequency domain filtering is executed.
Tutorial:
1.) Instead of following pre-processing step in above program use
fftshift function to shift FFT in the center. See changes in the result
and write conclusion.
%Preprocessing
of the image for
x=1:M
for y=1:N
myimg1(x,y)=myimg(x,y
)*((-
1)^(x+y)); end
end
Theory:
Image segmentation is to subdivide an image into its component regions or objects.
Segmentation should stop when the objects of interest in an application have been
isolated. Basic purpose of segmentation is to partition an image into meaningful
regions for particular application The segmentation is based on measurements
taken from the image and might be grey level, colour, texture, depth or motion.
First order derivative and second order derivative operators can do edge detection.
MATLAB CODE:
clear all;
clc;
if size(data, 3) == 3
data = rgb2gray(data);
end
imshow(data);
figure;
switch K
case 1
filename = 'path_to_your_image.jpg'; % Replace with your image path
data = imread(filename);
if size(data, 3) == 3
data = rgb2gray(data);
end
imshow(data);
case 2
M = [-1 -1 -1; -1 8 -1; -1 -1 -1]; % Mask for point detection
case 3
M = [-1 -1 -1; 2 2 2; -1 -1 -1]; % Mask for horizontal edges
case 4
M = [-1 2 -1; -1 2 -1; -1 2 -1]; % Mask for vertical edges
case 5
M = [-1 -1 2; -1 2 -1; 2 -1 -1]; % Mask for +45 degree diagonal line
case 6
M = [2 -1 -1; -1 2 -1; -1 -1 2]; % Mask for -45 degree diagonal line
case 7
M = [-1 -1 -1; -1 8 -1; -1 -1 -1]; % Rectangle mask
case 8
break;
otherwise
msgbox('Select a proper mask');
continue;
end
outimage = conv2(double(data), double(M), 'same');
figure;
imshow(mat2gray(outimage));
title('Filtered Image');
end
close all;
imwrite(mat2gray(outimage), 'outimage.jpg', 'Quality', 99);
Result:
Conclusion
MATLAB program for edge detection is executed.
Tutorial:
1.) Get mask for “Prewitt”, “Canny”, “Sobel” from the literature and write
MATLAB/SCILAB program for edge detection using 2Dconvolution
clear all;
clc;
if size(data, 3) == 3
data = rgb2gray(data);
end
2.) Write a MATLAB code for edge detection of a grayscale image without using
in-built function of edge detection.
clear all;
clc;
imshow(data);
title('Original Image');
figure;
subplot(1, 2, 1); imshow(edges_prewitt); title('Prewitt Edge Detection');
subplot(1, 2, 2); imshow(edges_sobel); title('Sobel Edge Detection');
EXPERIMENT NO. 11
Aim: Write and execute programs to remove noise using spatial filters
• Understand 1-D and 2-D convolution process
• Use 3x3 Mask for low pass filter and high pass filter
Theory:
Spatial Filtering is sometimes also known as neighborhood processing.
Neighborhood processing is an appropriate name because you define a center point
and perform an operation (or apply a filter) to only those pixels in predetermined
neighborhood of that center point. The result of the operation is one value, which
becomes the value at the center point's location in the modified image. Each point
in the image is processed with its neighbors. The general idea is shown below as a
"sliding filter" that moves throughout the image to calculate the value at the center
location.
MATLAB CODE:
1D convolution (Useful for 1-D Signal Processing):
clear;
clc;
x=input("Enter value of x: ")
y=input("Enter value of y: ")
n=length(x)
k=length(y)
for z=n+1:n+k-1
x(z)=0;
end
for u=k+1:n+k-1
y(u)=0;
end
for i=1:n+k-1
s(i)=0
for j=1:i
s(i)=(x(j)*y(i-j+1))+s(i)
end
end
subplot(3,1,1) plot2d3(x)
subplot(3,1,2) plot2d3(y)
subplot(3,1,3)
plot2d3(s)
clear;
clc;
x=input("Enter value of x in matrix form: ") y=input("Enter
value of y in matrix form: ")
[xrows,xcols]=size(x); [yrows,ycols]=size(y);
result=zeros(xrows+yrows,xcols+ycols)
for r = 1:xrows-1
for c = 1:xcols-1
sum=0;
for a=0:yrows
for b=0:ycols
sum=sum+x(r+a,c+b)*y(a+1,b+1);
end
end
result(r,c)=sum;
end
end
MATLAB Code:
clc;
close all;
clear all;
L1 = [1 1 1; 1 1 1; 1 1 1];
L2 = [0 1 0; 1 2 1; 0 1 0];
L3 = [1 2 1; 2 4 2; 1 2 1];
H1 = [-1 -1 -1; -1 9 -1; -1 -1 -1];
H2 = [0 -1 0; -1 5 -1; 0 -1 0];
H3 = [1 -2 1; -2 5 -2; 1 -2 1];
myimage = imread('model.jpg');
if size(myimage, 3) == 3
myimage = rgb2gray(myimage);
end
subplot(3,2,1);
imshow(myimage); title('Original Image');
L1 = L1 / sum(L1(:));
filt_image = conv2(double(myimage), double(L1), 'same');
subplot(3,2,2); imshow(filt_image, []); title('Filtered image with mask L1');
L2 = L2 / sum(L2(:));
filt_image = conv2(double(myimage), double(L2), 'same');
subplot(3,2,3); imshow(filt_image, []); title('Filtered image with mask L2');
L3 = L3 / sum(L3(:));
filt_image = conv2(double(myimage), double(L3), 'same');
subplot(3,2,4); imshow(filt_image, []); title('Filtered image with mask L3');
figure;
subplot(2,2,1); imshow(myimage); title('Original Image');
gaussmask = fspecial('gaussian', 3);
filtimg = imfilter(myimage, gaussmask);
subplot(2,2,2); imshow(filtimg, []), title('Output of Gaussian filter 3 X 3');
Tutorial:
1.) Write mathematical expression of spatial filtering of image f(x,y) of size M*N
using mask W of size a*b.
For an image f(x,y) of size M×N and a mask W of size a×b, the output image g(x,y)
after applying spatial filtering can be expressed mathematically as:
Where:
g(x,y) is the output pixel value at position (x,y).
W(i,j) is the weight of the mask at position (i,j).
The summation iterates over the mask dimensions, centering it at each pixel
(x,y)(x, y)(x,y) of the input image.
2.) What is need for padding? What is zero padding? Why it is required?
Zero Padding:
Zero padding is a technique where the borders of the image are extended by adding
a layer of zeros around the original image. For example, if we have an image of size
M×NM \times NM×N and we apply zero padding, the new image size will be
(M+2p)×(N+2q)(M + 2p) where p and q are the number of rows and columns of zeros
added.
It helps in avoiding the loss of information at the boundaries, ensuring that all
pixels can be processed.
Loss of Edge Definition: With larger masks, the definition of edges may be lost, as
more pixels are averaged together. This can be detrimental in applications where
edge detection is crucial.