Repa示例
在repa存储库中有一些有用的代码示例。它们都对类型为Array U DIM2 a
或Array DIM2 Float
或Array 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:
- repa-devil 包支持PNG、BMP、JPG、TIF格式。不幸的是,它们被解析为一个数组类型,与上面的repa示例不符,如repa-devil维护者在这里确认的那样。
- 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 Float
或Array DIM2 Float
或Array U DIM2 Double
类型的值。
问题
- 除了
toGreyScale
,repa-examples 中的所有示例都只适用于灰度图像吗?虽然从类型上看这是有道理的,但令人惊讶的是,没有针对彩色图像的 repa 示例。例如,为什么blur
的类型不是这样的:blur :: Monad m => Int -> Array U DIM2 (Word8, Word8, Word8) -> m (Array U DIM2 (Word8, Word8, Word8))
Array U DIM2 Float
中的浮点数捕获了什么值?它是在 0 到 255 之间的灰度值吗?- repa-io 包中是否有添加 JPG/PNG/TIF IO 支持的工作?
readImageFromBMP
函数,该函数返回一个三维数组,其中上标是像素位置,下标是单个通道的值。因此,如果有3个通道,则每个(x,y)对将有3个值。如果这更容易,您可以使用readComponentsFromBMP
函数,它返回一个三元组数组,每个颜色分量一个。模糊示例简单地使用fromIntegral
和truncate
将Word8
转换为Double
,然后再转回去。 - user2407038