Applying Regionprops to Only the Region of Interest

4 views (last 30 days)
I would like to use the "regionprops" function on a user-defined ROI from an image to generate a histogram that plots the frequencies of the diameters of the circular objects ONLY within the user-defined ROI. The image, labeled, "Image_No_ROI_Defined", is the output from my code which outlines circular objects from the entire image using "regionprops" and "viscircles" documentation. I had gotten to the point where my code allows the user to draw their ROI from this image and then marks the centroids of the circular objects within the user-defined ROI (See attached "Image_ROI_Defined" image file). Is there any possible way I would be able to use any functions to outline the circles within the user-defined ROI in a different color and then measure the diameters of the circles only within the yellow "ROI" region that have their centroids marked with the yellow crosses? I have attached the original image, the image with all circles outlined, and the image with the ROI I would only like regionprops to analyze to calculate diameter of the circles within the ROI.
Any help would be greatly appreciated. Thank you.

Accepted Answer

Image Analyst
Image Analyst on 28 Dec 2020
Edited: Image Analyst on 28 Dec 2020
Since it looks like you want even partial circles and the only criteria is that the circle centers lie within the polygon the user drew, I'd use inpolygon() to determine which measurements are inside. So if you have xCenters and yCenters arrays, and arrays of vertices, xv and yv, i'd do this
keeperIndexes = false(1, length(xCenters));
for k = 1 : length(xCenters)
if inpolygon(xCenters(k), yCenters(k), xv(k), yv(k))
keeperIndexes(k) = true;
end
end
% Now extract only those that are inside the polygon
roix = xCenters(keeperIndexes);
roiy = yCenters(keeperIndexes);
roiRadii = radii(keeperIndexes);
  32 Comments
Michael DiStefano
Michael DiStefano on 5 Jan 2021
I have similar code that uses inpolygon() to either outline an ROI for further analysis or exclude an ROI to prevent it from being included in further analysis. To make it even easier for the user and to also allow the user to select circles that should not be included in further analysis, I was wondering if there was a way to allow the user to select the circular objects to exclude from further analysis using ginput(), as you had previously mentioned to me through this code below:
message = sprintf('Click points and hit return when done entering all points.');
uiwait(helpdlg(message));
[xUser, yUser] = ginput(); % I think you hit return when done entering all points.
indexesToExclude = zeros(1, length(xCenters));
minDistance = zeros(1, length(xCenters));
for k = 1 : length(xUser)
allDistances = sqrt((xUser(k) - xCenters).^2 + (yUser(k) - yCenters).^2);
[minDistance(k), indexesToExclude(k)] = min(allDistances)
end
However, the code in my previous message, which incorporates this portion of code you had sent, attempts to do that but there is an issue with the last for loop in that it does not exclude the circles that the user selected. I was also considering writing a script that allows the user to outline multiple ROIs to either include or exclude but I thought that selecting circles would be more efficient. If there is any way you can help recommend how I can fix the mistake in my for loop in my previous message so that the user can just select circles to exclude, I would greatly appreciate it. Thank you again.
Image Analyst
Image Analyst on 5 Jan 2021
That will only compare distances to where you clicked, not the entire outline. I think you should use imfreehand() instead of ginput(). See attached demos.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!