IMAGE PROCESSING IN R
SARAVANAKUMAR SEKAR
14 May 2018
library(EBImage)
#image1=readImage("~//pp//pic1.jpg")
#image2=readImage("~//pp//pic2.jpg")
#print(image1)
#display(image1)
image3=readImage("http://all4desktop.com/data_images/1920%20x%201080/4166784-
ms-dhoni-HD.jpg")
Inference
Here I get image from internet using link of that image
print(image3)
## Image
## colorMode : Color
## storage.mode : double
## dim : 1920 1080 3
## frames.total : 3
## frames.render: 1
##
## imageData(object)[1:5,1:6,1]
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 0.4235294 0.4078431 0.4274510 0.4235294 0.4235294 0.4392157
## [2,] 0.4196078 0.4039216 0.4078431 0.4156863 0.4274510 0.4431373
## [3,] 0.4156863 0.4078431 0.3960784 0.4039216 0.4156863 0.4274510
## [4,] 0.4117647 0.4156863 0.4000000 0.4000000 0.4000000 0.4000000
## [5,] 0.4000000 0.4156863 0.4039216 0.4078431 0.4078431 0.4000000
Inference
In this print statement , it tell about overview of the image
In this image colorMode is denoted the it color or not ?
And it tell dim size ,frame.total,frames.render and image clarity weight
display(image3)
Inference
Using display for view the image
image4=readImage("http://www.olympicsupdate.com/wp-content/uploads/2016/02/MS
-Dhoni-wallpapers.jpg")
print(image4)
## Image
## colorMode : Color
## storage.mode : double
## dim : 1920 1080 3
## frames.total : 3
## frames.render: 1
##
## imageData(object)[1:5,1:6,1]
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 0.2705882 0.2666667 0.2666667 0.2627451 0.2627451 0.2666667
## [2,] 0.2745098 0.2745098 0.2705882 0.2666667 0.2666667 0.2666667
## [3,] 0.2823529 0.2784314 0.2745098 0.2705882 0.2705882 0.2705882
## [4,] 0.2784314 0.2784314 0.2745098 0.2705882 0.2705882 0.2705882
## [5,] 0.2705882 0.2705882 0.2705882 0.2705882 0.2705882 0.2705882
display(image4)
#histogram for image
hist(image3)
Inference
In this chart based we can tell about the intensity range and pixels of the image .
If any changes will occur in manipulating this chart intensity will change based on the
manipulating weight.
hist(image4)
#manipulating brightness
b1=image4+0.2
Inference
In this image I just increase the brightness 0.2 for image 4
print(b1)
## Image
## colorMode : Color
## storage.mode : double
## dim : 1920 1080 3
## frames.total : 3
## frames.render: 1
##
## imageData(object)[1:5,1:6,1]
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 0.4705882 0.4666667 0.4666667 0.4627451 0.4627451 0.4666667
## [2,] 0.4745098 0.4745098 0.4705882 0.4666667 0.4666667 0.4666667
## [3,] 0.4823529 0.4784314 0.4745098 0.4705882 0.4705882 0.4705882
## [4,] 0.4784314 0.4784314 0.4745098 0.4705882 0.4705882 0.4705882
## [5,] 0.4705882 0.4705882 0.4705882 0.4705882 0.4705882 0.4705882
Inference
Using print statement we can see the weight difference before weight is 0.27 but now 0.47i
n the category of brightness
display(b1)
b2=image4-0.4
print(b2)
## Image
## colorMode : Color
## storage.mode : double
## dim : 1920 1080 3
## frames.total : 3
## frames.render: 1
##
## imageData(object)[1:5,1:6,1]
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -0.1294118 -0.1333333 -0.1333333 -0.1372549 -0.1372549 -0.1333333
## [2,] -0.1254902 -0.1254902 -0.1294118 -0.1333333 -0.1333333 -0.1333333
## [3,] -0.1176471 -0.1215686 -0.1254902 -0.1294118 -0.1294118 -0.1294118
## [4,] -0.1215686 -0.1215686 -0.1254902 -0.1294118 -0.1294118 -0.1294118
## [5,] -0.1294118 -0.1294118 -0.1294118 -0.1294118 -0.1294118 -0.1294118
Inference
Here I reduced the brightness 0.4 now we can see the difference of the image in print state
ment.
display(b2)
hist(b2)
#manipulating contrast
c1=image4*0.3
Inference
Here I increase the image contrast0.3
display(c1)
c2=image4*1
Inference
last image contrast is very low so again I increase the contrast from 0.3 to 1 .using * symbol
we can modify the image contrast
display(c2)
#gamma corrections
g1=image4>0.3
display(g1)
g2=image4>0.6
display(g2)
g3=image4<0.5
display(g3)
Inference
Gamma is used for manipulate the darkness of the picture. Here, I just increase the darknes
s from 0.3 to 0.6 using >and < symbols
#color
colorMode(image4)=Grayscale
print(image4)
## Image
## colorMode : Grayscale
## storage.mode : double
## dim : 1920 1080 3
## frames.total : 3
## frames.render: 3
##
## imageData(object)[1:5,1:6,1]
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 0.2705882 0.2666667 0.2666667 0.2627451 0.2627451 0.2666667
## [2,] 0.2745098 0.2745098 0.2705882 0.2666667 0.2666667 0.2666667
## [3,] 0.2823529 0.2784314 0.2745098 0.2705882 0.2705882 0.2705882
## [4,] 0.2784314 0.2784314 0.2745098 0.2705882 0.2705882 0.2705882
## [5,] 0.2705882 0.2705882 0.2705882 0.2705882 0.2705882 0.2705882
#to return to color
colorMode(image4)=Color
Inference
ColorMode is use for manipulate color. Here I changed the colorMode from color to graysca
le and again I changed to color
#cropping
k=image4[60:860,360:960,]
display(k)
Inference
Cropping is again important manipulating operating in image processing . Here,I just crop
in this range of 60:860,360:960.
#new image file
writeImage(k,"MS.jpg")
Inference
In EBimage have write operation for save your all updates pictures in your local machine u
sing writeImage.
#flip,flop,rotate resize
l=flip(image4)
display(l)
m=flop(image4)
display(m)
r=rotate(image4,45)
display(r)
o=resize(image4,360)
display(o)
Inference
Using Flip,flop,rotate and resize commands we can flip and flop the image ,rotate the image
, and resize the image, which size you want?
#low-pass filter
low=makeBrush(21,shape = "disc",step = FALSE)
low=low/sum(low)
image.low=filter2(image4,low)
display(image.low)
low=makeBrush(81,shape = "disc",step = FALSE)
low=low/sum(low)
image.low=filter2(image3,low)
display(image.low)
#high-pass filter
high=matrix(1,nc=3,nr=3)
high[2,2]=-5
image.high=filter2(image4,high)
display(image.high)
Inference
Low-pass filter and high-pass filter is used for just blur the image whatever you want?
# combine
c=combine(image3,image4)
display(c)
## Only the first frame of the image stack is displayed.
## To display all frames use 'all = TRUE'.
Inference
We can merge the image one by one using combine command. Here I combine image3 and i
mage4
#put two image into one
i1=image3+image4
display(i1)
hist(i1)
i2=image3/3+image4
display(i2)
hist(i2)
Inference
Here I put two image into one image
If you want second image is more thickness compare with first image just increase the first
image thickness using / symbol

Image Processing in R

  • 1.
    IMAGE PROCESSING INR SARAVANAKUMAR SEKAR 14 May 2018 library(EBImage) #image1=readImage("~//pp//pic1.jpg") #image2=readImage("~//pp//pic2.jpg") #print(image1) #display(image1) image3=readImage("http://all4desktop.com/data_images/1920%20x%201080/4166784- ms-dhoni-HD.jpg") Inference Here I get image from internet using link of that image print(image3) ## Image ## colorMode : Color ## storage.mode : double ## dim : 1920 1080 3 ## frames.total : 3 ## frames.render: 1 ## ## imageData(object)[1:5,1:6,1] ## [,1] [,2] [,3] [,4] [,5] [,6] ## [1,] 0.4235294 0.4078431 0.4274510 0.4235294 0.4235294 0.4392157 ## [2,] 0.4196078 0.4039216 0.4078431 0.4156863 0.4274510 0.4431373 ## [3,] 0.4156863 0.4078431 0.3960784 0.4039216 0.4156863 0.4274510 ## [4,] 0.4117647 0.4156863 0.4000000 0.4000000 0.4000000 0.4000000 ## [5,] 0.4000000 0.4156863 0.4039216 0.4078431 0.4078431 0.4000000 Inference In this print statement , it tell about overview of the image In this image colorMode is denoted the it color or not ? And it tell dim size ,frame.total,frames.render and image clarity weight
  • 2.
    display(image3) Inference Using display forview the image image4=readImage("http://www.olympicsupdate.com/wp-content/uploads/2016/02/MS -Dhoni-wallpapers.jpg") print(image4) ## Image ## colorMode : Color ## storage.mode : double ## dim : 1920 1080 3 ## frames.total : 3 ## frames.render: 1 ## ## imageData(object)[1:5,1:6,1] ## [,1] [,2] [,3] [,4] [,5] [,6] ## [1,] 0.2705882 0.2666667 0.2666667 0.2627451 0.2627451 0.2666667 ## [2,] 0.2745098 0.2745098 0.2705882 0.2666667 0.2666667 0.2666667 ## [3,] 0.2823529 0.2784314 0.2745098 0.2705882 0.2705882 0.2705882
  • 3.
    ## [4,] 0.27843140.2784314 0.2745098 0.2705882 0.2705882 0.2705882 ## [5,] 0.2705882 0.2705882 0.2705882 0.2705882 0.2705882 0.2705882 display(image4) #histogram for image hist(image3)
  • 4.
    Inference In this chartbased we can tell about the intensity range and pixels of the image . If any changes will occur in manipulating this chart intensity will change based on the manipulating weight. hist(image4)
  • 5.
    #manipulating brightness b1=image4+0.2 Inference In thisimage I just increase the brightness 0.2 for image 4 print(b1) ## Image ## colorMode : Color ## storage.mode : double ## dim : 1920 1080 3 ## frames.total : 3 ## frames.render: 1 ## ## imageData(object)[1:5,1:6,1] ## [,1] [,2] [,3] [,4] [,5] [,6] ## [1,] 0.4705882 0.4666667 0.4666667 0.4627451 0.4627451 0.4666667 ## [2,] 0.4745098 0.4745098 0.4705882 0.4666667 0.4666667 0.4666667 ## [3,] 0.4823529 0.4784314 0.4745098 0.4705882 0.4705882 0.4705882 ## [4,] 0.4784314 0.4784314 0.4745098 0.4705882 0.4705882 0.4705882 ## [5,] 0.4705882 0.4705882 0.4705882 0.4705882 0.4705882 0.4705882 Inference
  • 6.
    Using print statementwe can see the weight difference before weight is 0.27 but now 0.47i n the category of brightness display(b1) b2=image4-0.4 print(b2) ## Image ## colorMode : Color ## storage.mode : double ## dim : 1920 1080 3 ## frames.total : 3 ## frames.render: 1 ## ## imageData(object)[1:5,1:6,1] ## [,1] [,2] [,3] [,4] [,5] [,6] ## [1,] -0.1294118 -0.1333333 -0.1333333 -0.1372549 -0.1372549 -0.1333333 ## [2,] -0.1254902 -0.1254902 -0.1294118 -0.1333333 -0.1333333 -0.1333333 ## [3,] -0.1176471 -0.1215686 -0.1254902 -0.1294118 -0.1294118 -0.1294118 ## [4,] -0.1215686 -0.1215686 -0.1254902 -0.1294118 -0.1294118 -0.1294118 ## [5,] -0.1294118 -0.1294118 -0.1294118 -0.1294118 -0.1294118 -0.1294118
  • 7.
    Inference Here I reducedthe brightness 0.4 now we can see the difference of the image in print state ment. display(b2) hist(b2)
  • 8.
    #manipulating contrast c1=image4*0.3 Inference Here Iincrease the image contrast0.3 display(c1)
  • 9.
    c2=image4*1 Inference last image contrastis very low so again I increase the contrast from 0.3 to 1 .using * symbol we can modify the image contrast display(c2)
  • 10.
  • 11.
  • 12.
    Inference Gamma is usedfor manipulate the darkness of the picture. Here, I just increase the darknes s from 0.3 to 0.6 using >and < symbols #color colorMode(image4)=Grayscale print(image4) ## Image ## colorMode : Grayscale ## storage.mode : double ## dim : 1920 1080 3 ## frames.total : 3 ## frames.render: 3 ## ## imageData(object)[1:5,1:6,1] ## [,1] [,2] [,3] [,4] [,5] [,6] ## [1,] 0.2705882 0.2666667 0.2666667 0.2627451 0.2627451 0.2666667 ## [2,] 0.2745098 0.2745098 0.2705882 0.2666667 0.2666667 0.2666667 ## [3,] 0.2823529 0.2784314 0.2745098 0.2705882 0.2705882 0.2705882 ## [4,] 0.2784314 0.2784314 0.2745098 0.2705882 0.2705882 0.2705882 ## [5,] 0.2705882 0.2705882 0.2705882 0.2705882 0.2705882 0.2705882
  • 13.
    #to return tocolor colorMode(image4)=Color Inference ColorMode is use for manipulate color. Here I changed the colorMode from color to graysca le and again I changed to color #cropping k=image4[60:860,360:960,] display(k) Inference Cropping is again important manipulating operating in image processing . Here,I just crop in this range of 60:860,360:960. #new image file writeImage(k,"MS.jpg") Inference In EBimage have write operation for save your all updates pictures in your local machine u sing writeImage. #flip,flop,rotate resize
  • 14.
  • 15.
  • 16.
    o=resize(image4,360) display(o) Inference Using Flip,flop,rotate andresize commands we can flip and flop the image ,rotate the image , and resize the image, which size you want? #low-pass filter low=makeBrush(21,shape = "disc",step = FALSE) low=low/sum(low) image.low=filter2(image4,low) display(image.low)
  • 17.
    low=makeBrush(81,shape = "disc",step= FALSE) low=low/sum(low) image.low=filter2(image3,low) display(image.low)
  • 18.
  • 19.
    Inference Low-pass filter andhigh-pass filter is used for just blur the image whatever you want? # combine c=combine(image3,image4) display(c) ## Only the first frame of the image stack is displayed. ## To display all frames use 'all = TRUE'.
  • 20.
    Inference We can mergethe image one by one using combine command. Here I combine image3 and i mage4 #put two image into one i1=image3+image4 display(i1)
  • 21.
  • 22.
  • 23.
    Inference Here I puttwo image into one image If you want second image is more thickness compare with first image just increase the first image thickness using / symbol