Image Processing 2d gaussian...

4 views (last 30 days)
Chris E.
Chris E. on 8 Aug 2013
Hello All,
I'm working on a fast image processing method that can be used for finding the main part of an beam image (like a 2d Gaussian that is slightly stretched) and then clipping it to show only the main beam up close. I'm still running into speed issues and also slight issues in the algorithm, such as false readings... If you run the code with the one titled in the comment line, "%what I want to happen:", it shows a good representation of what I want to happen. If you un-comment the code under, "%does not work all the time, but sometimes it does:", after running it a few times you should see sometimes it is working, sometimes it is not and other times it is kind of working(however not really good). I need to keep it fast (under or around 0.02 sec), yet clip the image. If you have any advice, comments or help, please feel free to help out!!! Any help is great help...
I have the code here:
function ImageClipper()
%what I want to happen:
useImg = customgauss([400 400], 20, 20, 3, 2, 100, [30 50]);
%does not work all the time, but sometimes it does:
% useImg = customgauss([400 400], round(rand*10),...
% round(rand*10), round(rand*3), round(rand*3),...
% 100, [round(rand*100) round(rand*100)]);
%If there were "noise" or other issues
%useImg = imnoise(useImg,'salt & pepper')
%this is the important part, above was to generate some data to use.
%I need to keep it under or about 0.01 seconds
threshold = 10;
mx = max(useImg);
my = max(useImg,[],2);
tx = mode(mx)+threshold;
ty = mode(my)+threshold;
v = mx > tx;
b = (diff(find([1 v ~v(end)]))-1);
n = round(size(b,2)/2);
fh = sum(b(:,1:n))+threshold;
sh = sum(b(:,n:end))+threshold;
tfc = [false(1,fh) true(1,(length(mx)-(fh+sh))) false(1,sh)];
v = my > ty;
b = (diff(find([1; v; ~v(end)]))-1);
n = round(size(b,1)/2);
fh = sum(b(1:n,:))+threshold;
sh = sum(b(n:end,:))+threshold;
tfr = [false(1,fh) true(1,(length(my)-(fh+sh))) false(1,sh)];
function ret = customgauss(gsize, sigmax, sigmay, theta, offset, factor, center)
ret = zeros(gsize);
rbegin = -round(gsize(1) / 2);
cbegin = -round(gsize(2) / 2);
for r=1:gsize(1)
for c=1:gsize(2)
ret(r,c) = rotgauss(rbegin+r,cbegin+c, theta, sigmax, sigmay, offset, factor, center);
function val = rotgauss(x, y, theta, sigmax, sigmay, offset, factor, center)
xc = center(1);
yc = center(2);
theta = (theta/180)*pi;
xm = (x-xc)*cos(theta) - (y-yc)*sin(theta);
ym = (x-xc)*sin(theta) + (y-yc)*cos(theta);
u = (xm/sigmax)^2 + (ym/sigmay)^2;
val = offset + factor*exp(-u/2);

Answers (1)

Image Analyst
Image Analyst on 10 Aug 2013
Why go to all that complicated stuff when all you need to do is to threshold, call regionprops() asking for bounding boxes, and then call imcrop()?
  1 Comment
Chris E.
Chris E. on 10 Aug 2013
because it is slower, I'm looking for simplicity and speed.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!