Open In App

Image Arithmetic in R

Last Updated : 30 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Image arithmetic in R Programming Language involves manipulating images by performing mathematical operations on their pixel values. This can be useful for tasks like blending images, adjusting contrast, and creating special effects. In this article, we will see how image arithmetic is implemented in R.

What is Image Arithmetic?

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) o B(x,y)

where ‘o’ is an arithmetic operation, ‘I’ is the resultant image, ‘A’ and ‘B’ are input images of identical size. Thus Image Arithmetic plays an important role in the context of ‘Image processing’. And this also has wide range of applications including computer vision, medical imaging, satellite imaging, and digital photography.

In this article we will perform arithmetic operations on two sample images using R programming.

Image Arithmetic using R

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:

image1-(1)

Image 1

Image2:

image2-(1)

Image 2

Image arithmetic in R can be done using a package named “imager”. This can be installed and loaded into R environment using the below command.

install.packages("imager")
library(imager)

Addition of two 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 C to each pixel of single input image.

Q( i , j ) = P1( i , j ) + C

Let us see an example of R script for addition of two images.

R
#install and load packages
install.packages("imager")
library(imager)

#load two input images
image1 <- load.image("C:/Users/kisha/Downloads/image1 (1).jpeg")
image2 <- load.image("C:/Users/kisha/Downloads/image2 (1).jpg")

#add two images
result_add <- image1 + image2

#display results
plot(result_add, main="Addition result")

Output:

add_img

Addition of two images

In the above example, ‘load.image()’ method takes image path or an url as an argument. After performing desired operation ‘plot()’ method is used to plot the output image according to the resultant pixels.

Subtract one image from other image

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 C from each pixel of single input image.

Q( i , j ) = P1( i , j ) - C

Let us see an example of R script for subtraction of two images.

R
#install and load packages
install.packages("imager")
library(imager)

#load two input images
image1 <- load.image("C:/Users/kisha/Downloads/image1 (1).jpeg")
image2 <- load.image("C:/Users/kisha/Downloads/image2 (1).jpg")

#subtract two images
result_sub <- image1 - image2

#display results
plot(result_sub, main="Subtraction result")

Output:

sub_img

Subtraction of two images

In the above example, ‘load.image()’ method takes image path or an url as an argument. After performing desired operation ‘plot()’ method is used to plot the output image according to the resultant pixels.

Multiplication of two images

Like other arithmetic operations, multiplication of two images comes with operand * and takes two or more identical sized images, which multiplies pixels of one input image with corresponding pixels of other image, and gives 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 C for each pixel of single input image.

Q( i , j ) = P1( i , j ) * C

Multiplication of images is also called as ‘graylevel scaling’ in the context of image processing. Let us see an example of R script for multiplication of two images.

R
#install and load packages
install.packages("imager")
library(imager)

#load two input images
image1 <- load.image("C:/Users/kisha/Downloads/image1 (1).jpeg")
image2 <- load.image("C:/Users/kisha/Downloads/image2 (1).jpg")

#multiply two images
result_mul <- image1 * image2

#display results
plot(result_mul, main="Multiplication result")

Output:

mul_img

Multiplication of two images

In the above example, ‘load.image()’ method takes image path or an url as an argument. After performing desired operation ‘plot()’ method is used to plot the output image according to the resultant pixels.

R
#install and load packages
install.packages("imager")
library(imager)

#load two input images
image1 <- load.image("C:/Users/kisha/Downloads/background-3177833_1280.jpg")
image2 <- load.image("C:/Users/kisha/Downloads/chick-4919202_1280 (1).jpg")

#multiply two images
result_mul <- image1 * image2

#plot images
par(mfrow=c(2,2))
plot(image1 , main="image1")
plot(image2 , main="image2")
plot(result_mul , main="multiplication")

Output:

mul1

image multiplication

Also Division of two or images can also be performed, below is the R script for division of images.

Division of two images

Here each pixel of input images are divided to produce an output image of identical size.

R
#install and load packages
install.packages("imager")
library(imager)

#load two input images
image1 <- load.image("C:/Users/kisha/Downloads/image1 (1).jpeg")
image2 <- load.image("C:/Users/kisha/Downloads/image2 (1).jpg")

#divide two images
result_div <- image1 * image2

#display results
plot(result_div, main="Division result")

Output:

div_img

Division of two images

In the above example, ‘load.image()’ method takes image path or an url as an argument. After performing desired operation ‘plot()’ method is used to plot the output image according to the resultant pixels.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads