function c = coocr(f,k)
%
% coocr: returns the 0 to 1 and 1 to 0 transition count in a k by k window
%
% On input:
%     f: 2D image function (binary gray levels)
%     k: look in k by k window surrounding pixel
% On output:
%     c: 2D image with value of 0-1 and 1-0 transitions in window
%

[m,n] = size(f);
c = zeros(m,n);
k2 = (k-1)/2;
ksqr = k*k;

for i1 = k2+1:m-k2
  for j1 = k2+1:n-k2
   if f(i1,j1)>0
    cooc = zeros(2,2);
    for i2 = -k2:k2
      for j2 = -k2:k2-1
        val1 = f(i1+i2,j1+j2)+1;
        val2 = f(i1+i2,j1+j2+1)+1;
	cooc(val1,val2) = cooc(val1,val2) + 1;
      end
    end
      c(i1,j1) = (cooc(1,2)+cooc(2,1))/ksqr;
    end
   end
  end
end
