detect certain shapes in binary

7 次查看(过去 30 天)
hi,in my image has 2 shapes ;triangular and circular(in binary). how can I eliminate the circular shape so that it just detect triangular shape.many thanks!

采纳的回答

Walter Roberson
Walter Roberson 2012-7-3
Use regionprops() to calculate the Eccentricity. Eccentricity should be nearly 0 for a circle.
  16 个评论
Tulips
Tulips 2012-7-6
编辑:Tulips 2012-7-6
sir,which file should I refer to?there are a lot..

请先登录,再进行评论。

更多回答(2 个)

Image Analyst
Image Analyst 2012-7-3
编辑:Image Analyst 2012-7-9
I prefer the perimeter squared to area ratio - the "circularity". For things like an asterisk shape, the eccentricity would be similar to similar to a circle bit the "circularity" of an asterisk would be much higher than that of a circle.
circularity = perimeter^2 / (4 * pi * area);
This is a very commonly used metric in shape analysis. So common that I don't know why it's not built into regionprops(). See my shape recognition demo:
% Demo to find certain shapes in an image based on their shape.
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;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'pillsetc.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
% Read in image into an array.
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display it.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Input Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Shape Recognition Demo','numbertitle','off')
% If it's monochrome (indexed), convert it to color.
if numberOfColorBands > 1
grayImage = rgbImage(:,:,2);
else
% It's already a gray scale image.
grayImage = rgbImage;
end
% Make a triangle on it.
triangleXCoordinates = [360 420 480];
triangleYCoordinates = [350 252 350];
traiangleBinaryImage = poly2mask(triangleXCoordinates, triangleYCoordinates, rows, columns);
% Burn it into the gray scale image.
grayImage(traiangleBinaryImage) = 255;
% Display it.
subplot(2, 2, 2);
imshow(grayImage, []);
title('Grayscale Image', 'FontSize', fontSize);
% Binarize the image.
binaryImage = grayImage > 120;
% Display it.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Initial (Noisy) Binary Image', 'FontSize', fontSize);
% Remove small objects.
binaryImage = bwareaopen(binaryImage, 300);
% Display it.
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Cleaned Binary Image', 'FontSize', fontSize);
[labeledImage numberOfObjects] = bwlabel(binaryImage);
blobMeasurements = regionprops(labeledImage,...
'Perimeter', 'Area', 'FilledArea', 'Solidity', 'Centroid');
% Get the outermost boundaries of the objects, just for fun.
filledImage = imfill(binaryImage, 'holes');
boundaries = bwboundaries(filledImage);
% Collect some of the measurements into individual arrays.
perimeters = [blobMeasurements.Perimeter];
areas = [blobMeasurements.Area];
filledAreas = [blobMeasurements.FilledArea];
solidities = [blobMeasurements.Solidity];
% Calculate circularities:
circularities = perimeters .^2 ./ (4 * pi * filledAreas);
% Print to command window.
fprintf('#, Perimeter, Area, Filled Area, Solidity, Circularity\n');
for blobNumber = 1 : numberOfObjects
fprintf('%d, %9.3f, %11.3f, %11.3f, %8.3f, %11.3f\n', ...
blobNumber, perimeters(blobNumber), areas(blobNumber), ...
filledAreas(blobNumber), solidities(blobNumber), circularities(blobNumber));
end
% Say what shape they are.
% IMPORTANT NOTE: depending on the aspect ratio of the rectangle or triangle
% their circularity can go from some minimum number up to a huge number.
for blobNumber = 1 : numberOfObjects
% Outline the object so the user can see it.
thisBoundary = boundaries{blobNumber};
subplot(2, 2, 2); % Switch to upper right image.
hold on;
% Display prior boundaries in blue
for k = 1 : blobNumber-1
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'b', 'LineWidth', 3);
end
% Display this bounary in red.
thisBoundary = boundaries{blobNumber};
plot(thisBoundary(:,2), thisBoundary(:,1), 'r', 'LineWidth', 3);
subplot(2, 2, 4); % Switch to lower right image.
% Determine the shape.
if circularities(blobNumber) < 1.2
message = sprintf('The circularity of object #%d is %.3f,\nso the object is a circle',...
blobNumber, circularities(blobNumber));
shape = 'circle';
elseif circularities(blobNumber) < 1.6
message = sprintf('The circularity of object #%d is %.3f,\nso the object is a square',...
blobNumber, circularities(blobNumber));
shape = 'square';
elseif circularities(blobNumber) > 1.6 && circularities(blobNumber) < 1.8
message = sprintf('The circularity of object #%d is %.3f,\nso the object is an isocoles triangle',...
blobNumber, circularities(blobNumber));
shape = 'triangle';
else
message = sprintf('The circularity of object #%d is %.3f,\nso the object is something else.',...
blobNumber, circularities(blobNumber));
shape = 'something else';
end
% Display in overlay above the object
overlayMessage = sprintf('Object #%d = %s\ncirc = %.2f, s = %.2f', ...
blobNumber, shape, circularities(blobNumber), solidities(blobNumber));
text(blobMeasurements(blobNumber).Centroid(1), blobMeasurements(blobNumber).Centroid(2), ...
overlayMessage, 'Color', 'r');
button = questdlg(message, 'Continue', 'Continue', 'Cancel', 'Continue');
if strcmp(button, 'Cancel')
break;
end
end
  11 个评论
nawaf
nawaf 2016-4-17
Thank you! this is very helpful, appreciated!
Laveena Kewlani
Laveena Kewlani 2016-8-3
Image Analyst, Is there any way to know the circularity of other important shape, hexagon, star etc?

请先登录,再进行评论。


murk hassan memon
murk hassan memon 2018-4-11
Hi Image Analyst, Currently i am working on microscopic blood images in order to detect infected parasites of malaria and for this implementation first i need much data set . i have gone through two different image resources (ASH,CDC) but did not find much data set there. so its my request to you that i you have data set of microscopic blood images(normal,abnormal)then kindly help me out or suggest me another image resources to get from there. waiting for your response

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by