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:
Image 1Image2:
Image 2Image 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:
Addition of two imagesIn 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:
Subtraction of two imagesIn 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:
Multiplication of two imagesIn 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:
image multiplicationAlso 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:
Division of two imagesIn 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.
Similar Reads
Create Heatmap in R
Heatmap is a graphical way to represent data. It is most commonly used in data analysis. In data analysis, we explore the dataset and draws insight from the dataset, we try to find the hidden patterns in the data by doing a visual analysis of the data. Heatmap visualizes the value of the matrix with
4 min read
Curve Fitting in R
In this article, we will discuss how to fit a curve to a dataframe in the R Programming language. Curve fitting is one of the basic functions of statistical analysis. It helps us in determining the trends and data and helps us in the prediction of unknown data based on a regression model/function.
4 min read
How to add image to ggplot2 in R ?
In this article, we will discuss how to insert or add an image into a plot using ggplot2 in R Programming Language. The ggplot() method of this package is used to initialize a ggplot object. It can be used to declare the input data frame for a graphic and can also be used to specify the set of plot
4 min read
Iris dataset in R
The Iris dataset is a classic dataset often used for learning and practicing data analysis and machine learning techniques. The Iris dataset in the R Programming Language is often used for loading the data to build predictive models. Iris dataset in RThe Iris dataset comprises measurements of iris f
7 min read
Automatically Maximize a Image in MATLAB
MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. With the help of this software, we can maximize automatically an image to have better details of the picture. It is done by running a
1 min read
Grid and Lattice Packages in R Programming
Every programming language has packages to implement different functions. Many functions are bundled together in a package. To use those functions, installation and loading of these packages are required. In R programming, there are 10, 000 packages in the CRAN repository. Grid and Lattice are some
3 min read
Graphical Data Analysis in R
Graphical Data Analysis (GDA) is a powerful tool that helps us to visualize and explore complex data sets. R is a popular programming language for GDA as it has a wide range of built-in functions for producing high-quality visualizations. In this article, we will explore some of the most commonly us
7 min read
Implement Machine Learning With Caret In R
In today's society, technological answers to human issues are knocking on the doors of practically all fields of knowledge. Every aspect of this universe's daily operations generates data, and technology solutions base their decisions on these data-driven intuitions. In order to create a machine tha
8 min read
ggpattern Package in R
The ggpattern package in R is a package for creating custom and visually appealing backgrounds for ggplot2 plots. The overall article is going to cover a comprehensive introduction to the ggpattern package in R, including its features and capabilities, installation and setup, and how to use it to cr
12 min read
Importing Data in R Script
We can read external datasets and operate with them in our R environment by importing data into an R script. R offers a number of functions for importing data from various file formats. In this article, we are going to see how to Import data in R Programming Language. Importing Data in R First, let'
3 min read