image - 使用 Julia 中的图像将 Uint8 数组导出为图像

标签 image julia

recently asked how to convert Float32 or Uint8 arrays into imagesImages包裹。我得到了 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 = 3colorspace = 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 ?

最佳答案

您可以在基本类型数组之间来回转换,例如 UInt8color types 的数组.这些转换通过两个函数以统一的方式实现:colorviewchannelview .
例子
转换 UInt8 的数组到 RGB 的数组:

arr = rand(UInt8, 3, 50, 50)
img = colorview(RGB, arr / 255)
转换回 channel View :
channelview(img)
笔记
  • 在此示例中,RGB 颜色类型要求数组的条目位于 [0,1] 中。作为浮点。我手动转换了 UInt8Float64使用显式除法 255 .使用 reinterpret 可能有一种更通用的方法来实现此结果。或 Images.jl 中的其他一些函数
  • colorviewchannelview函数假设 channel 维度是数组的第一个维度。您可以使用 permutedims如果您的 channel 位于不同的维度,或者使用 Images.jl 中的某些函数(也许 reinterpretc ?)来高效地完成它而无需内存副本。
  • 关于image - 使用 Julia 中的图像将 Uint8 数组导出为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25715909/

    相关文章:

    css - 如何在 svg 中间添加形状

    java - 在指定目录启动 JFileChooser 并仅显示特定类型的文件

    javascript - 将图像放到 Canvas 上

    css - 在 CSS 中定位 Picture 元素时,我们应该使用 img 还是图片选择器?

    julia - 如何使用 Julia 从 .CSV 文件数据中读取 DateTime 数据类型

    image - Kinect,使用深度图像获取Z值

    julia - 索引 Julia DataFrame 时感叹号的含义是什么?

    linux - 无法在 Julia 中打开文件

    julia - 尝试在 Julia 中添加 Plots 包时,为什么会出现 "Unsatisfiable requirements detected for package HTTP"错误?

    julia - IJulia 中的 ProgressBar 打印每一行