function c = findcone(a,K)
% c = findcone(a,K)
%     a: image
%     K: window size
[m n] = size(a);
k = (K-1)/2;
c = zeros(m,n);
x = 0:1:k;
maxvar = 0;
for indexi = 1+k:m-k
  for indexj = 1+k:n-k
    d0 = a(indexi,indexj:indexj+k);
    c0 = polyfit(x,d0,1);
    d2 = a(indexi-k:indexi,indexj)';
    c2 = polyfit(x,d2,1);
    d4 = a(indexi, indexj-k:indexj);
    c4 = polyfit(x,d4,1);
    d6 = a(indexi:indexi+k, indexj)';
    c6 = polyfit(x,d6,1);
    cslope = (c0(1,1) - c2(1,1) - c4(1,1) + c6(1,1))/4;
    var = (cslope-c0(1,1))^2 + (cslope+c2(1,1))^2 + (cslope+c4(1,1))^2 ...
        + (cslope-c6(1,1))^2;
    c(indexi,indexj) = var;
    if var > maxvar
      maxvar = var;
    end
  end
end
if maxvar>eps
   c = c/maxvar;
end
