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;
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
Post a Comment