dip record
dip record
DATE :
Step (4) − Convert the grayscale image to binary image based on the
defined threshold value.
Example
1
RESULT :
Thus the given program is executed and verified successfully
2
EX. NO : Implementation of Relationships between pixels
DATE :
You can calculate connected components by using the bwconncomp function. In this
sample code, BW is the binary matrix shown in the above figure. For this example,
specify a connectivity of 4 so that two adjoining pixels are part of the same object if
they are both on and are connected along the horizontal or vertical direction.
The PixelIdxList field identifies the list of pixels belonging to each connected
component.
BW = zeros(8,8);
BW(2:4,2:3) = 1;
BW(5:7,4:5) = 1;
BW(2,6:8) = 1;
BW(3,7:8) = 1;
BW
BW =
3
0 0 0 0 0 0 0 0
0 1 1 0 0 1 1 1
0 1 1 0 0 0 1 1
0 1 1 0 0 0 0 0
0 0 0 1 1 0 0 0
0 0 0 1 1 0 0 0
0 0 0 1 1 0 0 0
0 0 0 0 0 0 0 0
cc4 = bwconncomp(BW,4)
cc4 =
Connectivity: 4
ImageSize: [8 8]
NumObjects: 3
PixelIdxList: {[6x1 double] [6x1 double] [5x1 double]}
For comparison, calculate the connected components of the same binary image using
the default connectivity, 8.
cc8 = bwconncomp(BW)
cc8 =
Connectivity: 8
ImageSize: [8 8]
NumObjects: 2
PixelIdxList: {[12x1 double] [5x1 double]}
4
Create a label matrix by using the labelmatrix function. This sample code continue
with the connected component structure, cc4, defined in the preceding section.
L4 = labelmatrix(cc4)
L4 =
0 0 0 0 0 0 0 0
0 1 1 0 0 3 3 3
0 1 1 0 0 0 3 3
0 1 1 0 0 0 0 0
0 0 0 2 2 0 0 0
0 0 0 2 2 0 0 0
0 0 0 2 2 0 0 0
0 0 0 0 0 0 0 0
OUTPUT :
5
EX . NO : Implementation of Arithmetic, Set operations and
DATE : Transformations of an image
imadd
Syntax
Z = imadd(X,Y)
Description
Z = imadd(X,Y) adds each element in array X with the corresponding element in array Y and
returns the sum in the corresponding element of the output array Z.
Examples
Add Two uint8 Arrays
This example shows how to add two uint8 arrays with truncation for values that exceed 255.
Get
X = uint8([ 255 0 75; 44 225 100]);
Y = uint8([ 50 50 50; 50 50 50 ]);
Z = imadd(X,Y)
Z = 2x3 uint8 matrix
255 50 125
94 255 150
imshow(K,[])
6
Add a Constant to an Image
I = imread('rice.png');
Add a constant to the image.
Get
J = imadd(I,50);
Display the original image and the result.
Get
imshow(I)
Get
figure
imshow(J)
7
imsubtract
Subtract one image from another or subtract constant from image
Syntax
Z = imsubtract(X,Y)
Description
Z = imsubtract(X,Y) subtracts each element in array Y from the corresponding element in
array X and returns the difference in the corresponding element of the output array Z.
Imshow(j)
8
Perform Simple 2-D Translation Transformation
Try This ExampleCopy Code Copy Command
This example shows how to perform a simple affine transformation called a translation. In a
translation, you shift an image in coordinate space by adding a specified value to the x- and y-
coordinates. (You can also use the imtranslate function to perform translation.)
Read the image to be transformed. This example creates a checkerboard image using
the checkerboard function.
Get
cb = checkerboard;
imshow(cb)
Get spatial referencing information about the image. This information is useful when you want
to display the result of the transformation.
Get
cb_ref = imref2d(size(cb))
cb_ref =
imref2d with properties:
tx = 20;
ty = 30;
Create a transltform2d geometric transformation object that represents the translation
transformation. For other types of geometric transformations, you can use other types of
objects.
Get
tform = transltform2d(tx,ty);
Perform the transformation. Call the imwarp function, specifying the image you want to
transform and the geometric transformation object. imwarp returns the transformed
image, cb_translated. This example also returns the optional spatial referencing
9
object, cb_translated_ref, which contains spatial referencing information about the
transformed image.
Get
[cb_translated,cb_translated_ref] = imwarp(cb,tform);
View the original and the transformed image side-by-side. When viewing the translated image,
it might appear that the transformation had no effect. The transformed image looks identical to
the original image. The reason that no change is apparent in the visualization is
because imwarp sizes the output image to be just large enough to contain the entire
transformed image but not the entire output coordinate space. Notice, however, that the
coordinate values have been changed by the transformation.
Get
figure
subplot(1,2,1)
imshow(cb,cb_ref)
subplot(1,2,2)
imshow(cb_translated,cb_translated_ref)
To see the entirety of the transformed image in the same relation to the origin of the coordinate
space as the original image, use imwarp with the "OutputView" name-value argument,
specifying a spatial referencing object. The spatial referencing object specifies the size of the
output image and how much of the output coordinate space is included in the output image. To
do this, the example makes a copy of the spatial referencing object associated with the original
image and modifies the world coordinate limits to accommodate the full size of the transformed
image. The example sets the limits of the output image in world coordinates to include the origin
from the input.
Get
10
cb_translated_ref = cb_ref;
cb_translated_ref.XWorldLimits(2) = cb_translated_ref.XWorldLimits(2)+tx;
cb_translated_ref.YWorldLimits(2) = cb_translated_ref.YWorldLimits(2)+ty;
figure
subplot(1,2,1)
imshow(cb,cb_ref)
subplot(1,2,2)
imshow(cb_translated,cb_translated_ref);
11
Image Rotation and Scale
Try This ExampleCopy Code Copy Command
This example shows how to align or register two images that differ by a rotation and a
scale change. You can calculate the rotation angle and scale factor and transform the
distorted image to recover the original image.
Step 1: Read Image
Read an image into the workspace.
Get
original = imread("cameraman.tif");
imshow(original)
text(size(original,2),size(original,1)+15, ...
"Image courtesy of Massachusetts Institute of Technology", ...
FontSize=7,HorizontalAlignment="right")
scaleFactor = 0.7;
distorted = imresize(original,scaleFactor);
theta = 30;
distorted = imrotate(distorted,theta);
imshow(distorted)
12
Transform Image Using 2-D Affine Transformation Object
Read an image into the workspace.
A = imread('pout.tif');
Create an affine2d object that defines an affine geometric transformation. This example
combines vertical shear and horizontal stretch.
Get
tform = affine2d([2 0.33 0; 0 1 0; 0 0 1])
tform =
affine2d with properties:
T: [3x3 double]
Dimensionality: 2
13
RESULT :
Thus the given program is executed and verified successfully
14
EX . NO : contrast streching ,histogram & histogramequalization
DATE :
stretchlim
Find limits to contrast stretch an image
Syntax
• LOW_HIGH = stretchlim(I)
• LOW_HIGH = stretchlim(I,TOL)
• LOW_HIGH = stretchlim(RGB,TOL)
Description
If you omit the argument, TOL defaults to [0.01 0.99], saturating 2%.
Note If TOL is too big, such that no pixels would be left after saturating low
and high pixel values, stretchlim returns [0 1].
Class Support
The input image can be of class uint8, uint16, int16, double, or single. The output
limits returned, LOW_HIGH, are of class double and have values between 0 and 1.
15
Example
• I = imread('pout.tif');
• J = imadjust(I,stretchlim(I),[]);
• imshow(I), figure, imshow(J)
I = imread("pout.tif");
Display the image and its histogram. The original image has low contrast, with most pixel
values in the middle of the intensity range.
Get
figure
subplot(1,3,1)
imshow(I)
subplot(1,3,2:3)
imhist(I)
16
Adjust Contrast Using Default Equalization
Adjust the contrast using histogram equalization. Use the default behavior of the histogram
equalization function, histeq. The default target histogram is a flat histogram with 64 bins.
Get
J = histeq(I);
Display the contrast-adjusted image and its new histogram.
Get
figure
subplot(1,3,1)
imshow(J)
subplot(1,3,2:3)
imhist(J)
17
Adjust Contrast, Specifying Number of Bins
Adjust the contrast, specifying a different number of bins. With a small number of bins, there are
noticeably fewer gray levels in the contrast-adjusted image.
Get
nbins = 10;
K = histeq(I,nbins);
Display the contrast-adjusted image and its new histogram.
Get
figure
subplot(1,3,1)
imshow(K)
subplot(1,3,2:3)
imhist(K)
RESULT :
Thus the given program is executed and verified successfully
18
EX . NO : Display of bit planes of an images
DATE :
b1 = double(bitget(Img,1));
b2 = double(bitget(Img,2));
b3 = double(bitget(Img,3));
b4 = double(bitget(Img,4));
b5 = double(bitget(Img,5));
b6 = double(bitget(Img,6));
b7 = double(bitget(Img,7));
b8 = double(bitget(Img,8));
Img_b = cat(8,b1,b2,b3,b4,b5,b6,b7,b8);
figure,
imshow(Img), title('Original:');
figure,
subplot(2,2,1)
imshow(b1), title('Bit Plan: 1');
subplot(2,2,2)
imshow(b2), title('Bit Plan: 2');
subplot(2,2,3)
imshow(b3), title('Bit Plan: 3');
subplot(2,2,4)
imshow(b4), title('Bit Plan: 4');
figure,
subplot(2,2,1)
imshow(b5), title('Bit Plan: 5');
subplot(2,2,2)
imshow(b6), title('Bit Plan: 6');
subplot(2,2,3)
imshow(b7), title('Bit Plan: 7');
subplot(2,2,4)
imshow(b8), title('Bit Plan: 8');
y = Img_b;
output :
19
EX . NO : Display of fft of an image
DATE :
Steps:
20
Output:
21
Figure 3:Centered spectrum of FT image
22
Code Explanation :
Output :
Figure 1: Original Input Image was obtained by taking the Inverse FT of the FT image
23
Figure 2: log of absolute Inverse Fourier Transformed Image
24
EX . NO : Image Smoothening Filters
DATE :
Imgaussfilt
2-D Gaussian filtering of images
Syntax
B = imgaussfilt(A)
B = imgaussfilt(A,sigma)
B = imgaussfilt(___,Name,Value)
Description
B = imgaussfilt(A) filters image A with a 2-D Gaussian smoothing kernel with standard
deviation of 0.5, and returns the filtered image in B.
B = imgaussfilt(A,sigma) filters image A with a 2-D Gaussian smoothing kernel with
standard deviation specified by sigma.
B = imgaussfilt(___,Name,Value) uses name-value arguments to control aspects of the
filtering.
Examples
collapse all
Smooth Image with Gaussian Filter
Try This ExampleCopy Code Copy Command
I = imread('cameraman.tif');
Filter the image with a Gaussian filter with standard deviation of 2.
Get
Iblur = imgaussfilt(I,2);
Display the original and filtered image in a montage.
Get
montage({I,Iblur})
title('Original Image (Left) Vs. Gaussian Filtered Image (Right)')
25
Medfilt2
2-D median filtering
collapse all in page
Syntax
J = medfilt2(I)
J = medfilt2(I,[m n])
J = medfilt2(___,padopt)
Description
example
J = medfilt2(I) performs median filtering of the image I in two dimensions. Each output
pixel contains the median value in a 3-by-3 neighborhood around the corresponding pixel in the
input image.
J = medfilt2(I,[m n]) performs median filtering, where each output pixel contains the
median value in the m-by-n neighborhood around the corresponding pixel in the input image.
J = medfilt2(___,padopt) controls how medfilt2 pads the image boundaries.
Examples
collapse all
Remove Salt and Pepper Noise from Image
Try This ExampleCopy Code Copy Command
I = imread('eight.tif');
figure, imshow(I)
26
Add salt and pepper noise.
Get
K = medfilt2(J);
Display results, side-by-side.
Get
imshowpair(J,K,'montage')
RESULT :
Thus the given program is executed and verified successfully
27
EX.NO : Sharpening filters
DATE :
Syntax
B = imsharpen(A)
B = imsharpen(A,Name,Value)
Description
example
B = imsharpen(A) sharpens the grayscale or truecolor (RGB) image A by using the unsharp
masking method.
example
B = imsharpen(A,Name,Value) uses name-value arguments to control aspects of the
unsharp masking.
Examples
collapse all
Sharpen Image
Try This ExampleCopy Code Copy Command
a = imread('hestain.png');
imshow(a)
title('Original Image');
28
Sharpen the image using the imsharpen function and display it.
Get
b = imsharpen(a);
figure, imshow(b)
title('Sharpened Image');
a = imread('rice.png');
imshow(a), title('Original Image');
29
Sharpen image, specifying the radius and amount parameters.
Get
b = imsharpen(a,'Radius',2,'Amount',1);
figure, imshow(b)
title('Sharpened Image');
OUTPUT :
30
EX . NO : Edge Detection Sobel & Canny Filters
DATE :
In an image, an edge is a curve that follows a path of rapid change in image intensity.
Edges are often associated with the boundaries of objects in a scene. Edge detection is
used to identify the edges in an image.
To find edges, you can use the edge function. This function looks for places in the
image where the intensity changes rapidly, using one of these two criteria:
• Places where the first derivative of the intensity is larger in magnitude than some
threshold
• Places where the second derivative of the intensity has a zero crossing
edge provides several derivative estimators, each of which implements one of these
definitions. For some of these estimators, you can specify whether the operation should
be sensitive to horizontal edges, vertical edges, or both. edge returns a binary image
containing 1's where edges are found and 0's elsewhere.
The most powerful edge-detection method that edge provides is the Canny method. The
Canny method differs from the other edge-detection methods in that it uses two
different thresholds (to detect strong and weak edges), and includes the weak edges in
the output only if they are connected to strong edges. This method is therefore less
likely than the others to be affected by noise, and more likely to detect true weak edge
This example shows how to detect edges in an image using both the Canny edge
detector and the Sobel edge detector.
Read the image into the workspace and display it.
Get
I = imread('coins.png');
imshow(I)
31
Apply the Sobel edge detector to the unfiltered input image. Then, apply the Canny
edge detector to the unfiltered input image.
Get
BW1 = edge(I,'sobel');
BW2 = edge(I,'canny');
Display the filtered images side-by-side for comparison.
Get
tiledlayout(1,2)
nexttile
imshow(BW1)
title('Sobel Filter')
nexttile
imshow(BW2)
title('Canny Filter')
RESULT :
32
EX . NO : Implementation of image restoration techniques
DATE :
Image restoration is the operation of taking a corrupt/noisy image and estimating the clean,
original image. Corruption may come in many forms such as motion blur, noise and camera
mis-focus.
Steps :
EXAMPLE :
I = im2double(imread('Fig0526(a)(original_DIP).tif'));
imshow(I);
LEN = 90;
THETA = 45;
figure, imshow(blurred)
noise_mean = 0;
noise_var =0.01 ;
figure, imshow(blurred_noisy)
figure, imshow(wnr3)
33
OUTPUT :
RESULT :
34
EX . NO : Object Deduction And Recognition
DATE :
In MATLAB, object deduction and recognition can be performed using the Computer Vision
System Toolbox and Deep Learning Toolbox. Below is a step-by-step example demonstrating
how to achieve this using a pre-trained deep learning model, such as AlexNet.
STEPS :
1. Load Image: The script starts by loading an image from a specified path and displaying it.
2. Preprocess Image: The image is resized to the input size required by AlexNet (227x227
pixels). If the image is grayscale, it is converted to an RGB image by replicating the single
channel.
3. Load Model: The AlexNet model is loaded using the `alexnet` function. This function loads a
pre-trained AlexNet model.
4. Classify Image: The preprocessed image is classified using the `classify` function. The resulting
label is then displayed on the original image.
OBJECT DEDUCTION
ORIGINAL IMAGE :
CODE :
import cv2
img = cv2.imread("image.jpg")
35
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.subplot(1, 1, 1)
plt.imshow(img_rgb)
plt.show()
OUTPUT :
OBJECT RECOGNITION
CODE :
import cv2
img = cv2.imread("image.jpg")
stop_data = cv2.CascadeClassifier('stop_data.xml')
36
amount_found = len(found)
if amount_found != 0:
plt.subplot(1, 1, 1)
plt.imshow(img_rgb)
plt.show()
OUTPUT :
RESULT :
37