【问题标题】:Converting images from RGB to HSL and back again with julia使用 julia 将图像从 RGB 转换为 HSL 并再次转换回来
【发布时间】:2015-08-22 05:59:33
【问题描述】:

我一直在尝试打开一些 RGB 图像,将数据视为 HSL 像素的 2D 数组,在 HSL 空间中操作像素,转换回 RGB 并将操作后的图像写入文件。但是我不太明白(很棒的)julia 包ColorImages 中的转换是如何工作的。

例如,我希望下面的代码(部分按照this SO question 中的示例编写)编写非常类似于此图像文件的内容(如 test_1.png 和 test_2.png):

但是,下面的代码实际上生成了这个更暗的图像:

我应该如何重新排列数组或图像以获得我期望的输出?

using Color, Images

# Download file, read it in, convert colourspace to HSL and recast as array
fname=download("https://farm9.staticflickr.com/8725/17074451907_2381037c7d_m_d.jpg")
rgb=imread(fname)
hsl=convert(Image{HSL},float32(rgb))
hslArr=reinterpret(data(hsl))

# I would like to manipulate HSL data here...

# Two ways to convert manipulated array back to HSL image
hsl_1=Image(hslArr; colorspace="HSL", colordim=1, spatialorder=["x","y"])
hsl_2=reinterpret(HSL{Float32},hslArr)

# Two ways to convert HSL image to RGB image 
rgb_1=convert(Image{RGB},hsl_1)
rgb_2=convert(Array{RGB{Float32}},hsl_2)

# Finally, write images to file
imwrite(rgb_1,"test_1.png")
imwrite(rgb_2,"test_2.png")

更新

感谢@rickhg12hs 在 Color.jl 模块中发现错误,经过以下步骤后,我从上面的代码中得到了预期的输出:

  1. 在 github 上 fork source repository for Color.jl
  2. 更正转换函数(如下),将更改推送到我的 Color.jl 分支
  3. 移除 julia 自带的默认 Color.jl 模块
  4. 使用 Julia 的 git 机制安装我的分叉包。
  5. 重启朱莉娅

我无法弄清楚如何与以前的版本并行安装模块的分叉版本,但执行以下操作(然后重新启动 julia)应该可以暂时修复错误:

Pkg.rm("Color")
Pkg.clone("https://github.com/CnrLwlss/Color.jl.git","Color")
Pkg.checkout("Color","master")

一旦拉取请求通过,将需要切换回原始颜色模块。

【问题讨论】:

  • 在我看来就像Color.jl 中的 RGB 到 HSL 转换错误。 Github pull request 已提交修复。

标签: image colors type-conversion julia color-space


【解决方案1】:

Color.jl 得到更新并实施/通过更多测试之前,您可以在Color/src/conversions.jl 中更改单个字符,以最有可能解决此特定问题。在第 156 行将 - 更改为 +

   150  # Everything to HSL
   151  # -----------------
   152  
   153  function convert{T}(::Type{HSL{T}}, c::AbstractRGB)
   154      c_min = min(c.r, c.g, c.b)
   155      c_max = max(c.r, c.g, c.b)
   156      l = (c_max + c_min) / 2     # <-- Changed '-' to '+'

在我的机器上,你的 HSL 转换鸟现在看起来很棒!

【讨论】:

  • 请注意,如果 Julia 确定您的包是 "dirty",就像上面的更改一样,它不会更新包。当Color.jl更新时,您可以进入~/.juliagit checkout -- src/conversions.jl下的Color目录。然后Pkg.update() 将更新这个包,因为它不再是"dirty"
  • 现在运行代码,感谢您修复它@rickhg12hs。已在上方添加更新以显示目前的工作轮次。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-07
  • 1970-01-01
  • 2021-06-29
  • 1970-01-01
  • 2012-11-04
  • 2016-08-11
相关资源
最近更新 更多