Skip to main content

Image binng in matlab

Here are my own image binning codes. The first one was built on 2015/3/2. And the second one was written on 2018/11/5.

I think I have a big improvement. Ha~

====

function [ox,oy]=binning(x,y,nbins)

% This function is used to bin data to average

% x: x-axis data
% y: y-axis data
% nbins: binning factor
%
% 2015/03/02 
% Renfong

m=max(size(x));
n0=fix(m/nbins);
n1=mod(m,nbins);

if n1==0
ox=zeros(n0,1);
oy=zeros(n0,1);
for ii=1:n0
for jj=1:nbins
ox(ii)=ox(ii)+x((ii-1)*nbins+jj);
oy(ii)=oy(ii)+y((ii-1)*nbins+jj);
end
ox(ii)=ox(ii);
oy(ii)=oy(ii);
end
else
ox=zeros(n0+1,1);
oy=zeros(n0+1,1);
for ii=1:n0
for jj=1:nbins
ox(ii)=ox(ii)+x((ii-1)*nbins+jj);
oy(ii)=oy(ii)+y((ii-1)*nbins+jj);
end
ox(ii)=ox(ii);
oy(ii)=oy(ii);
end
for ii=1:n1
ox(n0+1)=ox(n0+1)+x(n0*nbins+ii);
oy(n0+1)=oy(n0+1)+y(n0*nbins+ii);
end
ox(n0+1)=ox(n0+1)*nbins/n1;
oy(n0+1)=oy(n0+1)*nbins/n1;
end

====

function out=xy_bins(img,nbins)
%
% xy_bins is to bin along x and y directions
% z maintains to its origin dimension.
%
% img: image stack, must be 2D or 3D dataset
% nbins: number of bins, the nbins must be the factor of img x-y dimension
%
% 2018/11/05
% Renfong

[sy,sx,sz]=size(img);
if mod(sy,nbins)~=0
    error('The dimension does match!');
    return;
end
out=zeros(sy/nbins,sx/nbins,sz);
for i=1:sz
    for j=1:nbins
        for k=1:nbins
            out(:,:,i)=out(:,:,i)+img(j:nbins:sy,k:nbins:sx,i);
        end
    end
end

out=out/(nbins)^2;

Comments

Popular posts from this blog

Top hat filter

The top_hat filter can be used to detect the relatively small edges/peaks superimposed on large background signals. The concept came from the EELS workshop during IMC19. Thanks to Prof. Nestor J. Zaluzec. -- // Using Top_hat digital filter to detect the  relatively small edges  //    superimposed on large background signals. // // ref: Ultramicroscopy 18 (1985) 185-190  //      Digital Filters  for Application to Data Analysis in EELS //      by Nestor J. ZALUZEC // Parameters: // win_s: signal window (default:3) // win_b: background window (default:3) //  a_s : amplitude of signal (fixed value) //  a_b : amplitude of background  (fixed value) // Renfong 2018/10/11 // Main function image Top_Hat_Filter(image img, number win_s, number win_b) { // read image string fname=img.GetName() number sx,sy img.getsize(sx,sy) // filter image img2 := imageclone(img)*0 //the area between...

MLLS in matlab

MLLS stands for  multiple linear least squares fitting, which is the common strategy for the solving EELS edge overlapping and which is also built-in the GMS software. The target spectrum Y and the reference spectrum X Y = A * X Assuming Y is 1*256 matrix and we have three reference spectrums, ie, X is 3*256 matrix. So A is 1*3 matrix. The target is to solve A. If Y and X are n*n matrices, we can use the simple formula Y * inv(X) = A * X * inv(X), ie., A = Y * inv(X). However, Y and X are not n*n  matrices, it is necessary to have some trick to solve it. We can multiply the transpose matrix to produce n*n matrix. Y * X' = A * X * X'  (ps X' means the transpose matrix of X) so A = Y * X' * inv(X * X') Here is the Matlab code: =========  % create target spectrum x=0:256; c=[90,120,155]; sig=[5,10,8]; int=[5,10,8]; xn=zeros(size(x)); ref=zeros(length(c),length(x)); factor=rand(size(c))'; for i=1:length(c)     xn=xn+int(i)*ex...

Drift correction in Matlab

In order to improve S/N ratio, microscopist uses several short acquisition time images, and then sum them up. So the drift correction is very important. Here is the demo of how to use Matlab do drift correction. In the first, load an image, and using circshift function to shift the object in the image. Then use fft cross correlation to compute the moving distance. Finally, shift the object to the origial position. Here is the testing code: == % Demo of drift correction % 2018/11/15  by Renfong im1= imread('cameraman.tif');    % reference image [sy,sx]=size(im1); % shift the object im2=circshift(im1,[20,10]);    % the object moved down 20 pixels and moved right 10 pixels. figure(1); subplot(121);imshow(im1); subplot(122);imshow(im2); % Using fft cross correlations to detect the moving distance[1] fftim1=fft2(im1); fftim2=fft2(im2); cc=fftshift(ifft2(fftim1.*conj(fftim2))); [shiftY,shiftX]=find(cc==max(cc(:))...