function rebi = RebinImage(arr,nr)


[nx,ny] = size(arr);

rebi = zeros(fix(nx/nr),fix(ny/nr));

x = 1;
y = 1;
ri = 1;
rj = 1;

nr2 = nr*nr;

while y < ny-nr
  x = 1;
  ri = 1;
  toy = y + nr - 1;
  while x < nx-nr
    sum = 0;
    tox = x+nr-1;
    for i=x:tox
        for j=y:toy
           %fprintf(1,'ri=%d, rj=%d  x=%d, y=%d, i=%d j=%d\n',ri,rj,x,y,i,j);
           sum = sum + arr(i,j);
        end
    end
    rebi(ri,rj) = sum/nr2;
    ri = ri + 1;
    x =  x + nr;
  end % of a row
  
  y = y + nr;
  rj = rj + 1;
  
end 


