我recently asked how to convert Float32
or Uint8
arrays into images在 Images
包裹。我得到了 Float32
的答案案例,但我仍然无法弄清楚如何保存 Uint8
大批。
例如,让我们创建一个随机 Uint8
使用传统 Matlab 方案的数组,其中维度为 (m,n,3)
:
array = rand(Uint8, 50, 50, 3);
img = convert(Image, array);
使用与 Float32
相同的方法案件,imwrite(img, "out.png")
失败并显示消息ERROR: method 'mapinfo' has no method matching mapinfo(::Type{ImageMagick}, ::Image{Uint8, 3, Image{Uint8, 3, Array{Uint8, 3}}}).
我查了文档,and it says
If data encodes color information along one of the dimensions of the array (as opposed to using a ColorValue array, from the Color.jl package), be sure to specify the "colordim" and "colorspace" in properties.
但是,检查
img
先前创建的对象显示它有 colordim = 3
和 colorspace = RGB
已经设置好了,所以这不是问题。然后我在文档中搜索了
MapInfo
的所有实例。 .在 core.md有一种情况:scalei: a property that controls default contrast scaling upon display. This should be a MapInfo value, to be used for setting the contrast upon display. In the absence of this property, the range 0 to 1 will be used.
但是没有关于什么是
MapInfo
的信息。对象是,所以我看得更远,在 function_reference.md它说:Here is how to directly construct the major concrete MapInfo types:
MapNone(T), indicating that the only form of scaling is conversion to type T. This is not very safe, as values "wrap around": for example, converting 258 to a Uint8 results in 0x02, which would look dimmer than 255 = 0xff.
...
以及其他一些例子。所以我试图指定
scalei = MapNone(Uint8)
如下:img2 = Image(img, colordim = 3, colorspace = "RGB", scalei = MapNone(Uint8));
imwrite(img, "out.png")
但又犯了同样的错误。你如何编码
Uint8
图像数据使用 Images
在 Julia ?
最佳答案
您可以在基本类型数组之间来回转换,例如 UInt8
和 color types 的数组.这些转换通过两个函数以统一的方式实现:colorview
和 channelview
.
例子
转换 UInt8
的数组到 RGB
的数组:
arr = rand(UInt8, 3, 50, 50)
img = colorview(RGB, arr / 255)
转换回 channel View :channelview(img)
笔记[0,1]
中。作为浮点。我手动转换了 UInt8
至 Float64
使用显式除法 255
.使用 reinterpret
可能有一种更通用的方法来实现此结果。或 Images.jl 中的其他一些函数colorview
和 channelview
函数假设 channel 维度是数组的第一个维度。您可以使用 permutedims
如果您的 channel 位于不同的维度,或者使用 Images.jl 中的某些函数(也许 reinterpretc
?)来高效地完成它而无需内存副本。关于image - 使用 Julia 中的图像将 Uint8 数组导出为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25715909/