使用R将jpg转换为灰度csv [英] convert jpg to greyscale csv using R

查看:440
本文介绍了使用R将jpg转换为灰度csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JPG图片的文件夹,我想分类为一个kaggle比赛。我看到了一些代码在Python,我认为将在论坛上完成这一点,但是想知道是否可以在R?我试图将许多jpg图像的这个文件夹转换为具有显示每个像素的灰度的数字的csv文件,类似这里的手数识别器 http://www.kaggle.com/c/digit-recognizer/

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 wondering is it possible to do in R? I'm trying to convert this folder of many jpg images into csv files that have numbers showing the grayscale of each pixel, similar to the hand digit recognizer here http://www.kaggle.com/c/digit-recognizer/

所以基本上jpg - > .csv in R,显示用于分类的每个像素的灰度的数字。我想把一个随机森林或线性模型。

So basically jpg -> .csv in R, showing numbers for the grayscale of each pixel to use for classification. I'd like to put a random forest or linear model on it.

推荐答案

有一些公式如何做到这一点此链接 raster 包是一种方法。这基本上将RGB带转换为一个黑白波段(它使它的尺寸更小,我猜你想要什么。)

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

我认为 EBImage 包也可以帮助解决这个问题(不在CRAN上,通过 source

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")

这篇关于使用R将jpg转换为灰度csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆