3

I just started using RStudio, as the available package to analyze my images is in R. I just want to analyze some 50 images which are stored in a folder. How can I read each image (by forming a loop), perform some operation on each image and save the output (my output is a list) as a vector?

UPDATE:

I just wrote a piece of code which is as following:

folder <- "F:/F_diff/1_d/glass/New folder/"      # path to folder that holds multiple .jpg files
file_list <- list.files(path=folder, pattern="*.jpg") # create list of all .jpg files in folder

for (i in 1:length(file_list)){
  assign(file_list[i],   
     #read image
     im2 <- readImage(paste(folder, file_list[i], sep=''))

     #analyze each image
     B <- matrix(im2,nrow=808,ncol=610,byrow=FALSE, dimnames=NULL)
     Haarimtest <- TOS2D(B, smooth = FALSE, nsamples = 100)
     summary(Haarimtest)

 )}

I am getting the following errors:

Error: unexpected symbol in: " #analyze each image B" Haarimtest <- TOS2D(B, smooth = FALSE, nsamples = 100) Error in base::log2(x) : non-numeric argument to mathematical function summary(Haarimtest) Error in summary(Haarimtest) : object 'Haarimtest' not found

)} Error: unexpected ')' in " )"

UPDATE 2

After some tinkering with the code and lot of searching, I was able to run it. The code first imports all 30 .tif images of size 64x64 pixels from a folder and perform some image analysis on each image The updated code is as follows:

> library(tiff) 
  library(LS2W) 
  library(LS2Wstat)
> # path to folder that holds multiple .tif files 
  path <- "C:/Users/Metaheuristics/Documents/MATLAB/diff_64 x64/2D/" 
> # create list of all .tif files in folder 
  files <- list.files(path=path, pattern="*.tif") 
> 
> #import all files  
  for(file in files) {   
  perpos <- which(strsplit(file, "")[[1]]==".")   
  assign(
> gsub(" ","",substr(file, 1, perpos-1)), 
> B<-readTIFF(paste(path,file,sep="")))
> 
  #perform image analysis on individual images   
  Haarimtest <- TOS2D(B, smooth = FALSE, nsamples = 100)            
  summary(Haarimtest)   
  }

Just one problem, I am not being able to save the result.

Uwe
  • 41,420
  • 11
  • 90
  • 134
Siddharth Rawat
  • 111
  • 1
  • 2
  • 7
  • If you know how to read one image file then you can do similar to http://stackoverflow.com/questions/11433432/importing-multiple-csv-files-into-r – jogo Apr 07 '17 at 11:26
  • 1
    see these two posts [looping through all files in directory in r](http://stackoverflow.com/questions/14958516/looping-through-all-files-in-directory-in-r-applying-multiple-commands) and [how to read jpeg in r](http://stackoverflow.com/questions/15283447/how-to-read-jpeg-in-r-2-15) – Imran Ali Apr 07 '17 at 11:29
  • Hi and welcome to SO. Please take a look at how questions should be asked : http://stackoverflow.com/help/how-to-ask It's an interesting question, but without at least an idea about what code you tried and which package you use for analyzing the image, it's impossible to answer satisfactory. – Joris Meys Apr 07 '17 at 11:36
  • I am using the LS2Wstat package (https://www.rdocumentation.org/packages/LS2Wstat/versions/2.0-3) which is a multiscale test for spatial stationarity for textured images. these textured images are laser speckle fields obtained from optical experiments. – Siddharth Rawat Apr 07 '17 at 12:01
  • The documentation says that `TOS2D()` function takes an image as one of the arguments whereas you are providing a matrix `B`, I would suggest that you pass `im2` as argument instead of `B` – Imran Ali Apr 07 '17 at 12:22
  • HI, For a single image of size 808x610, the following code works just fine, (here im2 is a numeric image array, and I convert it to a matrix B) im2 <- load.image("F:/F_diff/1_d/glass/1.jpg") B <- matrix(im2,nrow=808,ncol=610,byrow=FALSE, dimnames=NULL) Haarimtest <- TOS2D(B, smooth = FALSE, nsamples = 100) summary(Haarimtest) – Siddharth Rawat Apr 07 '17 at 12:32
  • replace the line ` assign(file_list[i], im2 <- readImage(paste(folder, file_list[i], sep=''))` with ` im2 <- readImage(paste(folder, file_list[i], sep=''))`, I don't think you need `assign` here – Imran Ali Apr 07 '17 at 13:17
  • Run `lapply` over a vector of the file names you construct instead of using a `for` loop, and remove the last line of the loop (`summary(Haarimtest)`), and you'll get a list comprising the results of the calls to `TOS2D`. Then you can inspect or further process or whatever those results one by one or with additional calls to `lapply`. – ulfelder Apr 10 '17 at 10:34

1 Answers1

1

Although it seems like its been like 2 years since this question. I had a similar problem and found the solution to it.

before your for loop, add: df <- NULL and after you line in:

Haarimtest <- TOS2D(B, smooth = FALSE, nsamples = 100)

just add (within for loop)

df<-rbind(df,data.frame(file,Haarimtest))
njmnli
  • 11
  • 1