convert jpg to greyscale csv using R

后端 未结 1 536
-上瘾入骨i
-上瘾入骨i 2020-12-11 16:56

I have a folder of JPG images that I\'m trying to classify for a kaggle competition. I have seen some code in Python that I think will accomplish this on the forums, but was

相关标签:
1条回答
  • 2020-12-11 17:43

    There are some formulas for how to do this at this link. The raster package is one approach. THis basically converts the RGB bands to one black and white band (it makes it smaller in size, which I am guessing what you want.)

    library(raster)
    color.image <- brick("yourjpg.jpg")
    
    # Luminosity method for converting to greyscale
    # Find more here http://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/
    color.values <- getValues(color.image)
    bw.values <- color.values[,1]*0.21 + color.values[,1]*0.72 + color.values[,1]*0.07
    

    I think the EBImage package can also help for this problem (not on CRAN, install it through source:

    source("http://bioconductor.org/biocLite.R")
    biocLite("EBImage")
    library(EBImage)
    
    color.image <- readImage("yourjpg.jpg")
    bw.image <- channel(color.image,"gray")
    writeImage(bw.image,file="bw.png")
    
    0 讨论(0)
提交回复
热议问题