Main Content

imadd

Add two images or add constant to image

Description

example

Z= imadd(X,Y)adds each element in arrayXwith the corresponding element in arrayYand returns the sum in the corresponding element of the output arrayZ.

Examples

collapse all

This example shows how to add twouint8arrays with truncation for values that exceed 255.

X = uint8([ 255 0 75; 44 225 100]); Y = uint8([ 50 50 50; 50 50 50 ]); Z = imadd(X,Y)
Z =2x3 uint8 matrix255 50 125 94 255 150

Read two grayscaleuint8images into the workspace.

I = imread('rice.png'); J = imread('cameraman.tif');

Add the images. Specify the output as typeuint16to avoid truncating the result.

K = imadd(I,J,'uint16');

Display the result.

imshow(K,[])

Figure contains an axes object. The axes object contains an object of type image.

Read an image into the workspace.

I = imread('rice.png');

Add a constant to the image.

J = imadd(I,50);

Display the original image and the result.

imshow(I)

Figure contains an axes object. The axes object contains an object of type image.

figure imshow(J)

Figure contains an axes object. The axes object contains an object of type image.

Input Arguments

collapse all

First array, specified as a numeric array or logical array of any dimension.

Second array to be added toX, specified as a numeric or logical array of the same size and class asX, or a numeric scalar of typedouble.

Output Arguments

collapse all

Sum, returned as a numeric array of the same size asX.Zis the same class asXunlessXis logical, in which caseZis data typedouble. IfXis an integer array, elements of the output that exceed the range of the integer type are truncated, and fractional values are rounded.

Version History

Introduced before R2006a