Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
image processing: binning

Subject: image processing: binning

From: Norman Fuhrmann

Date: 29 Jan, 2010 21:28:02

Message: 1 of 7

hi there,

i am looking for a fast code to realize software binning. this means reducing the size of an image by combing pixels by adding their intensities.

what i did was writing a for-loop, which is quite slow:

function O=binning(I, dim)
[n m]=size(I);
O=zeros(floor(n/dim), floor(m/dim));
[n m]=size(O);
for i=1:n
    for j=1:m
        O(i,j)=sum(sum(I(i*dim-(dim-1):i*dim, j*dim-(dim-1):j*dim)));
    end
end

because i have huge datasets to bin, I need a fast way for this operation. i don't think that there's a built-in function.. any ideas?

regards,
norman

Subject: image processing: binning

From: us

Date: 29 Jan, 2010 22:57:02

Message: 2 of 7

"Norman Fuhrmann" <n.fuhrmann@gmail.com> wrote in message <hjvjt2$1lr$1@fred.mathworks.com>...
> hi there,
>
> i am looking for a fast code to realize software binning. this means reducing the size of an image by combing pixels by adding their intensities.
>
> what i did was writing a for-loop, which is quite slow:
>
> function O=binning(I, dim)
> [n m]=size(I);
> O=zeros(floor(n/dim), floor(m/dim));
> [n m]=size(O);
> for i=1:n
> for j=1:m
> O(i,j)=sum(sum(I(i*dim-(dim-1):i*dim, j*dim-(dim-1):j*dim)));
> end
> end
>
> because i have huge datasets to bin, I need a fast way for this operation. i don't think that there's a built-in function.. any ideas?
>
> regards,
> norman

one of the solutions
- this might(!) be faster

     m=magic(9);
     len=3;
     r=filter2(ones(len),m,'valid');
     r=r(1:len:end,1:len:end)
%{
% r =
     531 198 378
     216 369 522
     360 540 207
%}

us

Subject: image processing: binning

From: ImageAnalyst

Date: 29 Jan, 2010 23:02:09

Message: 3 of 7

norman:
There is a built-in function called imresize() in the Image Processing
Toolbox. I think it does binning when it downsizes an image. Look it
up. If it doesn't you can use conv2() or imfilter() followed by
subsampling to do it. I think conv2() is also in the image processing
toolbox. These are all optimized for speed as far as I know.

Subject: image processing: binning

From: Matt J

Date: 29 Jan, 2010 23:06:05

Message: 4 of 7



Another solution:


function M=downsamp2d(M,bindims)
%DOWNSAMP2D - simple tool for 2D downsampling
%
% M=downsamp2d(M,bindims)
%
%in:
%
% M: a matrix
% bindims: a vector [p,q] specifying pxq downsampling
%
%out:
%
% M: the downsized matrix

p=bindims(1); q=bindims(2);
[m,n]=size(M); %M is the original matrix

M=sum( reshape(M,p,[]) ,1 );
M=reshape(M,m/p,[]).'; %Note transpose

M=sum( reshape(M,q,[]) ,1);
M=reshape(M,n/q,[]).'; %Note transpose

M=M/(p*q);

Subject: image processing: binning

From: us

Date: 29 Jan, 2010 23:38:06

Message: 5 of 7

"Norman Fuhrmann" <n.fuhrmann@gmail.com> wrote in message <hjvjt2$1lr$1@fred.mathworks.com>...
> hi there,
>
> i am looking for a fast code to realize software binning. this means reducing the size of an image by combing pixels by adding their intensities...

now, let's look at the timing of the three solutions
the code snippet below shows this on a
wintel sys: ic2/2*2.6ghz/2gb/winxp.sp3.32/r2009b
for binning a 2000x2000 by 2x2
       39.634 % <- nf original
       0.6173 % <- mj reshaper WINNER
       2.4848 % <- us filter2
just a thought...
us

function ng111(varargin)
     nt=10;
     m=magic(2000);
     len=2;
     t=nan(3,1);
     tic;
for i=1:nt
     r1=f1(m,len); % <- nf
end
     t(1)=toc;
     tic;
for i=1:nt
     r2=f2(m,[len,len]); % <- mj
end
     t(2)=toc;
     tic;
for i=1:nt
     r3=f3(m,len); % <- us
end
     t(3)=toc;
     disp(t);
     disp(isequal(r1,r2,r3));
end
% nf
function O=f1(I, dim)
     [n m]=size(I);
     O=zeros(floor(n/dim),floor(m/dim));
     [n m]=size(O);
for i=1:n
for j=1:m
     O(i,j)=sum(sum(I(i*dim-(dim-1):i*dim,j*dim-(dim-1):j*dim)));
end
end
end
% mj
function M=f2(M,bindims)
     p=bindims(1);
     q=bindims(2);
     [m,n]=size(M); %M is the original matrix
     M=sum(reshape(M,p,[]),1);
     M=reshape(M,m/p,[]).'; %Note transpose
     M=sum(reshape(M,q,[]) ,1);
     M=reshape(M,n/q,[]).'; %Note transpose
% M=M/(p*q); % <- note: OP did not scale the output
end
% us
function r=f3(r,len)
     [nr,nc]=size(r);
     r=filter2(ones(len),r,'valid'); % <- if scaling: ones(len)./(len*len)
     r=r(1:len:nr,1:len:nc);
end

Subject: image processing: binning

From: Matt J

Date: 30 Jan, 2010 00:01:08

Message: 6 of 7

"us " <us@neurol.unizh.ch> wrote in message <hjvrgu$2f1$1@fred.mathworks.com>...
> "Norman Fuhrmann" <n.fuhrmann@gmail.com> wrote in message <hjvjt2$1lr$1@fred.mathworks.com>...
> > hi there,
> >
> > i am looking for a fast code to realize software binning. this means reducing the size of an image by combing pixels by adding their intensities...
>
> now, let's look at the timing of the three solutions
> the code snippet below shows this on a
> wintel sys: ic2/2*2.6ghz/2gb/winxp.sp3.32/r2009b
> for binning a 2000x2000 by 2x2
> 39.634 % <- nf original
> 0.6173 % <- mj reshaper WINNER
> 2.4848 % <- us filter2
> just a thought...
> us
==================

Since I'm not sure who has the Image Processing Toolbox, I'll just add a timing comparison between reshaping and imresize()

>> A=magic(2000);
 
>> tic; B=downsamp2d(A,[2,2]); toc, clear B
Elapsed time is 0.111128 seconds.
 
>> tic; B=imresize(A,.5); toc, clear B
Elapsed time is 0.246488 seconds.

which I guess is because imresize() handles both integer and non-integer downsampling, and has additional overhead due to that.

Also note that none of the above solutions are applicable to large sparse arrays. Neither filter() nor imresize() is defined for sparse arrays, and reshaping sparse arrays is memory intensize.

The only way I've found to bin large sparse arrays is by matrix multiplication, see

http://www.mathworks.com/matlabcentral/newsreader/view_thread/269676
 

Subject: image processing: binning

From: Norman Fuhrmann

Date: 30 Jan, 2010 08:14:05

Message: 7 of 7

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hjvss4$pdk$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <hjvrgu$2f1$1@fred.mathworks.com>...
> > "Norman Fuhrmann" <n.fuhrmann@gmail.com> wrote in message <hjvjt2$1lr$1@fred.mathworks.com>...
> > > hi there,
> > >
> > > i am looking for a fast code to realize software binning. this means reducing the size of an image by combing pixels by adding their intensities...
> >
> > now, let's look at the timing of the three solutions
> > the code snippet below shows this on a
> > wintel sys: ic2/2*2.6ghz/2gb/winxp.sp3.32/r2009b
> > for binning a 2000x2000 by 2x2
> > 39.634 % <- nf original
> > 0.6173 % <- mj reshaper WINNER
> > 2.4848 % <- us filter2
> > just a thought...
> > us
> ==================
>
> Since I'm not sure who has the Image Processing Toolbox, I'll just add a timing comparison between reshaping and imresize()
>
> >> A=magic(2000);
>
> >> tic; B=downsamp2d(A,[2,2]); toc, clear B
> Elapsed time is 0.111128 seconds.
>
> >> tic; B=imresize(A,.5); toc, clear B
> Elapsed time is 0.246488 seconds.
>
> which I guess is because imresize() handles both integer and non-integer downsampling, and has additional overhead due to that.
>
> Also note that none of the above solutions are applicable to large sparse arrays. Neither filter() nor imresize() is defined for sparse arrays, and reshaping sparse arrays is memory intensize.
>
> The only way I've found to bin large sparse arrays is by matrix multiplication, see
>
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/269676
>

thanks a lot guys! matt's code is what i was looking for!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us