Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从R中的灰度图像中获取像素矩阵?_R_Image_Image Processing - Fatal编程技术网

如何从R中的灰度图像中获取像素矩阵?

如何从R中的灰度图像中获取像素矩阵?,r,image,image-processing,R,Image,Image Processing,当灰度图像由矩阵表示时,矩阵的每个元素决定相应像素的强度。为方便起见,当前大多数数字文件使用0(表示最小强度的黑色)和255(表示最大强度的白色)之间的整数,总灰度为256=2^8 有没有办法获得R中灰度图像的像素矩阵,其像素值范围为0到255 了解我是否可以在R中以首选维度(例如,$28\乘以28$)调整图像大小,然后将其转换为像素矩阵(其元素范围为0到255)也会很有帮助 如果原始图像是RGB,但我需要灰度矩阵,会发生什么情况?R软件包png提供了readPNG()函数,该函数可以将png格

当灰度图像由矩阵表示时,矩阵的每个元素决定相应像素的强度。为方便起见,当前大多数数字文件使用0(表示最小强度的黑色)和255(表示最大强度的白色)之间的整数,总灰度为256=2^8

有没有办法获得R中灰度图像的像素矩阵,其像素值范围为0到255

了解我是否可以在R中以首选维度(例如,$28\乘以28$)调整图像大小,然后将其转换为像素矩阵(其元素范围为0到255)也会很有帮助


如果原始图像是RGB,但我需要灰度矩阵,会发生什么情况?

R软件包
png
提供了
readPNG()
函数,该函数可以将png格式的光栅图形(由“像素矩阵”组成)读取为R。它返回一个带有[0,1]中灰度值的矩阵或三个RGB值在[0,1]中的矩阵

为了在[0,1]和{0,…,255}之间进行转换,如果需要,只需将255与整数相乘或相除即可

例如,对于RGB和灰度之间的转换,您可以使用
colorspace
软件包中的
desaturate()
函数

例如,让我们下载您建议的图像:

download.file("http://www.greenmountaindiapers.com/skin/common_files/modules/Socialize/images/twitter.png",
  destfile = "twitter.png")
然后我们加载上面提到的包:

library("png")
library("colorspace")
首先,我们将PNG图像读入一个尺寸为28 x 28 x 4的数组
x
。因此,图像具有28 x 28像素和四个通道:红色、绿色、蓝色和alpha(用于半透明度)

我希望这些版本中至少有一个是您想要的。为了检查像素矩阵,我编写了一个方便的可视化小函数:

pixmatplot <- function (x, ...) {
  d <- dim(x)
  xcoord <- t(expand.grid(1:d[1], 1:d[2]))
  xcoord <- t(xcoord/d)
  par(mar = rep(1, 4))
  plot(0, 0, type = "n", xlab = "", ylab = "", axes = FALSE, 
    xlim = c(0, 1), ylim = c(0, 1), ...)
  rect(xcoord[, 2L] - 1/d[2L], 1 - (xcoord[, 1L] - 1/d[1L]), 
    xcoord[, 2L], 1 - xcoord[, 1L], col = x, border = "transparent")
}

如果您有一个较大的图像,并希望将其设置为28 x 28,我将平均相应行/列的灰度值,并将结果插入到所需尺寸的矩阵中

最后一点注意:虽然在R中可以完成所有这些,但使用图像处理软件可能更方便。根据您的目标,使用ImageMagick的
mogrify可能更容易,例如:

mogrify -resize 28 -type grayscale twitter.png

下面是一个从灰度png转换和绘制图像的示例。请确保先安装相关软件包

library(png)
library(RCurl)
myurl = "https://postgis.net/docs/manual-dev/images/apple_st_grayscale.png"
my_image =  readPNG(getURLContent(myurl))
img_mat=my_image[,,1] # will hold the grayscale values divided by 255
img_mat=t(apply(img_mat, 2, rev)) # otherwise the image will be rotated
image(img_mat, col  = gray((0:255)/255)) # plot in grayscale

谢谢你的回答。你能告诉我如果我有一个名为“image.png”的图像,那么如何使用desaturate()命令来获取灰度图像吗?还有关于如何将图像调整为R中的首选尺寸(例如,28×28像素)?如果我读取图像(),为什么它会给我一个28×28×4的矩阵??你能给我解释一下吗?再次感谢。我在回复中添加了一个例子,并对其进行了解释。我非常喜欢这个答案。它消除了在解决r中与图像相关的问题时可能想到的所有其他疑问。
pixmatplot(y)
pixmatplot(yg)
mogrify -resize 28 -type grayscale twitter.png
library(png)
library(RCurl)
myurl = "https://postgis.net/docs/manual-dev/images/apple_st_grayscale.png"
my_image =  readPNG(getURLContent(myurl))
img_mat=my_image[,,1] # will hold the grayscale values divided by 255
img_mat=t(apply(img_mat, 2, rev)) # otherwise the image will be rotated
image(img_mat, col  = gray((0:255)/255)) # plot in grayscale