#Reading Image: The readImage Function is used to read image from file location or a URL
library(EBImage)
## Warning: package 'EBImage' was built under R version 3.4.3
Image <- readImage("C:/Users/Admin/Documents/RS.jpg")
#Displaying Image:Image can be displayed using display function and pixel intensities should range from 0 (black) to 1
display(Image)
writeImage(Image, 'F:/Image.jpg' ,quality=85)
print(Image)
## Image
## colorMode : Color
## storage.mode : double
## dim : 300 168 3
## frames.total : 3
## frames.render: 1
##
## imageData(object)[1:5,1:6,1]
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.003921569 0.003921569 0.003921569 0.003921569 0.003921569
## [2,] 0.000000000 0.003921569 0.003921569 0.003921569 0.003921569
## [3,] 0.000000000 0.000000000 0.000000000 0.003921569 0.003921569
## [4,] 0.003921569 0.003921569 0.003921569 0.003921569 0.007843137
## [5,] 0.003921569 0.003921569 0.003921569 0.003921569 0.003921569
## [,6]
## [1,] 0.003921569
## [2,] 0.003921569
## [3,] 0.003921569
## [4,] 0.007843137
## [5,] 0.007843137
Image matrices obtained values from mapping pixels in the image to the real line between 0 and 1. Both extremes of this interval [0, 1], are black and white colours, respectively. Hence, pixels with values closer to any of these end points are expected to be darker or lighter, respectively. And because pixels are contained in a large array, then we can do all matrix manipulations available in R for processing. As matrices, images can be manipulated with all R mathematical operators. For image sitting control can be achieved using mathematical operators.
Image1 <- Image + 0.2
Image2 <- Image - 0.2
display (Image1); display(Image2)
Image1 <- Image * 0.5
Image2 <- Image * 2
display(Image1); display(Image2)
Image1 <- Image ^ 4
Image2 <- Image ^ 0.9
display(Image1); display(Image2)
Spatial image transformations can be performed with the functions such as re-size, rotate, translate and the functions flip and flop to reflect images.
#Resizing the image
Image1 <- resize(Image, 500)
display(Image1)
# FLip and Flop of image
Image2 <- flip(Image)
display(Image2)
Image3 = flop(Image)
display(Image3)
#Image Rotation
Image4 <- translate(rotate(Image, 90), c(50, 0))
display(Image4)
The function colorMode can access and change the value of the slot colormode, altering the rendering mode of an image. In this, the Color image (Image) with one frame is changed into a Grayscale, corresponding to the red, green and blue channels.
print(Image)
## Image
## colorMode : Color
## storage.mode : double
## dim : 300 168 3
## frames.total : 3
## frames.render: 1
##
## imageData(object)[1:5,1:6,1]
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.003921569 0.003921569 0.003921569 0.003921569 0.003921569
## [2,] 0.000000000 0.003921569 0.003921569 0.003921569 0.003921569
## [3,] 0.000000000 0.000000000 0.000000000 0.003921569 0.003921569
## [4,] 0.003921569 0.003921569 0.003921569 0.003921569 0.007843137
## [5,] 0.003921569 0.003921569 0.003921569 0.003921569 0.003921569
## [,6]
## [1,] 0.003921569
## [2,] 0.003921569
## [3,] 0.003921569
## [4,] 0.007843137
## [5,] 0.007843137
colorMode(Image) <- Grayscale
display(Image)
## Only the first frame of the image stack is displayed.
## To display all frames use 'all = TRUE'.
Images can be linearly filtered using filter2. Low-pass filtering is used to perform blur image, remove noise, etc. High-pass filtering is used to perform detect edges, sharpen images etc., various filter shapes can be generated using makeBrush.
#LOW-PASS Filtering image
flo = makeBrush(21, shape='disc', step=FALSE)^2
flo = flo/sum(flo)
imgflo = filter2(Image, flo)
display(imgflo)
## Only the first frame of the image stack is displayed.
## To display all frames use 'all = TRUE'.
#HIGH-PASS Filtering image
fhi = matrix(1, nc=3, nr=3)
fhi[2,2] = -8
imgfhi = filter2(Image, fhi)
display(imgfhi)
## Only the first frame of the image stack is displayed.
## To display all frames use 'all = TRUE'.