From: Frank on
Hi all!

I would like to ask you if you know, given a 2D matrix, how to calculate the average value over the data which are on a circle of a certain radius r from the center of the image or any other given pixel the user would select as a center of his data.

Note: I don't care at all (since I have many points and this are data coming from the real world) about the unavoidable approximation needed to find points on a circle in a cartesian matrix x-y.

Thank you very much in advance

Frank
From: Bruno Luong on
% Image
A=peaks(40)
A=A(1:end-1,:);

% circle pixel-coordinates
xc = 15;
yc = 18;
r = 3; % radius

% Engine
y=1:size(A,1);
x=1:size(A,2);
[X Y]=meshgrid(x,y);

% This assume the circle falls *entirely* inside the image
R2 = (X-xc).^2+(Y-yc).^2;
c = contourc(x,y,R2,[0 0]+r^2);
c = round(c(:,2:end)); % pixels located ~ on circle
Ac = A(sub2ind(size(A),c(2,:),c(1,:))); % extract value
val = mean(Ac) % mean

% Bruno
From: Frank on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hrd4o1$64a$1(a)fred.mathworks.com>...
> % Image
> A=peaks(40)
> A=A(1:end-1,:);
>
> % circle pixel-coordinates
> xc = 15;
> yc = 18;
> r = 3; % radius
>
> % Engine
> y=1:size(A,1);
> x=1:size(A,2);
> [X Y]=meshgrid(x,y);
>
> % This assume the circle falls *entirely* inside the image
> R2 = (X-xc).^2+(Y-yc).^2;
> c = contourc(x,y,R2,[0 0]+r^2);
> c = round(c(:,2:end)); % pixels located ~ on circle
> Ac = A(sub2ind(size(A),c(2,:),c(1,:))); % extract value
> val = mean(Ac) % mean
>
> % Bruno

Very nice and clean solution! Thank you very much! By the way, may I ask you if there is an easy way to directly plot a 2D map (like with pcolor) and then to put the circle on it, to show where the average has been calculated? Is it possible to extend it to elliptic path or other more fancy shapes, like truncated parabolas and so on?

Thank you very much once more

Frank
From: Bruno Luong on
"Frank " <francesco.manni01(a)fastwebnet.it> wrote in message <hrecqf$44g$1(a)fred.mathworks.com>...
> By the way, may I ask you if there is an easy way to directly plot a 2D map (like with pcolor) and then to put the circle on it, to show where the average has been calculated?

Sure.

>Is it possible to extend it to elliptic path or other more fancy shapes, like truncated parabolas and so on?

Yes, I add the use of a function contour2poly() which can conveniently handle level curve with multiple connected parts:

% Image
A = peaks(100);

% ellipse pixel-coordinates
xc = 15;
yc = 18;
rx = 80; % half axis length
ry = 30; % half axis length

% Engine
y=1:size(A,1);
x=1:size(A,2);
[X Y]=meshgrid(x,y);

MAP = ((X-xc)/rx).^2+((Y-yc)/ry).^2-1;

c = contourc(x,y,MAP,[0 0]);
% from http://www.mathworks.com/matlabcentral/newsreader/view_thread/277426
c = contour2poly(c);

xpixel = round(cat(2,c.x));
ypixel = round(cat(2,c.y));
loc = unique([xpixel(:) ypixel(:)],'rows'); % <- this might reduce bias of the mean
Ac = A(sub2ind(size(A),loc(:,2),loc(:,1))); % extract value

val = mean(Ac) % mean

% Graphic
imagesc(A);
hold on
plot(xpixel,ypixel,'.w')

% Bruno
From: Frank on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hreoa0$5us$1(a)fred.mathworks.com>...
> "Frank " <francesco.manni01(a)fastwebnet.it> wrote in message <hrecqf$44g$1(a)fred.mathworks.com>...
> > By the way, may I ask you if there is an easy way to directly plot a 2D map (like with pcolor) and then to put the circle on it, to show where the average has been calculated?
>
> Sure.
>
> >Is it possible to extend it to elliptic path or other more fancy shapes, like truncated parabolas and so on?
>
> Yes, I add the use of a function contour2poly() which can conveniently handle level curve with multiple connected parts:
>
> % Image
> A = peaks(100);
>
> % ellipse pixel-coordinates
> xc = 15;
> yc = 18;
> rx = 80; % half axis length
> ry = 30; % half axis length
>
> % Engine
> y=1:size(A,1);
> x=1:size(A,2);
> [X Y]=meshgrid(x,y);
>
> MAP = ((X-xc)/rx).^2+((Y-yc)/ry).^2-1;
>
> c = contourc(x,y,MAP,[0 0]);
> % from http://www.mathworks.com/matlabcentral/newsreader/view_thread/277426
> c = contour2poly(c);
>
> xpixel = round(cat(2,c.x));
> ypixel = round(cat(2,c.y));
> loc = unique([xpixel(:) ypixel(:)],'rows'); % <- this might reduce bias of the mean
> Ac = A(sub2ind(size(A),loc(:,2),loc(:,1))); % extract value
>
> val = mean(Ac) % mean
>
> % Graphic
> imagesc(A);
> hold on
> plot(xpixel,ypixel,'.w')
>
> % Bruno

Very nice and clean solution once more! Do you have any suggestion about how to do it in a simpler case, which is the one of an average on a line profile with a generic orientation, which means oblique with respect to the x-y square axis reference of a 2d matrix?

Thank you very much once more

Frank
 |  Next  |  Last
Pages: 1 2
Prev: pcolor plot refresh
Next: suppressing plots