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

4 次查看(过去 30 天)
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.

回答(1 个)

KSSV
KSSV 2018-6-22
[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 ;

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by