Image Processing Toolbox | ![]() ![]() |
Adding Images
To add two images or add a constant value to an image, use the imadd
function. imadd
adds the value of each pixel in one of the input images with the corresponding pixel in the other input image and returns the sum in the corresponding pixel of the output image.
Image addition has many uses in image processing. For example, the following code fragment uses addition to superimpose one image on top of another. The images must be the same size and class.
You can also use addition to brighten an image by adding a constant value to each pixel. For example, the following code brightens an RGB image.
RGB = imread('flowers.tif'); RGB2 = imadd(RGB,50); subplot(1,2,1); imshow(RGB); subplot(1,2,2); imshow(RGB2);
Handling Overflow
When you add the pixel values of two images, the result can easily overflow the maximum value supported by the data type, especially for uint8
data. When overflow occurs, imadd
truncates the value to the maximum value supported by the data type. This is an effect known as saturation. For example, imadd
truncates uint8
data at 255. To avoid saturation, convert the image to a larger data type, such as uint16
, before performing the addition.
![]() | Image Arithmetic Truncation Rules | Subtracting Images | ![]() |