Sunteți pe pagina 1din 3

>> a=imread('E:\maze.

jpg');
l=im2bw(a);
r=a(:,:,1);
g=a(:,:,2);
b=a(:,:,3);
rtl=graythresh(r)*255; % lower threshold value for red
rth=255; % higher threshold value for red
gtl=graythresh(g)*255; % lower threshold value for green
gth=255; % higher threshold value for green
btl=graythresh(b)*255; % lower threshold value for blue
bth=255; % higher threshold value for blue
rm=(r>=rtl)&(r<=rth); % masking red i.e displayed image is devoid of red
gm=(g>=gtl)&(g<=gth); % masking green i.e displayed image is devoid of gree
n
bm=(b>=btl)&(b<=bth); % masking blue i.e displayed image is devoid of blue
rdot=imadd(l,~rm); % adds labeled image of a and inversed image of red m
ask
gdot=imadd(l,~gm); % adds labeled image of a and inversed image of green
mask
bdot=imadd(l,~bm); % adds labeled image of a and inversed image of blue
mask
se=strel('disk',5); % creates a structural element disk of radius 5 pixel
s
r2=imclose(rdot,se); % erodes the red dot by adding boundary pixels
g2=imclose(gdot,se); % erodes the green dot by adding boundary pixels
b2=imclose(bdot,se); % erodes the blue dot by adding boundary pixels
r3=bwlabel(~r2); % taking the inversed image to pointout cen
troid accurately
g3=bwlabel(~g2); % taking the inversed image to pointout cen
troid accurately
b3=bwlabel(~b2); % taking the inversed image to pointout cen
troid accurately
G1=regionprops(r3,'centroid'); % getting centroid of the dot
Gr=cat(1,G1.Centroid); % getting matrix of the centroid
G2=regionprops(g3,'centroid'); % getting centroid of the dot
Gg=cat(1,G2.Centroid); % getting matrix of the centroid
G3=regionprops(b3,'centroid'); % getting centroid of the dot
Gb=cat(1,G3.Centroid); % getting matrix of the centroid
>> bkm=rm+gm+bm; % creating a mask for all colors
>> se1=strel('disk',10); % creating structural element disk of radiu
s 10
>> lc=imclose(bkm,se1); % close= first dilate the erode -> gives bl
ack nodes removing lines
>> [bkl bkn]=bwlabel(~lc); % labeling the black dots
>> bkg=regionprops(bkl,'centroid'); % finding ccentroids
>> bkg1=cat(1,bkg.Centroid); % creating centroid matrix
>> lc1=~lc; % creating inverse of lc ->shows shows the
black dots in white
>> lcd=double(lc1); % converting format of lc1 to double
>> lines=imadd(bkm,lcd); % adding white dots and the black maze with
black nodes->gives lines with leftover pixels of black nodes
>> se2=strel('disk',3); % creating structural element disk of radiu
s 3
>> linesc=imclose(lines,se2); % close= first dilate the erode -> gives li
nes removing black dots almsot completely
>> linesr=~linesc; % inverse of image
>> lines1=bwareaopen(linesr,100); % removing any object of size<100 pixels
>> lines2=~lines1; % inverse of image
>> linesf=imerode(lines2,se2); % eroding hte image
>> [ll nl]=bwlabel(~linesf); % labeling objects of maze
>> lg=regionprops(ll,'centroid'); % finding centroids of the lines
>> lg1=cat(1,lg.Centroid); % creating matrix of the centroids
% plotting the centroids over lines
>> figure,imshow(bkm)
>> hold on
>> plot(lg1(:,1),lg1(:,2),'w.')
>> hold off
>> cm=cat(1,bkg1,Gg);
>> com=[];
>> name=lg1;
>> x1=Gr(1,1);
>> y1=Gr(1,2);

S-ar putea să vă placă și