# 图像处理中的一阶偏导和二阶偏导

1. 一阶差分：

2. 二阶偏导数的推导和近似：

3. 上式以点（i+1，j）为中心，用i代换i+1可得以（i,j）为中心的二阶偏导数则有：

4. 同理：

5. 进而可推导：

6. 这样我们就可以很好的运用其他的一阶偏导的定义，如SIFT特征OpenCV实现版本中的一阶以及二阶偏导：

1. /*
2. Computes the partial derivatives in x, y, and scale of a pixel in the DoG
3. scale space pyramid.
4. @param dog_pyr DoG scale space pyramid
5. @param octv pixel's octave in dog_pyr
6. @param intvl pixel's interval in octv
7. @param r pixel's image row
8. @param c pixel's image col
9. @return Returns the vector of partial derivatives for pixel I
10.     { dI/dx, dI/dy, dI/ds }^T as a CvMat*
11. */
12. static CvMat* deriv_3D( IplImage*** dog_pyr, int octv, int intvl, int r, int c )
13.     CvMat* dI;
14.     double dx, dy, ds;
15.
16.     dx = ( pixval32f( dog_pyr[octv][intvl], r, c+1 ) -
17.         pixval32f( dog_pyr[octv][intvl], r, c-1 ) ) / 2.0;
18.     dy = ( pixval32f( dog_pyr[octv][intvl], r+1, c ) -
19.         pixval32f( dog_pyr[octv][intvl], r-1, c ) ) / 2.0;
20.     ds = ( pixval32f( dog_pyr[octv][intvl+1], r, c ) -
21.         pixval32f( dog_pyr[octv][intvl-1], r, c ) ) / 2.0;
22.
23.     dI = cvCreateMat( 3, 1, CV_64FC1 );
24.     cvmSet( dI, 0, 0, dx );
25.     cvmSet( dI, 1, 0, dy );
26.     cvmSet( dI, 2, 0, ds );
27.
28.     return dI;
29.
30.
31.
32. /*
33. Computes the 3D Hessian matrix for a pixel in the DoG scale space pyramid.
34. @param dog_pyr DoG scale space pyramid
35. @param octv pixel's octave in dog_pyr
36. @param intvl pixel's interval in octv
37. @param r pixel's image row
38. @param c pixel's image col
39. @return Returns the Hessian matrix (below) for pixel I as a CvMat*
40.     / Ixx  Ixy  Ixs / <BR>
41.     | Ixy  Iyy  Iys | <BR>
42.     / Ixs  Iys  Iss /
43. */
44. static CvMat* hessian_3D( IplImage*** dog_pyr, int octv, int intvl, int r, int c )
45.     CvMat* H;
46.     double v, dxx, dyy, dss, dxy, dxs, dys;
47.
48.     v = pixval32f( dog_pyr[octv][intvl], r, c );
49.     dxx = ( pixval32f( dog_pyr[octv][intvl], r, c+1 ) +
50.             pixval32f( dog_pyr[octv][intvl], r, c-1 ) - 2 * v );
51.     dyy = ( pixval32f( dog_pyr[octv][intvl], r+1, c ) +
52.             pixval32f( dog_pyr[octv][intvl], r-1, c ) - 2 * v );
53.     dss = ( pixval32f( dog_pyr[octv][intvl+1], r, c ) +
54.             pixval32f( dog_pyr[octv][intvl-1], r, c ) - 2 * v );
55.     dxy = ( pixval32f( dog_pyr[octv][intvl], r+1, c+1 ) -
56.             pixval32f( dog_pyr[octv][intvl], r+1, c-1 ) -
57.             pixval32f( dog_pyr[octv][intvl], r-1, c+1 ) +
58.             pixval32f( dog_pyr[octv][intvl], r-1, c-1 ) ) / 4.0;
59.     dxs = ( pixval32f( dog_pyr[octv][intvl+1], r, c+1 ) -
60.             pixval32f( dog_pyr[octv][intvl+1], r, c-1 ) -
61.             pixval32f( dog_pyr[octv][intvl-1], r, c+1 ) +
62.             pixval32f( dog_pyr[octv][intvl-1], r, c-1 ) ) / 4.0;
63.     dys = ( pixval32f( dog_pyr[octv][intvl+1], r+1, c ) -
64.             pixval32f( dog_pyr[octv][intvl+1], r-1, c ) -
65.             pixval32f( dog_pyr[octv][intvl-1], r+1, c ) +
66.             pixval32f( dog_pyr[octv][intvl-1], r-1, c ) ) / 4.0;
67.
68.     H = cvCreateMat( 3, 3, CV_64FC1 );
69.     cvmSet( H, 0, 0, dxx );
70.     cvmSet( H, 0, 1, dxy );
71.     cvmSet( H, 0, 2, dxs );
72.     cvmSet( H, 1, 0, dxy );
73.     cvmSet( H, 1, 1, dyy );
74.     cvmSet( H, 1, 2, dys );
75.     cvmSet( H, 2, 0, dxs );
76.     cvmSet( H, 2, 1, dys );
77.     cvmSet( H, 2, 2, dss );
78.
79.     return H;