gpt4 book ai didi

haskell - 使用 Haskell repa 数组库进行彩色图像文件 IO

转载 作者:行者123 更新时间:2023-12-02 14:02:07 24 4
gpt4 key购买 nike

我正在通过尝试大量的 programming examples 来探索 Haskell repa 库。我的目标是使用 repa 实现常见的图像处理算法。

修复示例

有一些helpful code examples在 repa 存储库中。它们都对Array U DIM2 aArray DIM2 FloatArray U DIM2 Double类型的图像进行操作。

-- three image types used below
type Image = Array U DIM2 Double
type Image = Array DIM2 Float
type Image = Array U DIM2 (Word8, Word8, Word8)

-- examples/Blur/src-repa/Main.hs
blur :: Monad m => Int -> Array U DIM2 Double -> m (Array U DIM2 Double)

-- examples/Laplace/src-repa/SolverStencil.hs
solveLaplace :: Monad m => Int -> Array U DIM2 Double -> Array U DIM2 Double -> Array U DIM2 Double -> m (Array U DIM2 Double)

-- examples/Sobel/src-repa/SolverSeparated.hs
type Image = Array DIM2 Float
gradientX_sep :: Image -> Image
gradientX1 :: Image -> Image
gradientX2 :: Image -> Image
gradientY_sep :: Image -> Image
gradientY2 :: Image -> Image

-- examples/Canny/src-repa/Main.hs
type Image a = Array U DIM2 a
toGreyScale :: Image (Word8, Word8, Word8) -> IO (Image Float)
blurSepX :: Image Float -> IO (Image Float)
blurSepY :: Image Float -> IO (Image Float)
gradientX :: Image Float -> IO (Image Float)
gradientY :: Image Float -> IO (Image Float)
suppress :: Float -> Float -> Image (Float, Word8) -> IO (Image Word8)
wildfire :: Image Word8 -> Array U DIM1 Int -> IO (Image Word8)
selectStrong :: Image Word8 -> IO (Array U DIM1 Int)
gradientMagOrient :: Float -> Image Float -> Image Float -> IO (Image (Float, Word8))

图像文件IO

图像文件IO有两种选择:

  1. repa-devil支持 PNG、BMP、JPG、TIF 的包。不幸的是,它们被解析为不符合上面 repa 示例的数组类型,正如 repa-devil 维护者 here 所确认的那样。 .
  2. repa-io包更接近于 repa-examples 中图像的数组类型参数,但仅支持 BMP 文件。

repa-devil(与 repa-examples 不兼容)

repa-examples 包中的图像类型为Array F DIM3 Word8,如果是灰度图像,则类型为Array F DIM2 Word8。这意味着repa-devil不能用于读取要与repa-examples中的示例一起处理的图像,因为repa-examples中的图像是二维数组,而repa-devil中的图像是三维数组。

readImage :: FilePath -> IL Image
writeImage :: FilePath -> Image -> IL ()
data Image = RGBA (Array F DIM3 Word8)
| RGB (Array F DIM3 Word8)
| BGRA (Array F DIM3 Word8)
| BGR (Array F DIM3 Word8)
| Grey (Array F DIM2 Word8)

repa-io(与 repa-examples 的一些兼容性)

repa-examples 和 repa-io 之间有更密切的对应关系。

readImageFromBMP :: FilePath -> IO (Either Error (Array U DIM2 (Word8,Word8, Word8)))
writeImageToBMP :: FilePath -> Array U DIM2 (Word8, Word8, Word8) -> IO ()

这次,BMP 图像文件被解析为一个二维数组,其元素类型为 (Word8,Word8,Word8),大概表示 R、G 和 B 值。即便如此,repa-examples 包中唯一兼容的函数是上面的 toGreyScale 。所有其他函数都对 Array U DIM2 FloatArray DIM2 FloatArray U DIM2 Double 类型的值进行操作。

问题

  1. 除了 toGreyScale 之外,repa-examples 中的所有示例都只适合在灰度图像上使用吗?虽然从类型来看这是有意义的,但令人惊讶的是没有彩色图像的修复示例。例如,为什么 blur 的类型不是:blur::Monad m => Int -> Array U DIM2 (Word8, Word8, Word8) -> m (Array U DIM2 (Word8, Word8, Word8))
  2. Array U DIM2 Float 中捕获的浮点值是多少?灰度值是否在 0 到 255 之间?
  3. 在 repa-io 软件包中添加 JPG/PNG/TIF IO 支持方面是否做过任何工作?

最佳答案

  1. 没有理由。您可以将单 channel 模糊功能应用于彩色图像的每个 RGB channel 。
  2. 对于表示为 Array U DIM2 Float 的图像,元素的范围通常为 0.0 - 1.0
  3. 我认为 JPG/PNG 图像的加载不应放入 repa-io 包中,因为这会导致对外部编解码器库的依赖。使用其他软件包之一(例如 repa-devil)来加载图像。

repa-devil 包包装了外部 DevIL 库,因此加载的图像最终位于外部内存中 - 因此 F 索引位于 Array F DIM3 Word8 中。 DevIL 库本身不知道如何在 Haskell 堆中构建未装箱的 U 数组。

这些示例只是示例,我并不想让 repa-examples 成为一个功能齐全的图像处理库。事实上,有些数组使用外部 F 表示,有些数组使用未装箱的 U 表示,这反射(reflect)了包装外部代码的标准问题。如果您想要一个统一的图像处理 API,那么您要么需要更改边界处的图像表示(这可能会引入冗余复制),要么使函数更加多态(这会使它们的类型变得复杂),要么以某种方式隐藏问题(这会导致成本增加)。模型不明显)。无论您选择哪个选项,都会有人提示。

关于haskell - 使用 Haskell repa 数组库进行彩色图像文件 IO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21098864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com