【发布时间】:2018-04-16 00:05:01
【问题描述】:
我想知道如何从 R 上的图形创建具有像素颜色信息的矩阵。如示例中所示,其想法是将灰度图像处理为这样的矩阵:
[,1] [,2] [,3]
[1,] "Black" "White" "Black"
[2,] "White" "Black" "White"
[3,] "Black" "White" "White"
【问题讨论】:
标签: r
我想知道如何从 R 上的图形创建具有像素颜色信息的矩阵。如示例中所示,其想法是将灰度图像处理为这样的矩阵:
[,1] [,2] [,3]
[1,] "Black" "White" "Black"
[2,] "White" "Black" "White"
[3,] "Black" "White" "White"
【问题讨论】:
标签: r
你可以使用image函数:
## Generating the pixel matrix (1 is black, 0 is white)
my_image <- matrix(c(1,0,1,0,1,0,1,0,0), 3, 3)
## Remove the plot margin
par(mar = c(0,0,0,0))
## Plot the picture
image(my_image, col = c("white", "black"), xaxt = "n", yaxt = "n")
【讨论】:
由于我没有找到解决此问题的方法,因此我创建了一个 R 包并在 CRAN 上提供了它。如何获取此信息的示例:
# Install package
install.packages("bwimage")
# Load package
library("bwimage")
# Path to an example provided by bwimage package
bush<-system.file("extdata/bush.JPG",package ="bwimage")
# Convert the image to a binary matrix. For each pixel, the intensity of red, green and blue is averaged and compared to a threshold. If the average intensity is less than the threshold (default is 50%) the pixel will be set as black (1 in the matrix), otherwise it will be white (0).
threshold_color(bush,"jpeg", "frame_fixed",target_width = 15,target_height=15)
【讨论】: