Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
684 views
in Technique[技术] by (71.8m points)

extract RGB channels from a jpeg image in R

In order to classify a jpeg image in R, I would like to get the RGB values of each pixel.

My question: Is there a way to extract RGB channels from a jpeg image in R ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have several package to read in JPEG. Here I use package jpeg:

library(jpeg)
img <- readJPEG("Rlogo.jpg")

dim(img)
[1]  76 100   3

As you can see, there is 3 layers: they correspond to your R, G and B values. In each layer, each cell is a pixel.

img[35:39,50:54,]
, , 1

          [,1]      [,2]      [,3]      [,4]      [,5]
[1,] 0.5098039 0.5921569 0.4549020 0.3372549 0.1921569
[2,] 0.5098039 0.6000000 0.4549020 0.3372549 0.1921569
[3,] 0.5137255 0.6000000 0.4549020 0.3450980 0.1921569
[4,] 0.5215686 0.6039216 0.4627451 0.3450980 0.1921569
[5,] 0.5215686 0.6039216 0.4627451 0.3450980 0.1882353

, , 2

          [,1]      [,2]      [,3]      [,4]      [,5]
[1,] 0.5882353 0.6666667 0.5098039 0.3803922 0.2156863
[2,] 0.5882353 0.6627451 0.5098039 0.3803922 0.2156863
[3,] 0.5843137 0.6627451 0.5098039 0.3764706 0.2156863
[4,] 0.5843137 0.6627451 0.5058824 0.3764706 0.2117647
[5,] 0.5843137 0.6627451 0.5058824 0.3764706 0.2156863

, , 3

          [,1]      [,2]      [,3]      [,4]      [,5]
[1,] 0.7254902 0.7921569 0.6156863 0.4588235 0.2705882
[2,] 0.7254902 0.7921569 0.6156863 0.4588235 0.2784314
[3,] 0.7254902 0.7921569 0.6156863 0.4588235 0.2784314
[4,] 0.7176471 0.7921569 0.6156863 0.4666667 0.2862745
[5,] 0.7176471 0.7921569 0.6156863 0.4666667 0.2862745

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...