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

Assignment 1 2 3

The MATLAB code takes an image as input, converts it to black and white, then labels the connected components. It calculates properties like perimeter and centroid for each component. It then clusters the components into two classes based on a threshold perimeter value. The centroids are plotted with different colors to visualize the clustering. The code also provides functions to zoom an image using replication and bilinear interpolation, and to shrink an image by a factor of 2 using nearest neighbor approach.

Uploaded by

rakesh asery
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views

Assignment 1 2 3

The MATLAB code takes an image as input, converts it to black and white, then labels the connected components. It calculates properties like perimeter and centroid for each component. It then clusters the components into two classes based on a threshold perimeter value. The centroids are plotted with different colors to visualize the clustering. The code also provides functions to zoom an image using replication and bilinear interpolation, and to shrink an image by a factor of 2 using nearest neighbor approach.

Uploaded by

rakesh asery
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Assignment -1

Q-1 Write a MATLAB code to count the number of coins in an image.

Ans: - MATLAB code to count the number of coins in an image:-

MATLAB CODE:-

clear all;
close all;
clc;
% displays the grayscale image I
I = imread('coins.png'); figure; imshow(I);
%Convert image into binary image
bw = im2bw(I,0.4);figure; imshow(bw);
%Label connected components in 2-D binary image
[L,num] = bwlabel(bw);

COMMAND WINDOW:-
>> size(I)

ans =

246 300

>> size(bw)

ans =

246 300

>> size(L)

ans =

246 300

>> num

num =

10
Number of coins in an image:-10

COMMAND USED: - imread, imshow, im2bw, bwlabel.


Q-2 Write a MATLAB code for classification of different objects in two or more classes.

Ans: - MATLAB code for classification of different objects in two or more classes:-

MATLAB CODE:-

i=imread('coins.png');
b= im2bw(i,.4);
[L,num]=bwlabeln(b);
% Measure properties of image regions
s = regionprops(L, 'Perimeter');
c= regionprops(L,'centroid');
% Concatenate arrays along specified dimension
centroids = cat(1, c.Centroid);
p = [s.Perimeter];
x=sum(p);
y=x./num;
m=0;
n=0;
imshow(b);
hold on;
for z=1:num;
if (p(z)<=y);
m=m+1;
plot(centroids(z,1), centroids(z,2), 'b*')
else
n=n+1;
plot(centroids(z,1), centroids(z,2), 'r*')
end
end
hold off;

Figure: - Clustering of coins.png image


no of circle in category 1 = 4
no of circle in category 2 = 6

COMMAND USED: - imread, imshow, im2bw, bwlabel, regionprops, cat.

Q-3 Write a MATLAB code to convert and RGB image into Black & White image.

Ans: - MATLAB code to convert and RGB image into Black & White image:-

MATLAB CODE:-

clear all;
close all;
clc;
i=imread('C:\Users\RAKSHK\Pictures\1234','jpg');
figure();imshow(i);
% Convert RGB image or color image to grayscale
j=rgb2gray(i);
figure();imshow(j);
[x,y]=size(j);
for a=1:x
for b=1:y
if(j(a,b)<100)
j(a,b)=0;
else j(a,b)=255;
end
end
end
figure();imshow(j);

Figure: - RGB (color) image Figure: - Gray image


Figure: - Black & white image

COMMAND USED: - imread, imshow, im2bw.


Q-2 Write a Matlab program to zoom using: - (1).Replication and,

(2).Bilinear Methods of interpolation by a factor of 2.

Ans: - MATLAB code to zoom using Replication:-

MATLAB CODE:-

I=imread('coins.png');
figure;imshow(I);
[r,c,d]=size(I);%Array dimensions
z=input('input the value');
zr=z*r;
zc=z*c;
for i=1:1:zr;
x=i/z;
m=round(x);%Round to nearest integer
if m==0;
m=1;
end
for j=1:1:zc
y=j/z;
n=round(y);
if n==0;
n=1;
end
zoom(i,j,:)=I(m,n,:);
end
end
figure;imshow(zoom);

>>input the value 2


.
Figure: -Zoom image

COMMAND USED: - imread, imshow, input, round.

USING FUNCTION
clear all;
close all;
clc;
I=imread('C:\Users\RAKSHK\Pictures\1234','jpg');
figure;imshow(I);
Z = imresize(I, 2, 'nearest');
figure;imshow(Z);

MATLAB code to zoom using Bilinear Methods of interpolation by a factor of 2:-


MATLAB CODE:-

USING FUNCTION
clear all;
close all;
clc;
I=imread('C:\Users\RAKSHK\Pictures\1234','jpg');
figure;imshow(I);
Z = imresize(I, 2, 'bilinear');
figure;imshow(Z);
Q-3 Write a Matlab program to shrink one image by a factor of 2 using nearest neighbours
approach.

Ans: - MATLAB code to shrink:-

MATLAB CODE:-

I=imread('coins.png');
figure;imshow(I);
[r,c,d]=size(I);%Array dimensions
z=input('input the value');
zr=z*r;
zc=z*c;
for i=1:1:zr;
x=i/z;
m=round(x);%Round to nearest integer
if m==0;
m=1;
end
for j=1:1:zc
y=j/z;
n=round(y);
if n==0;
n=1;
end
zoom(i,j,:)=I(m,n,:);
end
end
figure;imshow(zoom);

>> input the value 0 .5

Figure: -coin.png image Figure: -coin.png shrink image


COMMAND USED: - imread, imshow, input, round.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy