Main Content

# imlincomb

Linear combination of images

## Syntax

``Z = imlincomb(K1,A1,K2,A2,...,Kn,An)``
``Z = imlincomb(K1,A1,K2,A2,...,Kn,An,K)``
``Z = imlincomb(___,outputClass)``

## Description

example

````Z = imlincomb(K1,A1,K2,A2,...,Kn,An)` computes the linear combination of images, `A1`, `A2`, … , `An`, with weights `K1`, `K2`, … , `Kn` according to: `Z = K1*A1 + K2*A2 + ... + Kn*An````

example

````Z = imlincomb(K1,A1,K2,A2,...,Kn,An,K)` adds an offset, `K`, to the linear combination: `Z = K1*A1 + K2*A2 + ... + Kn*An + K````

example

````Z = imlincomb(___,outputClass)` specifies the output class of `Z`.```

## Examples

collapse all

Read an image into the workspace.

`I = imread('cameraman.tif');`

Scale the image using a coefficient of 1.5 in the linear combination.

`J = imlincomb(1.5,I);`

Display the original image and the processed image.

`imshow(I)` ```figure imshow(J)``` Read an image into the workspace.

`I = imread('cameraman.tif');`

Create a low-pass filtered copy of the image.

`J = uint8(filter2(fspecial('gaussian'), I));`

Find the difference image and shift the zero value to 128 using a linear combination of `I` and `J`.

`K = imlincomb(1,I,-1,J,128); %K(r,c) = I(r,c) - J(r,c) + 128`

Display the resulting difference image.

`imshow(K)` Read two grayscale `uint8` images into the workspace.

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

Add the images using a linear combination. Specify the output as type `uint16` to avoid truncating the result.

`K = imlincomb(1,I,1,J,'uint16');`

Display the result.

`imshow(K,[])` This example shows the difference between nesting calls and using linear combinations when performing a series of arithmetic operations on images. To illustrate how `imlincomb` performs all the arithmetic operations before truncating the result, compare the results of calculating the average of two arrays, `X` and `Y`, using nested arithmetic functions and using `imlincomb`.

Create two arrays.

```X = uint8([ 255 0 75; 44 225 100]); Y = uint8([ 50 50 50; 50 50 50 ]);```

Average the arrays using nested arithmetic functions. To calculate the average returned in `Z(1,1)`, the function `imadd` adds 255 and 50 and truncates the result to 255 before passing it to `imdivide`. The average returned in `Z(1,1)` is 128.

`Z = imdivide(imadd(X,Y),2)`
```Z = 2x3 uint8 matrix 128 25 63 47 128 75 ```

In contrast, `imlincomb` performs the addition and division in double precision and only truncates the final result. The average returned in `Z2(1,1)` is 153.

`Z2 = imlincomb(.5,X,.5,Y)`
```Z2 = 2x3 uint8 matrix 153 25 63 47 138 75 ```

## Input Arguments

collapse all

Image coefficients, specified as numeric scalars.

Data Types: `double`

Input images, specified as numeric arrays of the same size and class.

Offset, specified as a numeric scalar.

Data Types: `double`

Output class of `Z`, specified as a string scalar or character vector containing the name of a numeric class.

Example: `'uint16'`

Example: `"double"`

## Output Arguments

collapse all

Linearly combined image, returned as a numeric array of the same size as `A1`. If `A1` is `logical`, then `Z` is `double`, otherwise `Z` has the same class as `A1`.

## Tips

• When performing a series of arithmetic operations on a pair of images, you can achieve more accurate results if you use `imlincomb` to combine the operations, rather than nesting calls to the individual arithmetic functions, such as `imadd`. When you nest calls to the arithmetic functions, and the input arrays are of an integer class, each function truncates and rounds the result before passing it to the next function, thus losing accuracy in the final result. `imlincomb` computes each element of the output `Z` individually, in double-precision floating point. If `Z` is an integer array, `imlincomb` clips elements of `Z` that exceed the range of the integer type and rounds off fractional values.

## See Also

Introduced before R2006a

Download ebook