function classifyShapes = ClassifyShapes (thisImage) % read the file in RGB = imread(thisImage); % convert to gray scale GRAY = rgb2gray(RGB); % turn the image into a binary image and invert the color scheme threshold = graythresh(GRAY); BW = im2bw(GRAY, threshold); BW = ~ BW; L = bwlabel(BW); % Trace the region boundaries in the binary image % Using noholes option for performance [boundaries,labelMatrix] = bwboundaries(BW, 'noholes'); % Find the bounding box of the object STATS = regionprops(labelMatrix, 'all'); Centroid = regionprops(L, 'centroid'); Area = regionprops(L, 'area'); centroidSize = size(Centroid); figure, imshow(RGB), title('Results'); hold on for x=1:centroidSize(1) b= boundaries{x}; centroidSize = size(b); for i=1:centroidSize(1); distance{x}(1,i) = sqrt ( ( b(i,2) - Centroid(x).Centroid(1) )^2 + ( b(i,1) - Centroid(x).Centroid(2) )^2 ); end a=max(distance{x}); b=min(distance{x}); c=Area(x).Area; distDiff=a-b; square= c/(4*b^2); triangle=(c*3^0.5)/((a+b)^2); if distDiff < 10 findSphere(STATS); elseif (square < 1.05 ) && (square > 0.95 ) findCube(STATS); elseif (triangle < 1.05 ) && (triangle > 0.95 ) findTriangle(STATS); end end end function findCube = FindCube(STATS); stats = STATS; %rect = [left, bottom, width, height] area = stats.Area; %since this is a cube, we only need one side side = sqrt(area); format compact set(gcf,'Menubar','none','Name','Cube', ... 'NumberTitle','off','Position',[500,300,side*2.5,side*2.5], ... 'Color',[0.1 0.3 0.2]); % set up the cube axes h1 = axes('Position',[.2 .2 .9 .9]); vert = [1 1 1; 1 side 1; side side 1; side 1 1 ; ... 1 1 side; 1 side side; side side side; side 1 side]; fac = [1 2 3 4; ... 2 6 7 3; ... 4 3 7 8; ... 1 5 8 4; ... 1 2 6 5; ... 5 6 7 8]; tempFac = [1 2 3; 1 3 4; 2 3 7; 2 7 6; 5 6 7; 5 7 8; 7 8 4; 4 3 7; 5 4 8; 5 1 4; 6 5 1; 2 6 1]; patch('Faces',fac,'Vertices',vert,'FaceColor','r'); % patch function saveobjmesh('cube.obj',vert, tempFac, 'cube'); % color and lighting stuff light('Position',[1 3 2]); light('Position',[-3 -1 3]); material shiny; alpha('color'); alphamap('rampdown'); camlight(45,45); lighting phong view(30,30); end function findSphere = FindSphere(STATS); stats = STATS; % area = pi * r^2 so sqrt{ area / pi } = R radius = sqrt (stats.Area/pi); diameter = radius *2; format compact set(gcf,'Menubar','none','Name','Sphere', ... 'NumberTitle','off','Position',[500,300,diameter * 5,diameter*5], ... 'Color',[0.2 0.3 0.4]); h(1) = axes('Position',[0 0 1 1]); vertices = [1 diameter 1; 1 -diameter 1; diameter -radius 1; 1 -radius diameter; ... -diameter -radius 1; 1 -radius -diameter; diameter radius 1; 1 radius diameter; ... -diameter radius 1; 1 radius -diameter ]; faces = [ 2 3 4; 1 8 7; 2 4 5; 1 9 8; ... 2 5 6; 1 10 9; 2 6 3; 1 7 10; 3 7 8; ... 3 8 4; 4 8 5; 8 9 5; 5 9 10; ... 5 10 6; 6 10 3; ]; patch('Faces',faces,'Vertices',vertices,'FaceColor','r'); % patch function saveobjmesh('sphere.obj',vertices, faces, 'sphere'); set(hs1,'EdgeColor','none', ... 'FaceColor','red', ... 'FaceAlpha','interp'); alpha('color'); alphamap('rampdown'); camlight(45,45); lighting phong hidden off axis square end function findTriangle = FindTriangle(STATS); stats = STATS; base = stats.MajorAxisLength; %rect = [left, bottom, width, height] area = stats.Area; %since this is a triangle, we need to solve height = (2*area)/base; format compact set(gcf,'Menubar','none','Name','Triangle', ... 'NumberTitle','off','Position',[500,300,base*2,base*2], ... 'Color',[0.1 0.3 0.2]); % set up the cube axes h1 = axes('Position',[.2 .2 .9 .9]); center = stats.Centroid(1); triVert = [center height center; 1 1 1; 1 1 height; height 1 height ; height 1 1; center 1 center; ]; triFac = [ 3 1 2; 2 5 1; 4 5 1; 4 3 1; 6 6 2; 5 6 6; 2 5 6; 6 3 2; 6 4 3; 6 5 4 ]; patch('Faces',triFac,'Vertices',triVert,'FaceColor','r'); % patch function saveobjmesh('fred.obj',triVert, triFac, 'Triangle'); % color and lighting stuff light('Position',[1 3 2]); light('Position',[-3 -1 3]); material shiny; alpha('color'); alphamap('rampdown'); camlight(45,45); lighting phong view(30,30); end function saveobjmesh(name,vert,fac, objName) fid=fopen(name,'w'); [vrow, vcol] = size(vert); % for each row print v for row = 1: vrow; fprintf(fid, 'v '); for col = 1: vcol; %then print out each column on the same line fprintf(fid, '%g %g %g', vert(row,col)); end fprintf(fid, '\r\n'); end [frow, fcol] = size(fac); for row = 1: frow; fprintf(fid, 'f '); for col = 1: fcol; %then print out each column on the same line fprintf(fid, '%g %g %g', fac(int8(row),int8(col))); end fprintf(fid, '\r\n'); end fprintf(fid,'g ' ); fprintf(fid,'g\n\n'); fclose(fid);