clear all
clc
I=imread('z.png');
I=uint8(I);
%I=rgb2gray(I);

%%%%%%%%%%%%%%%%%%%%%%%%%%


prah=150;

I=uint8(conv2(I,fspecial('average',5)));

R=zeros(size(I,1),size(I,2));

%R= convolution(I,[0,0,0;0,1,0;0,0,0]);
%R= convolution(I,[1,1,1;1,1,1;1,1,1])/9;


%R= convolution(I,[1,0,-1;1,0,-1;1,0,-1]);


%I= convolution(I,[-1,-2,-1;0,0,0;1,2,1]); -sobel prava derivace
XX= convolution2(I,[1,2,3;1,-2,1;-3,-2,-1]);

Ix= convolution(I,[1,1,1;0,0,0;-1,-1,-1])/6;
Iy= convolution(I,[1,0,-1;1,0,-1;1,0,-1])/6;

Ix=Ix.*Ix;
Iy=Iy.*Iy;

Ixy=Ix.*Iy;


Ix=conv2(Ix,fspecial('gaussian',3));
Iy=conv2(Iy,fspecial('gaussian',3));
Ixy=conv2(Ixy,fspecial('gaussian',3));


prah=150000;
k=0.05;
 for i=1:size(I,1)
     for j=1:size(I,2)
         A = Ix(i,j);
         B =Iy(i,j);
         C =Ixy(i,j);
         %Metoda 1.
         H = (A * B - C * C) - (k * ((A + B) * (A + B)));
         
         %metoda 2.
%          d=(A*B)-(2*C);
%          t=A+B;
%          H=d-(0.5*t);
          H=abs(H);
         
         %if(H>prah)
            R(i,j)=H;
        %end
     end
 end
 
 
 %r- polomer okna ve kterem se muze vyskytovat maximalne jedno maximum
 r=15;
 

 
 
  for i=r+1:size(R,1)-r
     for j=r+1:size(R,2)-r
         if(R(i,j)<=0)
             R(i,j)=0;
             continue
         end
         akt=R(i,j);
         
         for a=-r:r
             for b=-r:r
                 if(R(i+a,j+b)>akt)
                     akt=0;
                     break;
                 end
             end
         end
         R(i,j)=akt;
     end
 end
R=R(r+1:size(R,1)-r,r+1:size(R,2)-r); 
 
 
 
 
%  imshow(I);
%  figure(2);
%  imshow(uint8(Ix));
%  figure(3);
%  imshow(uint8(Iy));
%  figure(4);
%  imshow(uint8(Ixy));
%  figure(5);
 imshow(R);
 