Image Processing Toolbox    

Subtracting Images

To subtract one image from another, or subtract a constant value from an image, use the imsubtract function. imsubtract subtracts each pixel value in one of the input images from the corresponding pixel in the other input image and returns the result in the corresponding pixel in an output image.

Image subtraction can be used as a preliminary step in more complex image processing or by itself. For example, you can use image subtraction to detect changes in a series of images of the same scene. This code fragment subtracts the background from an image of rice grains. The images must be the same size and class.

To subtract a constant from each pixel in I, replace Y with a constant, as in the following example.

Handling Negative Values

Subtraction can result in a negative values for certain pixels. When this occurs with unsigned data types, such as uint8 or uint16, the imsubtract function truncates the negative value to zero (0), which displays as black. To avoid negative values, but preserve the value differentiation of these pixels, use the imabsdiff function. The imabsdiff function calculates the absolute difference between each corresponding pixel in the two images so the result is always nonnegative.


  Adding Images Multiplying Images