Please help me to find Perimeter using regionprops()

9 views (last 30 days)
I no get value of the perimeter in use regionprops().
I already trying another way :
1. Perimeter = stats.Perimeter;
2. Allperimeter = [blobMeasurenment.Perimeter];
But the value still 0.
Do I wrong ? Please help me
  6 Comments
Walter Roberson
Walter Roberson on 5 Oct 2018
We need your rgb .bmp file because your code extracts color channels in multiple places.

Sign in to comment.

Accepted Answer

Akira Agata
Akira Agata on 5 Oct 2018
Please set 'Perimeter' to the 2nd input argument of the regionprops function. The following is an example.
I = imread('3_closing.bmp');
BW = imbinarize(I);
s = regionprops(BW,{'Area','Centroid','Perimeter'});
  4 Comments
Akira Agata
Akira Agata on 5 Oct 2018
Seems strange. Did you run my 3-line code with using the binary image you attached in your comment above?

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 5 Oct 2018
Your code does im2uint8(binaryImage) and works with the result. When that gets passed into regionprops(), because it is datatype uint8 rather than logical or struct, it falls into the numeric array case, which indicates to regionprops that you are providing a label matrix. The values are all numeric 0 or numeric 255, so regionprops is returning information about all of the labels 1, 2, 3, ... 255 (the maximum of your label value.) That is why your returned table has 255 rows.
Now, since it is a label matrix and all of the non-zero values are the same, then all of the non-zero locations are to be treated as belonging to a single object. That distorts your statistics because you have discontinuous areas. And you encounter the limitation documented for Perimeter: "If the image contains discontiguous regions, regionprops returns unexpected results."
I see no good reason for you to im2uint8() at any point. Leave the array as logical.
I also see that your file output logic is messed up.
Your medfilt2 happens to leave 0 at the four corners, and when you imcomplement those four corners become active pixels, so you get 5 total regions, 4 of which are single pixel and the other is the region of interest.
Repaired code attached -- though I did not touch the logic about those 4 corners.
  6 Comments
Image Analyst
Image Analyst on 6 Oct 2018
OK, I did a complete, fancy demo for you. See my answer below.

Sign in to comment.


Image Analyst
Image Analyst on 6 Oct 2018
Oman, try this:
% Mask a leaf out of an RGB image.
clc; % Clear the command window.
clear all;
close all;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
%===============================================================================
% Read in leaf color demo image.
folder = pwd
baseFileName = 'base1.bmp';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% 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.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the original color image.
subplot(2, 4, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Enlarge figure to full screen.
set(gcf, 'Name', 'Color Channels', 'Units', 'Normalized', 'Outerposition', [0, 0.05, 1, 0.95]);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Histogram the images and show the histograms.
% Display red channel.
subplot(2, 4, 2);
imshow(redChannel);
impixelinfo; % Let user mouse around over the image to see the gray levels in a static text label.
title('Red Channel', 'FontSize', fontSize);
% Take histogram and display it.
subplot(2, 4, 6);
imhist(redChannel); % Will produce a plot.
[countsR, binLocationsR] = imhist(redChannel); % Will not produce a plot, but will give data back.
grid on;
title('Histogram of Red Channel', 'FontSize', fontSize);
% Display green channel.
subplot(2, 4, 3);
imshow(greenChannel);
impixelinfo; % Let user mouse around over the image to see the gray levels in a static text label.
title('Green Channel', 'FontSize', fontSize);
% Take histogram and display it.
subplot(2, 4, 7);
imhist(greenChannel); % Will produce a plot.
[countsG, binLocationsG] = imhist(greenChannel); % Will not produce a plot, but will give data back.
grid on;
title('Histogram of Green Channel', 'FontSize', fontSize);
% Display blue channel.
subplot(2, 4, 4);
imshow(blueChannel);
impixelinfo; % Let user mouse around over the image to see the gray levels in a static text label.
title('Blue Channel', 'FontSize', fontSize);
% Take histogram and display it.
h8 = subplot(2, 4, 8);
imhist(blueChannel); % Will produce a plot.
[countsB, binLocationsB] = imhist(blueChannel); % Will not produce a plot, but will give data back.
grid on;
title('Histogram of Blue Channel', 'FontSize', fontSize);
% Plot histograms of all three.
subplot(2, 4, 5);
plot(binLocationsR, countsR, 'r-', 'LineWidth', 2);
hold on;
plot(binLocationsG, countsG, 'g-', 'LineWidth', 2);
plot(binLocationsB, countsB, 'b-', 'LineWidth', 2);
grid on;
title('All 3 Histograms', 'FontSize', fontSize);
% Create a mask of the leaf only.
leafMask = ~imbinarize(blueChannel);
% Get the threshold
otsuThreshold = 255 * graythresh(blueChannel);
% Put a line up at the threshold over the blue channel.
hold on;
line([otsuThreshold, otsuThreshold], ylim, 'Color', 'c', 'LineWidth', 2)
axes(h8); % Using suplot(2, 4, 8) blows away the plot for some reason.
hold on;
hold on;
line([otsuThreshold, otsuThreshold], ylim, 'Color', 'c', 'LineWidth', 2)
% Fill holes.
leafMask = imfill(leafMask, 'holes');
% Get rid of any possible small noise blobs.
leafMask = bwareafilt(leafMask, 1);
% Display the mask image.
figure;
subplot(2, 2, 1);
imshow(leafMask);
title('Leaf Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Get the background mask
backgroundMask = ~leafMask;
subplot(2, 2, 2);
imshow(backgroundMask);
title('Background Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Mask out the leaf, leaving only the background.
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(leafMask, 'like', rgbImage));
% Display the mask image.
subplot(2, 2, 3);
imshow(maskedRgbImage);
title('Leaf-Only Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Mask out the background, leaving only the leaf.
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(~leafMask, 'like', rgbImage));
% Display the mask image.
subplot(2, 2, 4);
imshow(maskedRgbImage);
title('Background-Only Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Enlarge figure to half screen.
set(gcf, 'Name', 'Masks', 'Units', 'Normalized', 'Outerposition', [0.25, 0.25, .5, 0.5]);
Don't be afraid that it's long. It's just that I put in lots of comments to help you understand each step, and I put in lots of visualizations so that you can understand what's happening at each step. You should be able to easily follow it.
  1 Comment
Oman Wisni
Oman Wisni on 6 Oct 2018
@mr.Analyst, thank you very much for your help, a lot of explain and knowledge from you and your team. Thanks sir, and I follow every steps you said.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!