Please help me to find Perimeter using regionprops()

3 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2018-10-5
We need your rgb .bmp file because your code extracts color channels in multiple places.

请先登录,再进行评论。

采纳的回答

Akira Agata
Akira Agata 2018-10-5
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 个评论
Akira Agata
Akira Agata 2018-10-5
Seems strange. Did you run my 3-line code with using the binary image you attached in your comment above?

请先登录,再进行评论。

更多回答(2 个)

Walter Roberson
Walter Roberson 2018-10-5
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 个评论
Image Analyst
Image Analyst 2018-10-6
OK, I did a complete, fancy demo for you. See my answer below.

请先登录,再进行评论。


Image Analyst
Image Analyst 2018-10-6
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 个评论
Oman Wisni
Oman Wisni 2018-10-6
@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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Agriculture 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by