MATLAB Answers

Unable to find the number of pixels on the boundary of a ROI

조회 수: 1(최근 30일)
Warid Islam
Warid Islam 2021년 6월 30일
댓글: Image Analyst 2021년 7월 1일
I want to find the number of pixels on the boundary of a ROI. I also want to find the number of pixels inside the ROI. I tried the regionprops method but it is throwing me an error message. Any suggestions would be appreciated.
myFolder = 'D:\regionGrowing_MLT\newim\Segmentation Results';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray = imread(fullFileName);
s = regionprops(imageArray,'Area','Perimeter');
The following error message is displayed:
Unable to perform assignment because the size
of the left side is 1-by-1 and the size of
the right side is 5-by-1.
Error in im3d (line 24)

채택된 답변

Image Analyst
Image Analyst 2021년 6월 30일
Try this:
perimImage = bwperim(imageArray);
numPerimPixels = nnz(perimImage);
boundaries = bwboundaries(imageArray);
numPerimPixels = 0;
for k = 1 : length(boundaries)
thisBoundary = boundaries{k};
numPerimPixels = numPerimPixels + size(thisBoundary, 1);
  댓글 수: 2
Image Analyst
Image Analyst 2021년 7월 1일
bwarea gives an area weighted by the shape of the boundary, while regionprops() or nnz() is strictly a pixel count. Just depends on how you want it.
for this image
0 1
1 1
What is the area? Is it 3 pixels? Or is it half a pixel? Neither is wrong - it's just how you interpret "area". Do you consider a pixel like a little block or tile? Or are you going from pixel center to pixel center.
What is the length of the 1 region here:
0 1 1 1 0
Is it 3? Or is it 2 (going center to center)?

댓글을 달려면 로그인하십시오.

추가 답변(0개)




Community Treasure Hunt

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

Start Hunting!

Translated by