使用 julia 将图像从 RGB 转换为 HSL,然后再转换回来
Converting images from RGB to HSL and back again with julia
我一直在尝试打开一些 RGB 图像,将数据视为 HSL 像素的二维数组,处理 HSL 中的像素 space,转换回 RGB 并将处理后的图像写入文件。但是我不太明白(很棒的)julia 包 Color and Images 中的转换是如何工作的。
例如,我希望下面的代码(部分是根据 this SO 问题中的示例编写的)编写非常类似于此图像文件的内容(如 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 模块中发现错误,我在执行以下步骤后从上面的代码得到了预期的输出:
- 在 github
上分叉 source repository for Color.jl
- 修改转换函数(如下),推送修改到我的Color.jl
的fork
- 删除 julia
附带的默认 Color.jl 模块
- 使用 Julia 的 git 机制安装我的分叉包。
- 重启朱莉娅
我一直无法弄清楚如何与以前的版本并行安装模块的分叉版本,但是执行以下操作(然后重新启动 julia)应该可以暂时修复错误:
Pkg.rm("Color")
Pkg.clone("https://github.com/CnrLwlss/Color.jl.git","Color")
Pkg.checkout("Color","master")
一旦拉取请求通过,将需要切换回原始颜色模块。
在 Color.jl
得到更新和更多测试 implemented/passed 之前,您可以在 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 转换鸟现在看起来很棒!
我一直在尝试打开一些 RGB 图像,将数据视为 HSL 像素的二维数组,处理 HSL 中的像素 space,转换回 RGB 并将处理后的图像写入文件。但是我不太明白(很棒的)julia 包 Color and Images 中的转换是如何工作的。
例如,我希望下面的代码(部分是根据 this SO 问题中的示例编写的)编写非常类似于此图像文件的内容(如 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 模块中发现错误,我在执行以下步骤后从上面的代码得到了预期的输出:
- 在 github 上分叉 source repository for Color.jl
- 修改转换函数(如下),推送修改到我的Color.jl 的fork
- 删除 julia 附带的默认 Color.jl 模块
- 使用 Julia 的 git 机制安装我的分叉包。
- 重启朱莉娅
我一直无法弄清楚如何与以前的版本并行安装模块的分叉版本,但是执行以下操作(然后重新启动 julia)应该可以暂时修复错误:
Pkg.rm("Color")
Pkg.clone("https://github.com/CnrLwlss/Color.jl.git","Color")
Pkg.checkout("Color","master")
一旦拉取请求通过,将需要切换回原始颜色模块。
在 Color.jl
得到更新和更多测试 implemented/passed 之前,您可以在 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 转换鸟现在看起来很棒!