【问题标题】:How to measure gradient and Hessian matrix in matlab over gradient?如何在梯度上测量matlab中的梯度和Hessian矩阵?
【发布时间】:2014-02-05 13:15:31
【问题描述】:

维基百科http://en.wikipedia.org/wiki/Hessian_matrix定义为函数的二阶偏导方阵。

谁能告诉我它是否正确?

[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;

【问题讨论】:

    标签: matlab matrix hessian-matrix


    【解决方案1】:

    我尝试了上面提出的@Matt J 的方法,似乎代码存在尺寸不匹配问题。我将第 3 行和第 4 行修改为

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

    现在它开始工作了。

    【讨论】:

      【解决方案2】:

      每个像素的 Hessian 矩阵将是 [Hxx, Hxy; Hyx, Hyy] 形式的 2 x 2 矩阵。您可以通过以下方式在所有像素上以矢量化方式计算这些数据值:

      [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);
      

      这些计算假设您对片面的差异感到满意。您还可以将 im 与居中差分内核卷积,例如k = [1 0 -1] 近似一阶导数,然后第二次求二阶导数。

      【讨论】:

        猜你喜欢
        • 2016-07-21
        • 2018-05-16
        • 2015-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-12
        相关资源
        最近更新 更多