0

I need to get a 1-D profile of a circular image, for example 256x256 sin(R) image 256x256 sin(R) image

I've written a matlab function for the task but it turns out to be very un-efficient. the function averages over radius intervals of the original images.

matlab profiler reveals that the first line in the for-loop [indxs=find(...)] takes ~86% of the running time.

i need to run the function on a some thousands of simulated images (some larger then 256x256) and it takes very long time to complete.

does anyone knows how can i make this code run faster? maybe someone has another, more efficient way to do the task??

i also tried to convert to function into C++ & mex file using matlab coder but it took longer (x3) to perform the task, might be because the sub-function- "findC" uses some 2D-ffts to find the center of the image.

Thanks you All, Dudas

My Matlab function:

function [sig R_axis Center]= Im2Polar (imR,ch,Center_Nblock)
% Converts Circular image to 1-D sig
% based on true image values w/o interpolation

% Input -
% imR - circular sinuns image
% ch - number of data-points in output signal (sig)
% Center_Nblock - a varible related to the image center finding method

% Output -
% sig - 1D vector of the circular image profile
% R_axis - axis data-points for sig
% Center - image center in pixels


[Mr Nr] = size(imR); % size of rectangular image
[Center]=findC(imR,Center_Nblock);
Xc=Center(1);
Yc=Center(2);

rMax=sqrt((Mr/2)^2 + (Nr/2)^2);

x=[0:1:Mr-1]-Xc+1;
y=[0:1:Nr-1]-Yc+1;

[X,Y]=meshgrid(x,y);
[TH,R] = cart2pol(X,Y);

% Assembling 1-D signal
sig=single([]);
ii=1;
dr=floor(rMax)/ch;
V=dr:dr:floor(rMax);

for v=V
    indxs=find((v-dr)<=R & R<v);**
    sig(ii)=mean(imR(indxs));
    Nvals(ii)=length(indxs);

    ii=ii+1;
end %for v

R_axis=V-dr/2;

end % of function
4

1 に答える 1

1

Following from the comments here's an example of something I might try. Let's work with a 9x9 example. Suppose you have the following annulus.

A =

 0     0     0     0     0     0     0     0     0
 0     0     1     1     1     1     1     0     0
 0     1     1     1     0     1     1     1     0
 0     1     1     0     0     0     1     1     0
 0     1     0     0     0     0     0     1     0
 0     1     1     0     0     0     1     1     0
 0     1     1     1     0     1     1     1     0
 0     0     1     1     1     1     1     0     0
 0     0     0     0     0     0     0     0     0

Then the indices of your sort of mask are, lets say [k n]

 >> [k n]

 ans =

 3     2
 4     2
 5     2
 6     2
 7     2
 2     3
 3     3
 4     3
 6     3
 7     3
 8     3
 2     4
 3     4
 7     4
 8     4
 2     5
 8     5
 2     6
 3     6
 7     6
 8     6
 2     7
 3     7
 4     7
 6     7
 7     7
 8     7
 3     8
 4     8
 5     8
 6     8
 7     8

Now have a 9x9 matrix of zeroes on hand called B, we can shift the whole thing over to the left by one pixel as follows using the formula (i+9*(j-1)) to convert double index to a single index.

 >> B=zeros(9,9);
 >> B((k)+9*(n-2))=1

 B =

 0     0     0     0     0     0     0     0     0
 0     1     1     1     1     1     0     0     0
 1     1     1     0     1     1     1     0     0
 1     1     0     0     0     1     1     0     0
 1     0     0     0     0     0     1     0     0
 1     1     0     0     0     1     1     0     0
 1     1     1     0     1     1     1     0     0
 0     1     1     1     1     1     0     0     0
 0     0     0     0     0     0     0     0     0

Or move down and to the right as follows

 >> B=zeros(9,9);
 >> B((k+1)+9*(n-0))=1

 B =

 0     0     0     0     0     0     0     0     0
 0     0     0     0     0     0     0     0     0
 0     0     0     1     1     1     1     1     0
 0     0     1     1     1     0     1     1     1
 0     0     1     1     0     0     0     1     1
 0     0     1     0     0     0     0     0     1
 0     0     1     1     0     0     0     1     1
 0     0     1     1     1     0     1     1     1
 0     0     0     1     1     1     1     1     0

As long as it doesn't go out of bounds you should be able to shift a single annular mask around with a simple addition to put the center at the image center.

于 2013-03-27T00:56:55.003 に答える