function M_rebin = IMAGE_REBINNING(M,bin)
%%% M_rebin = IMAGE_REBINNING(M,bin)
%%% M : Image entree
%%% bin : Facteur de binning

[n,m] = size(M);

if bin>1
    I0=M(1:floor(n/bin)*bin,1:floor(m/bin)*bin);
    I0(isnan(I0)) = 0;
    I0(isinf(I0)) = 0;
    %%%%%%%% Binning de l'image
    fun = @(block_struct) sum(sum(block_struct.data));
    M_rebin = blockproc(I0,[bin bin],fun);
else
    M_rebin = M;
end

return;