function [newval, strengths] = laserfind(img) img = img .^ 3; %zeroth pass: blur image and locate laser img_blurred = gaussianBlur(img,4); [maxs, xcoords] = max(img_blurred, [], 2); figure(1); clf; plot(xcoords, 1:size(img,1),'r-'); %now multiply image by gaussians centered at that x-coord sigma = 10 x = meshgrid(1:size(img,2),1:size(img,1)); %test = x-repmat(xcoords,[1 size(img,2)]); %imagesc(test); colorbar;pause; gaussian = (1./sqrt(2*pi)./sigma).*exp(-(x-repmat(xcoords,[1 size(img,2)])).^2./(2*sigma^2)); %color_img = zeros([size(img) 3]); %color_img(:,:,3) = (img - min(min(img)))./(max(max(img)) - min(min(img))); %color_img(:,:,1) = (img - min(min(img)))./(max(max(img)) - min(min(img))); img = img.*gaussian; color_img(:,:,2) = gaussian ./ max(max(gaussian)); %imagesc(color_img); %first pass: find moment of each row strengths = sum(img,2); xcoords = sum(img .* x, 2) ./ strengths; figure(1); clf; imshow(img); axis image; hold on; %second pass: lowpass results kernel = gaussian_1d(5, 50); kernel = kernel / sum(kernel); size(kernel) weighted = xcoords .* strengths; newval = conv2(weighted, kernel', 'same'); newscal = conv2(strengths, kernel', 'same'); newval = newval ./ newscal; thresh = 0.05 * max(newscal); newval(find(newscal < thresh)) = NaN; plot(newval, 1:size(img,1),'g-');