1

By wikipedia http://en.wikipedia.org/wiki/Hessian_matrix , it is defined to be the square matrix of second order partial derivative of a function.

Can anybody tell me if it's correct?

[i,j]=gradient(im);
filt1=(1./2).*[1,0,-1;0,0,0;1,0,-1];
filt2=(1./2).*[-1,0,-1;0,0,0;1,0,1];
ii=(conv2(filt1,i));
jj=(conv2(filt2,j));

Gx=conv2(ii,im); % Gradient of the image in x-axis
Gy=conv2(jj,im); % Gradient of the image in y-axis


dif_Gx = conv2(f_x,a); % Gradient differentiation of the image in x-axis
dif_Gy = conv2(f_y,a); % Gradient differentiation of the image in y-axis

% Calculate second derivative
Gxx = Gx.^2;
Gyy = Gy.^2;
Gxy = Gx.*Gy;
Amir
  • 10,600
  • 9
  • 48
  • 75
user3269865
  • 49
  • 3
  • 11

2 Answers2

1

I tried @Matt J's method proposed above and it seems that the code has dimension dis-match issue. I modified the 3rd and 4th line as

Hxx(2:m-1,1:end) = diff(im,2,1);
Hyy(1:end,2:n-1) = diff(im,2,2);

And now it's working.

SimaGuanxing
  • 673
  • 2
  • 10
  • 29
0

The Hessian at each pixel will be a 2 x 2 matrix of the form [Hxx, Hxy; Hyx, Hyy]. You could compute these data values in a vectorized way across all pixels by doing:

[m,n]=size(im);

[Hxx,Hyy,Hxy,Hyx]=deal(zeros(m,n));

Hxx(2:m-1,2:n-1) = diff(im,2,1);
Hyy(2:m-1,2:n-1) = diff(im,2,2);

tmp = diff(diff(im,1,1),1,2);    
Hxy(2:m-1,2:n-1) = tmp(2:end,2:end);

tmp = diff(diff(im,1,2),1,1);    
Hyx(2:m-1,2:n-1) = tmp(2:end,2:end);

These computations assume you're happy with one-sided differences. You could also convolve im with a centered differencing kernel, e.g. k = [1 0 -1] to approximate first derivatives, and then a second time to get second derivatives.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Matt J
  • 1,127
  • 7
  • 15