Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use R for basic image processing

I am currently working on an application of Principal Component Analysis to visual data in R.

In Matlab, one can invoke commands such as "im2double" and "mat2gray" to convert a bitmap into a numerical matrix and back again to an image.

I was wondering whether this can be achieved in R, maybe via additional packages.

like image 689
Experimental Psychologist Avatar asked Jan 03 '13 21:01

Experimental Psychologist


3 Answers

I've used the EBImage package (vignette here) available on bioconductor to work with and manipulate images:

# installing package if needed
source("http://bioconductor.org/biocLite.R")
biocLite("EBImage")

library(EBImage) 
f = readImage(system.file("images", "lena-color.png", package="EBImage"))
str(f)
#Formal class 'Image' [package "EBImage"] with 2 slots
#  ..@ .Data    : num [1:512, 1:512, 1:3] 0.886 0.886 0.875 0.875 0.886 ...
#  ..@ colormode: int 2
like image 121
MattBagg Avatar answered Nov 15 '22 19:11

MattBagg


I was curious enough to try this out; clearly a package is a better solution, but if you really want to stick to base R, this will load a png (albeit upside down and backwards; that's probably fixable). It assumes the presence of the netpbm tools, so probably won't work out of the box on Windows systems.

readPng <- function(pngFile) {
  contents <- system(paste('pngtopnm',pngFile,'| pnmtoplainpnm'),intern=TRUE)
  imgDims <- strsplit(contents[2], ' ')
  width <- as.numeric(imgDims[[1]][1])
  height <- as.numeric(imgDims[[1]][2])
  rawimg <- scan(textConnection(contents),skip=3)
  return(list(
    x=1:width,
    y=1:height,
    z=matrix(rawimg,width),
    width=width,
    height=height))
}

You can run image(img) on the list returned from this function directly, or access the per-pixel values using img$z.

like image 45
user295691 Avatar answered Nov 15 '22 21:11

user295691


Two methods to install the package.

  1. install through command line if you have no Editor like RStudio
  2. install the command line by entering into R interpreter using R command in bash.

Go to prompt where you can execute R commands. here these the basic image processing command.

execute this command to install the Bio conductor backage biocLite, which will help to install the EBIMage package( This package is used widely for image processing)

source("http://bioconductor.org/biocLite.R")

install the EMImage package to use image processing commands.

biocLite("EBImage")

Load the EBIMage package to use the image processing

library("EBImage")
# Reading image from computer
img=readImage(files="~/Desktop/Prog/R/tinago.JPG")
display(img)
img1=img+ 0.2 # increase brightness
img2=img- 0.2 # decrease brightness
display(img1) # Display images in browser or graphical window
display(img2) # Display images in browser or graphical window
img3= img * 0.5 # decrease contrast 
img4=img * 2    # increase contrast
display(img3); display(img4)  # show result images
img5=img^2 # increase Gamma correction
img6=img^0.7 # decrease Gamma correction
display(img5); display(img6)  # Display result images 

Note : readImage to read the image. Display is used to view the image in Graphical Window.

like image 22
Hafiz Shehbaz Ali Avatar answered Nov 15 '22 20:11

Hafiz Shehbaz Ali