【问题标题】:How to extract colors of pixels of grayscale image in R如何在R中提取灰度图像像素的颜色
【发布时间】:2018-04-16 00:05:01
【问题描述】:

我想知道如何从 R 上的图形创建具有像素颜色信息的矩阵。如示例中所示,其想法是将灰度图像处理为这样的矩阵:

     [,1]    [,2]    [,3]   
[1,] "Black" "White" "Black"
[2,] "White" "Black" "White"
[3,] "Black" "White" "White"

【问题讨论】:

标签: r


【解决方案1】:

你可以使用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")

【讨论】:

    【解决方案2】:

    由于我没有找到解决此问题的方法,因此我创建了一个 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) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-08
      • 1970-01-01
      • 2015-10-26
      • 2020-01-27
      • 1970-01-01
      • 2023-03-07
      • 2015-06-08
      相关资源
      最近更新 更多