mean value in a sphere

后端 未结 3 1181
面向向阳花
面向向阳花 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:07

    You can also use rangesearch to get the points lying within the given radius of the circle. As below:

    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
    %% Use nearest neighbour search
    idx = rangesearch([X(:),Y(:)],pos,r) ;
    xi = X(idx{1}) ; yi = Y(idx{1}) ;
    plot(xi,yi,'.r')
    mypixels = M(idx{1}) ;
    

提交回复
热议问题