You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
shape detection and classification without regionprop command
1 view (last 30 days)
Show older comments
1. A binary (black and white) image with all the shapes in white and background in black. In addition, a number, indicating the classification according to the above numbered descriptors, must be inserted in the center of each shape. A value of zero (0) should be placed in the center of the shape that does not fall into one of the above classifications.
A color image with each shape displayed in a pre-assigned color i.e. instead of inserting a number into each shape they can be displayed in different colors e.g. blue for all circles and yellow for all hexagons. The color black must be assigned to all unclassified shapes. The background of this image must be white.
A binary (black and white) image with all the shapes in white and background in black. In addition, a number, indicating the area (in m2 and 3 decimal places), must be inserted in the center of each shape in the image. A text value of "N/A" should be placed in the center of the shape that does not fall into one of the above classifications. Assume each pixel represents a distance of 1 mm.
In my attached files you would find my progress on each part of the question. I have done most of it, i just need your help to see where i am wrong.
9 Comments
Guillaume
on 11 Nov 2014
Edited: Guillaume
on 11 Nov 2014
It's not urgent. You should have started your homework earlier.
As I doubt many people will be willing to spend the time to download your code, figure out how to run it, and then figure out what it's supposed to do, why don't you tell us what is not working and what is expected and show just the relevant portion of code?
jason
on 11 Nov 2014
I have been working on it for the past two months. The problem i am having is once i put the number or area result into the shape i detected it displays but when i add all the shapes to make the image again the number or area value does not appear. The reason i put my code is so your'll could see that i have done my homework and i am asking for help because i am into the position where i ran out of ideas.This is my final subject to get my diploma, it means a lot to me if your'll are willing to help.
Guillaume
on 11 Nov 2014
You need to make it as easy as possible for people to help you.
The first sentence of your question ( A binary (black and white)...) does not even have a verb. If you make it hard to understand your question, people won't bother.
Similarly, you give us a bunch of code (three files which at first glance look exactly the same) and ask us to figure out what's wrong with it. Rather than letting us guess, you need to be more precise and tell us what you expect (the final image is displayed with numbers in it) and what actually happens (the final image is displayed correctly but there's no number in it).
Furthermore, If I try to run your first file I get an error " img_wk_bw not defined ". Again you're making hard to help you by providing code with typos.
After spending much longer than I wanted looking at your code, it seems to me that your problem is that you've displayed your numbers on the figure for each shape, but you haven't done so on the final figure. I'm not sure why you then expect the numbers to be there.
jason
on 11 Nov 2014
That's the problem i am having, after detecting the shapes i insert the number but when i combine the shapes i lose the number, i just don't know how to get the numbers into the combined image. Sorry for the 3 files, just each one is for each part of the question. I am having similar problem with the color part as well.
jason
on 11 Nov 2014
img_wk_bw_L_total=img_wk_bw_L *0; %I mistakely deleted the L that's why it gave an error.
jason
on 11 Nov 2014
the specification of my design states no use of region prop command, so i classified the shapes using the bounding box area ratio compared to area of each shape,and i inserted a number into each shape,but when i combine all the shapes to get the original image in grayscale with the classified numbers,i get the shapes but not the numbers.
jason
on 12 Nov 2014
Thanks guys it works, but i am having this clash. Last triangle got number 3 and 0.
Accepted Answer
Guillaume
on 11 Nov 2014
Using text on a figure, only displays that text on the figure. It doesn't write it on the original image that you've used to create the figure.
Therefore, if you want all the numbers to be displayed on the final figure. You need to store their position and value until you're ready to write them at the end.
So at the beginning of your script, once you know how many shapes, predeclare a matrix for your centroids and a cell array for your text:
allcentroids = zeros(2, Ne);
texts = cell(1, Ne);
After you display your individual figures, you then store the centroid and text in the corresponding column of the above matrix/cell
allcentroids(:, n) = Centroid';
texts{n} = num2str(a);
Then after you display the final image, you also display the text at the coordinate you saved
figure(99),imshow(img_wk_bw_L_total);
for shapenumber = 1:Ne
text(allcentroids(1, shapenumber), allcentroids(2, shapenumber), texts{shapenumber});
end
11 Comments
jason
on 11 Nov 2014
close all %close previous work clc %clears command window image=('test_shapes.jpg'); %load test image RGB = imread(image); %read the loaded image
sharp=imsharpen(RGB); %sharpen the read image grayscale=rgb2gray(sharp); %convert from rgb image to grayscale format
h = fspecial('gaussian',40,1); %create a smoothing filter filtered=imfilter(grayscale,h); %filtering
Edgecanny = edge(filtered,'canny'); %edge detection X=imfill(Edgecanny,'holes'); %fill in with holes
Bw=im2bw(X); %convert to black and white image figure(3), imshow(Bw); %displays black and white image
dimensions = size(RGB); %finds size of original image img_blank = zeros(dimensions); %creates a blank image same size as original
[L,Ne]=bwlabel(Bw); img_wk_bw_L=L==100; figure(5),imshow(img_wk_bw_L); img_wk_bw_L_total=img_wk_bw_L*0;
for n=1:Ne
allcentroids = zeros(2, Ne);
texts = cell(1, Ne);
%SHAPE FACTORS%
img_wk_bw_L=(L==n);
[row, col] = find(L==n); Edgesobel=edge((L==n), 'sobel'); %edge detect each shape again BinaryX1=imfill((L==n),'holes'); %fill in the holes in each binarycomponent
%To find Bounding Box sx=min(col)-0.5; %starting position for column sy=min(row)-0.5; %starting position for row breadth=max(col)-min(col)+1; %gives breadth of the box length=max(row)-min(row)+1; %gives length of the box BBox=[sx sy breadth length]; %Bounding Box value for the range of n
%To find Ratio area1=bwarea(BinaryX1); %calculates the area of each binary component x1=round(area1); %rounding off calculated area d=x1 * %converting from mm to m a=roundn(d,-3); %rounding off to 3 decimal places area2=(length*breadth %calculates the area of each bounding box ratio = area2/area1; %calculates the ratio of area's Ratio=roundn(ratio,-2); %rounds the ratio to two decimal places
%Find Perimeter BW1=bwboundaries(BinaryX1); c=cell2mat(BW1(1)); Perimeter=0; for i=1:size(c,1)-1 Perimeter=Perimeter+sqrt((c(i,1)-c(i+1,1)).^2+(c(i,2)-c(i+1,2)).^2); end display(Perimeter);
%Find Roundness Roundness=(4*area1*pi)\Perimeter.^2; display(Roundness); %display roundness factor
%Edge Detection Mx=corner(Edgesobel); %corner detection x1 = Mx(2,1); x2 = Mx(3,1); x3 = Mx(1,1); y1 = Mx(2,2); y2 = Mx(3,2); y3 = Mx(1,2);
length = sqrt(((x3-x2).^2)+((y1-y2).^2)); %length of side D1 height= sqrt(((x3-x2).^2)+((y3-y2).^2)); %length of side D2 Result=length-height;
%Find Centroid X=mean(col); %calculates average of columns Y=mean(row); %calculates average of rows Centroid=[X Y]; %matrix of centroid dimensions display(Centroid); %display centroid
%SHAPE CLASSIFICATION CIRCLE
if(Ratio>=1.27 && Ratio<=1.29 && Roundness>=1.138 && Roundness<=1.200); %compares factors
disp('circle'); %display result
figure(10+n),imshow(L==n);title('Circle Image'); %shows result in figure
hold on %holds figure
text(Centroid(1),Centroid(2), num2str('1')); %places text
hold off %goes to next command
img_wk_bw_L=(L==n);
end
%SHAPE CLASSIFICATION SQUARE
if (Result==0 && Ratio==1 ) %compares factors
disp('square'); %display result
figure(10+n), imshow(L==n);title('Square Image'); %shows result in figure
hold on %holds figure
text(Centroid(1),Centroid(2), num2str('2')); %places text
hold off %goes to next command
img_wk_bw_L=(L==n);
end
%SHAPE CLASSIFICATION TRIANGLE
if (Ratio>=1.82 && Ratio<=1.90 && Roundness>=1.800 && Roundness<=1.8500);%compares factors
disp('triangle'); %display result
figure(10+n),imshow(L==n);title('Triangle Image'); %shows result in figure
hold on %holds figure
text(Centroid(1),Centroid(2), num2str('3')); %places text
hold off %goes to next command
img_wk_bw_L=(L==n);
end
%SHAPE CLASSIFICATION RECTANGLE
if (Result~=0 && Ratio==1) %compares factors
disp('rectangle'); %display result
figure(10+n), imshow(L==n);title('Rectangle Image'); %shows result in figure
text(Centroid(1),Centroid(2), num2str('4')); %places text
img_wk_bw_L=(L==n);
end
%SHAPE CLASSIFICATION PENTAGON
if (Ratio>=1.40 && Ratio<=1.44 ) %compares factors
disp('pentagon'); %display result
figure(10+n), imshow(L==n);title('Pentagon Image'); %shows result in figure
text(Centroid(1),Centroid(2), num2str('5')); %places text
img_wk_bw_L=(L==n);
end
%SHAPE CLASSIFICATION HEXAGON
if (Ratio>=1.30 && Ratio<=1.32) %compares factors
disp('hexagon'); %display result
figure(10+n),imshow(L==n);title('Hexagon Image'); %shows result in figure
text(Centroid(1),Centroid(2), num2str('6')); %places text
img_wk_bw_L=(L==n);
end
%SHAPE CLASSIFICATION ELLIPSE
if (Ratio>=1.25 && Ratio<=1.28 && Roundness>=1.400 && Roundness<=1.500) %compares factors
disp('ellipse'); %display result
figure(10+n),imshow(L==n);title('Ellipse Image'); %shows result in figure
text(Centroid(1),Centroid(2), num2str('7')); %places text
img_wk_bw_L=(L==n);
end
%SHAPE CLASSIFICATION OCTAGON
if (Ratio>=1.10 && Ratio<=1.20) %compares factors
disp('octagon'); %display result
figure(10+n),imshow(L==n);title('Octagon Image'); %shows result in figure
text(Centroid(1),Centroid(2), num2str('8')); %places text
img_wk_bw_L=(L==n);
end
%SHAPE CLASSIFICATION DIAMOND
if (Ratio>=1.87 && Ratio<=1.90 && Roundness>=1.300 && Roundness<=1.400 )%compares factors
disp('diamond'); %display result
shape='diamond';
figure(10+n), imshow(L==n);title('Diamond Image'); %shows result in figure
text(Centroid(1),Centroid(2), num2str('9')); %places text
img_wk_bw_L=(L==n);
end
%SHAPE CLASSIFICATION UNKNOWN SHAPES
if (Ratio>=1.27 && Ratio<=1.29 && Roundness>=1.138 && Roundness<=1.200); %compares factors
elseif(Result==0 && Ratio==1); %compares factors
elseif(Ratio>=1.82 && Ratio<=1.90 && Roundness>=1.800 && Roundness<=1.8500); %compares factors
elseif(Ratio>=1.40 && Ratio<=1.44); %compares factors
elseif(Ratio>=1.30 && Ratio<=1.32); %compares factors
elseif(Ratio>=1.25 && Ratio<=1.28 && Roundness>=1.400 && Roundness<=1.500); %compares factors
elseif(Ratio>=1.10 && Ratio<=1.20); %compares factors
elseif(Ratio>=1.87 && Ratio<=1.90 && Roundness>=1.300 && Roundness<=1.400 ); %compares factors
elseif(Result~=0 && Ratio==1); %compares factors
else figure(10+n), imshow(L==n);title('Not classified'); %shows result in figure
text(Centroid(1),Centroid(2), num2str('0')); %places text
img_wk_bw_L=(L==n);
end
allcentroids(:, n) = Centroid';
texts{n} = num2str(a);
img_wk_bw_L_total= (img_wk_bw_L_total + img_wk_bw_L) > 0;
figure(99),imshow(img_wk_bw_L_total);
for shapenumber = 1:Ne
text(allcentroids(1, shapenumber), allcentroids(2, shapenumber), texts{shapenumber});
end
end
jason
on 11 Nov 2014
gives me error :Error in shapedetectorfinal (line 209) text(allcentroids(1, shapenumber), allcentroids(2, shapenumber), texts{shapenumber});
jason
on 11 Nov 2014
Please could you edit my original code, i am very confused as to how to do it, you could show me with one shape and i'll figure out the rest , thank you.
jason
on 11 Nov 2014
This numbers should be for these shapes:
1. Circle
2. Square
3. Triangle
4. Rectangle
5. Pentagon
6. Hexagon
7. Ellipse
8. Octagon
9. Diamond
Image Analyst
on 11 Nov 2014
So you can use bwboundaries() but not regionprops()? Where's the sense in that? Anyway, if you have the Computer Vision System Toolbox, there is an insertText method: http://www.mathworks.com/help/vision/ref/inserttext.html, but are you allowed to use that or not? It will "burn" the text into the image. If you use text() it puts the text into the overlay above the image rather than changing the actual image pixels.
Guillaume
on 11 Nov 2014
I thought it was fairly obvious where each bit went.
%... first part of script. Identifies objects, etc.
%initialise storage of centroids / text to display
allcentroids = zeros(2, Ne);
texts = cell(1, Ne);
for n=1:Ne
%...
%SHAPE CLASSIFICATION xxxxx
%...
figure(10+n),imshow(L==n);title('xxxxxx');
text(Centroid(1),Centroid(2), num2str('xxxx'));
allcentroids(:, n) = Centroid';
texts{n} = num2str(a);
%...
end
figure(99),imshow(img_wk_bw_L_total);
for shapenumber = 1:Ne
text(allcentroids(1, shapenumber), allcentroids(2, shapenumber), texts{shapenumber});
end
Note that I moved the display of the final image out of the for loop.
Once again, you need to be more precise in the information you give. If you get an error, what is the error?
jason
on 12 Nov 2014
Now that each shape is classified into its group, how would i go about to add color to each shape, each shape must be colored according to group i.e squares all blue, circles all red,but shape that don't fall into the classification should be black in color. I used RGB2 below but i cant add the shapes together into an image with a white background. Is there any other method???
RGB2 = label2rgb(L==n, 'copper', 'w', 'noshuffle');
More Answers (2)
jason
on 11 Nov 2014
jason
on 12 Nov 2014
Now that each shape is classified into its group, how would i go about to add color to each shape, each shape must be colored according to group i.e squares all blue, circles all red,but shape that don't fall into the classification should be black in color. I used RGB2 below but i cant add the shapes together into an image with a white background. Is there any other method???
RGB2 = label2rgb(L==n, 'copper', 'w', 'noshuffle');
See Also
Categories
Find more on Deep Learning with Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)