从R中的jpeg图像中提取RGB通道

时间:2022-10-06 21:22:51

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

为了在R中对jpeg图像进行分类,我想获取每个像素的RGB值。

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

我的问题是:有没有办法从R中的jpeg图像中提取RGB通道?

3 个解决方案

#1


14  

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

您有几个包可以在JPEG中读取。这里我使用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.

如您所见,有3个层:它们对应于R、G和B值。在每个层中,每个单元格都是一个像素。

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

#2


7  

I recommend the biOpspackage for image manipulation.

我推荐使用生物包装来处理图像。

Here is an example:

library(biOps)
x <- readJpeg(system.file("samples", "violet.jpg", package="biOps"))
plot(x)

r <- imgRedBand(x)
plot(r)
image(x[,,1])

g <- imgGreenBand(x)
plot(g)
image(x[,,2])

b <- imgBlueBand(x)
plot(b)
image(x[,,3])

Visual example:

redPal <- colorRampPalette(c("black", "red"))
greenPal <- colorRampPalette(c("black", "green"))
bluePal <- colorRampPalette(c("black", "blue"))

x11(width=9, height=2.5)
par(mfcol=c(1,3))
image(x=seq(ncol(r)), y=seq(nrow(r)), z=t(r), asp=1, xaxt="n", yaxt="n", bty="n", xlab="", ylab="", main="red channel", col=redPal(256))
image(x=seq(ncol(g)), y=seq(nrow(g)), z=t(g), asp=1, xaxt="n", yaxt="n", bty="n", xlab="", ylab="", main="green channel", col=greenPal(256))
image(x=seq(ncol(b)), y=seq(nrow(b)), z=t(b), asp=1, xaxt="n", yaxt="n", bty="n", xlab="", ylab="", main="blue channel", col=bluePal(256))

从R中的jpeg图像中提取RGB通道

#3


5  

I like the approach via R's biOps package. After loading your data into canvas, you're able to convert your jpg file from imagedata to raster and do some further processing. Here's my code:

我喜欢R的biOps软件包。在将数据加载到画布之后,您可以将jpg文件从imagedata转换为光栅,并进行一些进一步的处理。这是我的代码:

# Required packages
library(biOps)
library(raster)

# Load and plot data
data(logo)
jpg <- logo

plot.imagedata(jpg)

# Convert imagedata to raster
rst.blue <- raster(jpg[,,1])
rst.green <- raster(jpg[,,2])
rst.red <- raster(jpg[,,3])

# Plot single raster images and RGB composite
plot(stack(rst.blue, rst.green, rst.red), 
     main = c("Blue band", "Green band", "Red band"))
plotRGB(stack(rst.blue, rst.green, rst.red))

#1


14  

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

您有几个包可以在JPEG中读取。这里我使用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.

如您所见,有3个层:它们对应于R、G和B值。在每个层中,每个单元格都是一个像素。

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

#2


7  

I recommend the biOpspackage for image manipulation.

我推荐使用生物包装来处理图像。

Here is an example:

library(biOps)
x <- readJpeg(system.file("samples", "violet.jpg", package="biOps"))
plot(x)

r <- imgRedBand(x)
plot(r)
image(x[,,1])

g <- imgGreenBand(x)
plot(g)
image(x[,,2])

b <- imgBlueBand(x)
plot(b)
image(x[,,3])

Visual example:

redPal <- colorRampPalette(c("black", "red"))
greenPal <- colorRampPalette(c("black", "green"))
bluePal <- colorRampPalette(c("black", "blue"))

x11(width=9, height=2.5)
par(mfcol=c(1,3))
image(x=seq(ncol(r)), y=seq(nrow(r)), z=t(r), asp=1, xaxt="n", yaxt="n", bty="n", xlab="", ylab="", main="red channel", col=redPal(256))
image(x=seq(ncol(g)), y=seq(nrow(g)), z=t(g), asp=1, xaxt="n", yaxt="n", bty="n", xlab="", ylab="", main="green channel", col=greenPal(256))
image(x=seq(ncol(b)), y=seq(nrow(b)), z=t(b), asp=1, xaxt="n", yaxt="n", bty="n", xlab="", ylab="", main="blue channel", col=bluePal(256))

从R中的jpeg图像中提取RGB通道

#3


5  

I like the approach via R's biOps package. After loading your data into canvas, you're able to convert your jpg file from imagedata to raster and do some further processing. Here's my code:

我喜欢R的biOps软件包。在将数据加载到画布之后,您可以将jpg文件从imagedata转换为光栅,并进行一些进一步的处理。这是我的代码:

# Required packages
library(biOps)
library(raster)

# Load and plot data
data(logo)
jpg <- logo

plot.imagedata(jpg)

# Convert imagedata to raster
rst.blue <- raster(jpg[,,1])
rst.green <- raster(jpg[,,2])
rst.red <- raster(jpg[,,3])

# Plot single raster images and RGB composite
plot(stack(rst.blue, rst.green, rst.red), 
     main = c("Blue band", "Green band", "Red band"))
plotRGB(stack(rst.blue, rst.green, rst.red))