Ben
Ben

Reputation: 3922

For histogram of oriented gradients, how to compute the gradient vector of pixels on the edges?

To compute histogram of oriented gradients for an image, we need to compute gradient vectors for every pixel. But pixels on the edges lack some neighbers for the gradient vectors. How to deal with that?

For example, a pixel on the left edge of an image doesn't have a neighber on its left. Then how to compute its gradient in the x direction?

Upvotes: 0

Views: 256

Answers (1)

Rotem
Rotem

Reputation: 32094

Refer to MATLAB documentation of Numerical gradient: http://www.mathworks.com/help/matlab/ref/gradient.html

gradient calculates the central difference for interior data points. For example, consider a matrix with unit-spaced data, A, that has horizontal gradient G = gradient(A). The interior gradient values, G(:,j), are:

G(:,j) = 0.5*(A(:,j+1) - A(:,j-1)); where j varies between 2 and N-1, where N is size(A,2).

The gradient values along the edges of the matrix are calculated with single-sided differences, so that

G(:,1) = A(:,2) - A(:,1); G(:,N) = A(:,N) - A(:,N-1); If the point spacing is specified, then the differences are scaled appropriately. If two or more outputs are specified, gradient also calculates differences along other dimensions in a similar manner. Unlike the diff function, gradient returns an array with the same number of elements as the input.

A = magic(5)

Result:

A =

   17   24    1    8   15
   23    5    7   14   16
    4    6   13   20   22
   10   12   19   21    3
   11   18   25    2    9


[Gx, Gy] = gradient(A)

Result:

Gx =

    7.0000   -8.0000   -8.0000    7.0000    7.0000
  -18.0000   -8.0000    4.5000    4.5000    2.0000
    2.0000    4.5000    7.0000    4.5000    2.0000
    2.0000    4.5000    4.5000   -8.0000  -18.0000
    7.0000    7.0000   -8.0000   -8.0000    7.0000

Gy =

    6.0000  -19.0000    6.0000    6.0000    1.0000
   -6.5000   -9.0000    6.0000    6.0000    3.5000
   -6.5000    3.5000    6.0000    3.5000   -6.5000
    3.5000    6.0000    6.0000   -9.0000   -6.5000
    1.0000    6.0000    6.0000  -19.0000    6.0000

Most left column of Gx:

A(:, 2) - A(:, 1)

Result:

    7
  -18
    2
    2
    7

As you can see, Gx(:, 1) equals A(:, 2) - A(:, 1).

Same result for Gy:

A(2, :) - A(1, :)

Result:

6  -19    6    6    1

Padding edges by replicate:
For filter gradient filter (size [3, 1] and [1, 3]) padding is just replicate row and column in each side:

   17   17   24    1    8   15   15
   17   17   24    1    8   15   15
   23   23    5    7   14   16   16
    4    4    6   13   20   22   22
   10   10   12   19   21    3    3
   11   11   18   25    2    9    9
   11   11   18   25    2    9    9

Example of formula inside the image boundaries:
Gx(:, 2) = 0.5*(A(:, 3) - A(:, 1))

Example:

0.5*(A(:, 3) - A(:, 1))

Result:

  -8.0000
  -8.0000
   4.5000
   4.5000
   7.0000

Upvotes: 1

Related Questions