I want to detect circles and traingles in binary image by couting one's in my image. My code works well for traingle but it doesnot working for circles kindly any one help me?? I have to submit my project in monday..

5 views (last 30 days)
clc clear all close all a = imread ('w1.JPG'); aa2 = im2bw(a); %aak=imresize(aa2,[480 640]);
imshow(aa2)
%/////////FOR BIG CIRCLE///////////////////////
RRBig = aa2(297,431:610);
CCBig = aa2(214:400,506);
[X1 Y1]=size(RRBig);
[wid1 len1]=size(CCBig);
for i=1:X1
for j=1:Y1
for ii=1:wid1
for jj=1:len1
end
end
end
end
if (RRBig(i,j)==1 && CCBig(ii,jj)==1 )
'Big C YES'
else
'Big C NO'
end
%//////// FOR SMALL CIRCLE////////////////////////
RRSmall = aa2(74,445:545);
CCSmall = aa2(35:117,494);
[X2 Y2]=size(RRSmall);
[wid2 len2]=size(CCSmall);
for i1=1:X2
for j1=1:Y2
for ii1=1:wid2
for jj1=1:len2
end
end
end
end
if (RRSmall(i1,j1)==1 && CCSmall(ii1,jj1)==1 )
'Small C YES'
else
'Small C NO'
end
%{
///////////////////////////////////////
if(RRSmall==1 )
'Small Circle Available'
else
'Small Circle not Available'
end
%}
%/////////FOR BIG Triangle///////////////////////
HHbig = aa2(154,74:183);
VVbig = aa2(66:155,127) ;
[X3 Y3]=size(HHbig);
[wid3 len3]=size(VVbig);
for i2=1:X3
for j2=1:Y3
for ii2=1:wid3
for jj2=1:len3
end
end
end
end
if (HHbig(i2,j2)==1 && VVbig(ii2,jj2)==1 )
'Big T YES'
else
'Big T NO'
end
%{
if(HHbig==1 )
'Big Triangle Available'
else
'Big Triangle not Available'
end
%}
%///////////FOR SMALL TRIANGLE/////////////////////
HHsmall = aa2(379,53:140);
VVsmall = aa2(310:385,102);
%///////////////////////////////
[X4 Y4]=size(HHsmall);
[wid4 len4]=size(VVsmall);
for i3=1:X4
for j3=1:Y4
for ii3=1:wid4
for jj3=1:len4
end
end
end
end
if (HHsmall(i3,j3)==1 && VVsmall(ii3,jj3)==1 )
'Small T YES'
else
'Small T NO'
end
%{
if(HHsmall==1 )
'Small Triangle Available'
else
'Small Triangle not Available'
end
%////////////////////////////////
%}

Answers (1)

Image Analyst
Image Analyst on 27 Jun 2015
Edited: Image Analyst on 27 Jun 2015
Well you blew a chance. You forgot to attach w1.jpg so no one can run your code. And soon I'm heading out of town for most of the day. In the meantime, please read this: http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer. I'll check again at the end of the day.
You can also look at my Image Segmentation Tutorial to see how to filter objects by criteria such as shape: http://www.mathworks.com/matlabcentral/fileexchange/25157-image-segmentation-tutorial
You can compute the circularities. Circles and triangles will have different values.
[labeledImage, totalNumObjects] = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'Area', Perimeter');
allAreas = [measurements.Area];
allPerimeters = [measurements.Perimeter];
allCircularities = allPerimeters .^ 2 / (4*pi*allAreas);
triangleIndexes = allCircularities > 2; % Or whatever number works for you.
numberOfTriangles = sum(triangleIndexes);
numberOfCircles = sum(~triangleIndexes);
  3 Comments
Muhammad Sohail Sabir
Muhammad Sohail Sabir on 27 Jun 2015
Image Analyst thanks for ur help buddy. I have attached the image kindly view it. My code works fine on triangles i will find all the triangles but it does not work circles i also found some zero's along ones..
Image Analyst
Image Analyst on 28 Jun 2015
This code seems to work just fine with your image:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
fullImageFileName='C:\Users\Muhammad\Documents\Temporary\w1.jpg';
% Check to see that the image exists. (Mainly to check on the demo images.)
if ~exist(fullImageFileName, 'file')
message = sprintf('This file does not exist:\n%s', fullImageFileName);
uiwait(msgbox(message));
return;
end
% Read in image into an array.
[grayImage, storedColorMap] = imread(fullImageFileName);
[rows, columns, numberOfColorChannels] = size(grayImage);
% Display the original image.
subplot(2, 2, 1);
imshow(grayImage);
% If it's color, make it gray scale.
if numberOfColorChannels > 1
title('Original Color Image', 'FontSize', fontSize);
grayImage = grayImage(:,:,1);
else
caption = sprintf('Original Image');
title(caption, 'FontSize', fontSize);
end
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full screen.
set(gcf,'name','Image Analysis Demo','numbertitle','off')
drawnow; % Make it display immediately.
% Display it.
subplot(2, 2, 2);
imshow(grayImage, []);
title('Grayscale Image', 'FontSize', fontSize);
% Binarize the image.
binaryImage = grayImage > 100;
% Display it.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
[labeledImage, numberOfObjects] = bwlabel(binaryImage);
blobMeasurements = regionprops(labeledImage,'Perimeter','Area', 'Centroid');
% for square ((a>17) && (a<20))
% for circle ((a>13) && (a<17))
% for triangle ((a>20) && (a<30))
circularities = [blobMeasurements.Perimeter].^2 ./ (4 * pi * [blobMeasurements.Area])
% Say what they are
for blobNumber = 1 : numberOfObjects
text(blobMeasurements(blobNumber).Centroid(1), blobMeasurements(blobNumber).Centroid(2), ...
num2str(blobNumber), 'FontSize', 20, 'Color', 'b');
if circularities(blobNumber) < 1.19
message = sprintf('The circularity of object #%d is %.3f, so the object is a circle',...
blobNumber, circularities(blobNumber));
elseif circularities(blobNumber) < 1.53
message = sprintf('The circularity of object #%d is %.3f, so the object is a square',...
blobNumber, circularities(blobNumber));
else
message = sprintf('The circularity of object #%d is %.3f, so the object is a triangle',...
blobNumber, circularities(blobNumber));
end
uiwait(msgbox(message));
end

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!