Git Product home page Git Product logo

Comments (3)

stm avatar stm commented on July 20, 2024

The problem with your code is that system.file() is intended to provide file paths and names of files that are part of an R package. Hence, in your command i <- img_read(system.file("example_images", i, package = "imagefluency")) you are telling R to search for the image high_1.jpg (if i = 1) in the subdirectory example_images of the {imagefluency} package.

If you want to analyze images stored in a folder on your system, the following code should work.

library(imagefluency)

# make sure that the folder `example_images` is a subfolder of your current working directory
# to check the current working directory, use `getwd()`
# to change the current working directory, use `setwd()`
dir.exists("example_images/")

# define image list
imglist <- c("high_1.jpg", "high_2.jpg", "high_3.jpg", "high_4.jpg", "high_5.jpg", "high_6.jpg")

for (i in imglist) {
  # read image (file path is constructed with `paste0()`)
  img <- img_read(paste0("example_images/", i))
  
  # calculate and print scores
  print(data.frame(
    img_complexity(img),
    img_contrast(img),
    mean(rgb2gray(img)),
    img_self_similarity(img))
  )
}

Alternatively, instead of constructing the file paths manually, a more elegant way would be to let R search for images in the example_images folder.

# here, we only search for images with the extension .jpg
imglist <- list.files("example_images", pattern = '*.jpg$', full.names = TRUE)

for (i in imglist) {
  # read image
  img <- img_read(i)
  
  # calculate and print scores
  print(data.frame(
    img_complexity(img),
    img_contrast(img),
    mean(rgb2gray(img)),
    img_self_similarity(img))
  )
}

If your goal is to store the results of all images in a data frame, then the following code can be used:

imglist <- list.files("example_images", pattern = '*.jpg$', full.names = TRUE)

# data frame to store results 
results <- data.frame(filename = basename(imglist),
                      complexity = NA, contrast = NA,
                      mean = NA, selfsimilarity = NA)

# loop
for(i in seq_along(imglist)){
  # print loop info
  print(paste('** Estimating scores for image',i,'**'))
  
  # 1. read image into R
  img <- img_read(imglist[i])
  
  # 2. estimate scores and store the results
  results$complexity[i] <- img_complexity(img)
  results$contrast[i] <- img_contrast(img)
  results$mean[i] <- mean(rgb2gray(img))
  results$selfsimilarity[i] <- img_self_similarity(img)
}

# print data frame
print(results)

from imagefluency.

bssengul avatar bssengul commented on July 20, 2024

Hello, thank you for your detailed answer. example_images folder is actually in the imagefluency folder in my computer. It's also in the R folder. I added my images to example_images folder but the code still did not work.

from imagefluency.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.