Você está na página 1de 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.

Você também pode gostar