mean value in a sphere

后端 未结 3 1206
面向向阳花
面向向阳花 2021-01-22 14:35

I\'m trying to calculate the mean value of the pixels inside a circle. In the future this needs to be extended to 3D, but for now a 2D sollution would already help me out.

3条回答
  •  轮回少年
    2021-01-22 15:08

    You can use inpolygon, to get the indices which lie inside the circle, once you have those indices you can get your pixels and do what you want.

    M = rand(100); %data
    [nx,ny] = size(M) ;
    [X,Y] = meshgrid(1:ny,1:nx) ;
    
    pos=[20,20];
    r = 5;
    phi=linspace(0,2*pi,100);
    imagesc(M);
    axis image
    hold on
    plot(pos(1),pos(2),'rx')
    xc = pos(1)+r*sin(phi) ;
    yc = pos(2)+r*cos(phi) ;
    plot(xc,yc,'-r');
    % hold off
    %% get indices which are inside the circle
    idx = inpolygon(X(:),Y(:),xc,yc) ;
    xi = X(idx) ; yi = Y(idx) ;
    plot(xi,yi,'.r')
    mypixels = M(idx) ;
    

提交回复
热议问题