how to measure boundary length in a binary image?

13 visualizzazioni (ultimi 30 giorni)
Matthew il 11 Nov 2014
Risposto: Rahul punk il 19 Gen 2019
I am trying to measure the total boundary length of the binary image in the following link:
I have tried bwperim, bwarea and regionprops( BW ,'perimeter'), but seems none of them gives an accurate result. I think the hard point in my case is to measure the real length of a inclined line. For example, the actual length of ([0 0 1 1]; [0 1 1 0]; [1 1 0 0]) is 1+2sqrt(2)+1 pixels instead of 6. Function regionprops( BW ,'perimeter') seems working well for closed boundary, but gives unexpected result for disclosed ones. Is there existing functions I can directly use for my case, or you have some other wise methods to address my problem? Thanks a lot forehead.

Risposte (2)

Rahul punk
Rahul punk il 19 Gen 2019
boundaries = bwboundaries(binaryImage);
x = boundaries(:, 1);
y = boundaries(:, 2);
maxDistance = -inf;
for index1 = 1 : length(x)
for index2 = 1 : length(y)
deltaX = x(index1) - x(index2);
deltaY = y(index1) - y(index2);
distance = sqrt(deltaX^2+deltaY^2);
if distance > maxDistance

Rahul punk
Rahul punk il 19 Gen 2019
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 25;
% Get the name of the image the user wants to use.
baseFileName = 'image.JPG';
baseFileName = 'image1.JPG';
% Get the full filename, with path prepended.
folder = []; % Determine where demo folder is (works with all versions).
fullFileName = fullfile(folder, baseFileName);
% Read in a demo image.
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
axis image;
caption = sprintf('Original Gray Scale Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0, 1, 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Rahul', 'NumberTitle', 'Off')
% binarize the image.
binaryImage = imbinarize(grayImage);
% Make sure there is only one blob
binaryImage = bwareafilt(binaryImage, 1);
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
axis on;
axis image;
caption = sprintf('Binary Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
% Label the image
labeledImage = bwlabel(binaryImage);
% Make measurements of bounding box
props = regionprops(labeledImage, 'BoundingBox');
width = props.BoundingBox(3);
height = props.BoundingBox(4);
% Display it with the box overlaid on it.
subplot(2, 2, 3);
imshow(grayImage, []);
axis on;
axis image;
caption = sprintf('Gray Scale Image with box overlaid');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
hold on;
rectangle('Position', props.BoundingBox, 'EdgeColor', 'r', 'LineWidth', 2);
message = sprintf('The width = %f.\nThe height = %f', width, height);


Scopri di più su Image Processing Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by