Image Arithmetic is performing arithmetic operations on two or more images. Here the operation is done in a pixel-by-pixel fashion which means that the value of the pixels of the output image depends only on the values of the corresponding pixels in the input images. Here the arithmetic operation is denoted as,
I(x, y) = A(x, y) \odot B(x, y)
Where:
I(x,y): The output or result of the operation at the coordinates(x,y) .A(x,y): The first input function, matrix or array at the coordinates(x,y) .B(x,y): The second input function, matrix or array at the coordinates(x,y) .\odot: Represents the element-wise multiplication (Hadamard product). It means each element inA(x,y) is multiplied by the corresponding element inB(x,y) at the same coordinates.
Image Arithmetic also has wide range of applications including computer vision, medical imaging, satellite imaging, and digital photography. In this article, we will implement image arithmetic in R Programming Language.
To perform any arithmetic operation on two or more images, the input images must be identical in size and its number of color channels. The two sample input images used are:
Image1:

Image2:

1. Installing and Loading the Required Package
We will perform image arithmetic in R using imager package. We will install and load it into our R environment.
install.packages("imager")
library(imager)
2. Performing Addition of Images
Operator used for addition of two images is '+', which takes two identical sized images as input operands and produces a third output image which is of same size. Here each pixel of output image is sum of values of corresponding pixels of each of two input images. The output pixel values are denoted by,
Q( i , j ) = P1( i , j ) + P2( i , j )
Or in other cases, addition operation can also be done by adding a constant value
Q( i , j ) = P1( i , j ) + C
We will see an example of an R script for the addition of two images
- load.image(): Takes an image path or URL as an argument to load the image.
- +: Performs addition of corresponding pixels in two images.
- plot(): Used to display the output image based on the resultant pixels.
image1 <- load.image("/content/image1.jpeg")
image2 <- load.image("/content/image2.jpg")
result_add <- image1 + image2
plot(result_add, main="Addition result")
Output:

3. Performing Subtraction of Images
Operator used for subtraction of two images is '-', which takes two identical sized images as input operands and produces a third output image which is of same size. Here each pixel of output image is pixel of one image minus corresponding pixel of other input image. The pixels of output image are denoted by,
Q( i , j ) = P1( i , j ) - P2( i , j )
Or in other cases, subtraction can also be done by removing a constant value
Q( i , j ) = P1( i , j ) - C
We will see an example of an R script for subtraction of two images.
- load.image(): Takes an image path or URL as an argument to load the image.
- -: Performs subtraction of corresponding pixels in two images.
- plot(): Used to display the output image based on the resultant pixels.
image1 <- load.image("/content/image1.jpeg")
image2 <- load.image("/content/image2.jpg")
result_sub <- image1 - image2
plot(result_sub, main="Subtraction result")
Output:

4. Performing Multiplication of Images
Operator used for multiplication of two images is '*' which takes two or more identical sized images and multiplies pixels of one input image with corresponding pixels of other image, giving an output image of same dimensions. Here pixels of output image are denoted by,
Q( i , j ) = P1( i , j ) * P2( i , j )
In other form, multiplication can also be done by multiplying a constant value
Q( i , j ) = P1( i , j ) * C
We will see an example of an R script for multiplication of two images.
- load.image(): Takes an image path or URL as an argument to load the image.
- *: Performs multiplication of corresponding pixels in two images.
- plot(): Used to display the output image based on the resultant pixels.
image1 <- load.image("/content/image1.jpeg")
image2 <- load.image("/content/image2.jpg")
result_mul <- image1 * image2
plot(result_mul, main="Multiplication result")
Output:

5. Performing Division of Images
The operator used for the division of two images is /, which takes two or more identically sized images and divides the pixels of one input image by the corresponding pixels of another image, producing an output image of the same dimensions. Here, the pixels of the output image are denoted by:
Q(i, j) = \frac{P_1(i, j)}{P_2(i, j)}
In another form, division can also be performed by dividing each pixel of a single input image by a constant value
Q(i, j) = \frac{P_1(i, j)}{C}
We will see an example of an R script for the division of two images.
- load.image(): Takes an image path or URL as an argument to load the image.
- /: Performs division of corresponding pixels in two images.
- plot(): Used to display the output image based on the resultant pixels.
image1 <- load.image("/content/image1.jpeg")
image2 <- load.image("/content/image2.jpg")
result_div <- image1 / image2
plot(result_div, main="Division result")
Output:
