How do I calculate number of white pixels inside ROI?
1 view (last 30 days)
Show older comments
I'm trying to calculate the number of white pixels in my roi of the last image which has already been segmented and converted to binary. I need the number of white pixels within the roi and not the entire image. I've copied my code below. The area im having issues with is commented ROI. My image is in the answers section.
clc;
close all;
clear;
% Get dimensions of image
grayImage = imread('viralplaque.jpg');
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
subplot(2, 3, 1);
imshow(grayImage,[]);
title('grayscale');
% Binarize image by thresholding
mask = grayImage > 90;
subplot(2, 3, 2);
imshow(mask);
title('binary')
% Get rid of small blobs
mask = bwareaopen(mask, 50);
labeledImage = bwlabel(mask,4);
% Find areas and perimeters
stats = regionprops(labeledImage,'Area', 'Perimeter');
allAreas = [stats.Area];
sortedAreas = sort(allAreas,'descend');
allPerimeters = [stats.Perimeter];
% Compute circularities
circularities = allPerimeters .^ 2 ./ (4 * pi * allAreas)
sortedC = sort(circularities, 'descend');
% Keep only blobs that are nowhere close to circular or compact.
minAllowableCircularity = .5;
keeperIndexes = find(circularities >= minAllowableCircularity);
mask = ismember(labeledImage, keeperIndexes);
% Display the mask image.
subplot(2, 3, 3);
imshow(mask);
title('removeWhite')
% Get rid of black islands
subplot(2, 3, 4);
maskFinal = ~bwareaopen(~mask, 1000);
imshow(maskFinal);
title('removeBlack')
% Plaque count
Rmin = 7;
Rmax = 11;
[centers,radii] = imfindcircles(maskFinal,[Rmin Rmax],'objectpolarity','bright','sensitivity',0.96,'edgethreshold',0.1);
subplot(2, 3, 5)
imshow(maskFinal);
title('plaqueCount');
plaques = viscircles(centers,radii,'color','r');
length(centers)
% ROI
Rmin2 = 150;
Rmax2 = 300;
[center,radius] = imfindcircles(maskFinal,[Rmin2 Rmax2],'ObjectPolarity','dark','sensitivity',0.95);
subplot (2, 3, 6);
imshow(maskFinal);
title('roi');
roi = images.roi.Circle(gca,'Center',center, 'Radius',0.99*radius)
Answers (1)
Madhav Thakker
on 21 Aug 2020
Hi
You can use the inpolygon function to check whether a query point lies inside the polygon. You can make a polygon from your ROI and count the number of white pixels that lie inside the polygon.
I am attaching the code for better understanding.
xc = roi.Center(1);
yc = roi.Center(2);
n = 200;
theta = (0:n-1)*(2*pi/n);
x = xc + r*cos(theta);
y = yc + r*sin(theta);
P = polyshape(x,y);
xpoly = P.Vertices(:,1);
ypoly = P.Vertices(:,2);
[xquery, yquery] = find(maskFinal);
inpolygon(xquery, yquery, xpoly, ypoly);
inpolygon(xquery, yquery, xpoly, ypoly);
disp(nnz(inpolygon(xquery, yquery, xpoly, ypoly)))
Hope this helps.
inpolygon documentation for better understanding –
0 Comments
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!