Convert jpg to greyscale csv using R

I have a JPG image folder that I am trying to classify for competitions. I saw some code in Python that I think will execute this on the forums, but wondered if it is possible to do this in R? I am trying to convert this folder from many jpg images to csv files that have numbers showing the shades of gray of each pixel similar to a manual digitizer here http://www.kaggle.com/c/digit-recognizer/

So basically jpg → .csv in R, showing the numbers for the gray shades of each pixel to be used for classification. I would like to place a random forest or linear model on it.

+6
source share
1 answer

There are several formulas for how to do this on this link . The raster package is one approach. This basically converts the RGB stripes into a single black and white strip (this makes it smaller in size, and I assume you want to.)

 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 that the EBImage package EBImage also help in 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") 
+7
source

Source: https://habr.com/ru/post/979649/


All Articles