How to create a grid of squares inside a circle, assign them and get the sum of pixels inside the squares?

4 views (last 30 days)
Hello fellows. I want to create a grid of squares within a circle like the image attached. The condition is to consider only total squares as the image, assign them, and get the sum of pixels within each square. I want to overlay this circle with the grids in a gray image. I have the following code to build the circle mask:
if true
%build circular mask
clearvars Mask BW H x_center y_center
data = imread(whateverimage);
x_center = fix(size(data,1)/2);
y_center = fix(size(data,2)/2);
H = imellipse(gca, [ 16 17 90.2 90.2 ] )
wait(H);
BW = createMask(H);
Mask(:,:,1)=BW;
I1=single(data).*Mask;
figure(3), imshow(I1(:,:,1),'DisplayRange', [],'InitialMagnification', 'fit');
end
The circle ROI is fine. I'm stuck with this grid in how to fit it inside the circle ROI and quantify inside the squares.
Thanks in advance. Best regards.

Answers (1)

KSSV
KSSV on 22 Jun 2018
[X,Y,Z] = peaks(500) ;
% center of circle and Radius
C = [mean(unique(X)) mean(unique(Y)) ] ;
R = 1.5 ;
% make circle
th = linspace(0,2*pi,500) ;
xc = C(1)+R*cos(th) ; yc = C(2)+R*sin(th) ;
% get pixels inside circle
idx = inpolygon(X(:),Y(:),xc,yc) ;
Z(~idx) = NaN ;
iwant = nansum(Z(~idx))
figure
hold on
pcolor(X,Y,Z) ;
shading interp ;

Categories

Find more on Images 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!