Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The image gradient is the rate of change over any given pixel of an image, either in the horizontal or vertical direction. An image can be thought of as a large matrix of values [0, 255]. A common horizontal matrix for taking an image gradient is [1, 0, -1], or the value to the left of our pixel minus the value to the right of our pixel.

I understand this conceptually, but in practice I'm not sure how to apply it to the edges of an image (eg the value to the left or the right of the pixel doesn't exist).

share|cite|improve this question
up vote 0 down vote accepted

Check out the documentation for the matlab function 'imfilter'. The first box on that page has the title 'Boundary Options'. It list four options, for example: 'replicate' Input array values outside the bounds of the array are assumed to equal the nearest array border value.

This is a standard problem when filtering an image (and you are approximating the gradient with a filter), and there are many ways to deal with it that are more or less equally good.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.