Escolar Documentos
Profissional Documentos
Cultura Documentos
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
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;
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);
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);
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);
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.
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);