# Image Arithmetic Operations in matlab

Arithmetic operators are applied in a pixel-by-pixel way, i.e. the value of a pixel in the output image depends only on the values of the corresponding pixels in the input images. so the the images must be of the same size. Image arithmetic is the most simple form of image processing, hence it has wide range of applications.

## Image Addition

To add two images or add a constant value to an image, use the `imadd`

function. imadd is matlab inbulit function, It 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.

% img add by constant factor I = rgb2gray(imread('lena.bmp')); J = imadd(I,50); K = imadd(I,90); L = imadd(I,130); subplot(2,2,1), imshow(I), title('lena.bmp'); subplot(2,2,2), imshow(J),title('Constant Factor: 50'); subplot(2,2,3), imshow(K),title('Constant Factor: 90'); subplot(2,2,4), imshow(L),title('Constant Factor: 130');

##### Output

`imadd`

method can be used to brighten an image by adding a constant value to each pixel.

% Addition of Images close all; I1=imread('back.bmp'); I2=imresize(I1,[500 500]); I3 = I2;%background I1=imread('car.bmp'); I2=imresize(I1,[500 500]); I4 = I2;%car J = imadd(I3,I4); subplot(1,3,1), imshow(I3) subplot(1,3,2), imshow(I4) subplot(1,3,3), imshow(J) title('Image Addition'); imwrite(J,'output_add_image.png','png');

download code:

## Image Subtraction

`imsubtract`

is inbuilt matlab function 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 for more complex image processing.

You can use image subtraction to detect changes in a series of images of the same scene. For this operation the images must be the same size and class.

% image substraction close all; I1=imread('church.bmp'); I2=imresize(I1,[500 500]); I3 = I2;%rgb2gray(I2); I1=imread('mask.bmp'); I2=imresize(I1,[500 500]); I4 = I2; J = imsubtract(I3,I4); subplot(1,3,1), imshow(I3) subplot(1,3,2), imshow(I4) subplot(1,3,3), imshow(J) title('image substraction'); imwrite(J,'output_sub_image.png','png');

##### OUTPUT

## Image Multiplication

`immultiply`

is inbuilt matlab function to multiply one image with another, or multiply a constant value from an image, use the `immultiply`

function. `immultiply`

multiplies 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. if elements of output_image exceeding the range of the integer type are truncated, and fractional values are rounded. Image multiplication can be used for more complex image processing.