Please help me to find Perimeter using regionprops()
이전 댓글 표시
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
2018년 10월 5일
Can you attach the array to be analyzed?
Oman Wisni
2018년 10월 5일
Image Analyst
2018년 10월 5일
No, not a screenshot. Please attach the binary image that you passed into bwlabel(), bwconncomp(), or regionprops().
Oman Wisni
2018년 10월 5일
Walter Roberson
2018년 10월 5일
We need your rgb .bmp file because your code extracts color channels in multiple places.
Oman Wisni
2018년 10월 5일
편집: Oman Wisni
2018년 10월 5일
채택된 답변
추가 답변 (2개)
Walter Roberson
2018년 10월 5일
1 개 추천
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
Oman Wisni
2018년 10월 5일
편집: Oman Wisni
2018년 10월 5일
Walter Roberson
2018년 10월 5일
I suggest you bwareafilt() to remove the insignificant areas.
I wonder if it would be better to imclose() instead of medfilt2() ?
In the code you posted, you are concentrating on finding the ROI. Once you have the ROI you should use it to extract the corresponding portions of the color image, which you can then do texture analysis of (perhaps after converting to grayscale.)
Oman Wisni
2018년 10월 6일
Image Analyst
2018년 10월 6일
I don't see any reason to call either medfilt2() or imclose(). Simply call bwareafilt() and imfill(). Don't call im2uint8(), medfilt2(), or imclose(). Get rid of all that nonsense and just use my original leaf segmentation code. I see bits and pieces of it in there but you or someone, for some reason, tried to "improve" it and ended up ruining it.
Oman Wisni
2018년 10월 6일
Image Analyst
2018년 10월 6일
OK, I did a complete, fancy demo for you. See my answer below.
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.


카테고리
도움말 센터 및 File Exchange에서 Image Filtering에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




