# Perimeter of a specific area?

17 views (last 30 days)
Steven on 24 Dec 2013
Commented: Image Analyst on 24 Dec 2013
Hi. I want to calculate the perimeter of the object below:
I used bwperim to get this:
but I really want is indeed the red region specified here (only the arc!):
How I can do so?
Can MATLAB automatically do it or I have to ask the user to select it?
Thanks so much!
Steven

Image Analyst on 24 Dec 2013
First of all, invert and call bwareaopen() to get rid of the small specks, then invert to get back the original again. Then you can get a perimeter without the little noise bits.
But I wouldn't use bwperim because I'd want the boundary coordinates in order so I can find the kinks. So I'd use bwboundaries() to get the x,y coordinates in order as it goes around the curve.
Then you can use Roger Stafford's kink finding code in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_do_I_find_.22kinks.22_in_a_curve.3F to find the 3 "kinks" in your curve. Once you know that, you can split up the boundary coordinates into 3 segments. Of course one of them will be on each end of the array. So you'll have (part of segment1),(all of segment2),(all of segment3),(remaining part of segment1) as you traverse the boundary coordinates. Does that make sense. Finally, for robustness you'll have to check for the case where a kink pixel lies exactly at the first coordinate. That's not too likely but if you want bulletproof code, you need to check for it and be prepared to handle that case.

Show 1 older comment
Image Analyst on 24 Dec 2013
It's not difficult. Plus Roger's code is right there in the FAQ. Just append it to my code below. This should give you a good start:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Users\Steven\Documents\Temporary';
baseFileName = '1.jpg';
% 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
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale 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', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Get rid of black specks
binaryImage = grayImage < 128;
binaryImage = bwareaopen(binaryImage, 1000);
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
axis on;
title('Binary Image', 'FontSize', fontSize);
boundaries = bwboundaries(binaryImage);
x = boundaries{1}(:,2);
y = boundaries{1}(:,1);
% Display the boundaries over the binary image.
subplot(2, 2, 3);
imshow(binaryImage, []);
axis on;
title('Binary Image', 'FontSize', fontSize);
hold on;
plot(x, y, 'r-', 'LineWidth', 4);
uiwait(msgbox('Now call Rogers kink-finding code'));
Steven on 24 Dec 2013
Thanks again. I did so but I did not understand these three segments you mentioned and how to handle them is difficult.
Also I used bwperim since you kindly suggested to me somewhere else:
But since I have too many different cases, I am not sure how to deal with them all, so that as you said I can have a bulletproof code.
Thanks.
Image Analyst on 24 Dec 2013
Well, you just have to decide on a case by case basis whether you want to use bwperim or bwboundaries. I use both depending on which seems better and easier.
Now the boundary is a list of (x,y) coordinates, right? And you have 3 segments, right? The two straight ones and the curved one. So what happens if the list of coordinates starts right in the middle of one of the segments? Well, that segment would be split into two, one for the start of the coordinate list, and the remainder would be at the end of the coordinate list. Does that explain it better?