我正在尝试使用fromPtr
加速 io 将图像从 OpenCV 中舀出并放入 Accelerate 数组中。此功能的文档很迟钝,并且此示例无法编译(由于 Criterion,我无法安装加速示例)。这段代码:
import Foreign.Ptr
import Foreign.C.Types
import AI.CV.CxCore
import AI.CV.HighGui
import Data.Array.Accelerate as A
import Data.Array.Accelerate.IO as A
import Data.Word
main :: IO ()
main = do
capture <- cvCreateCameraCapture 0
frame <- cvQueryFrame capture
imgPtr <- cvGetImage frame -- Ptr IplImage -> Ptr Word
arr <- A.fromPtr (Z :. 480 :. 640) ((), castPtr imgPtr)
return ()
结果是Couldn't match expected type 'BlockPtrs (EltRepr e0)' with actual type '((), Ptr b0)'. The type variables 'e0', 'b0' are ambiguous.
删除castPtr
给了我Couldn't match expected type 'BlockPtrs (EltRepr e0)' with actual type '((), Ptr Word8)'. The type variable 'e0' is ambiguous.
查看BlockPtrs
and的定义EltRepr
只会让我更加困惑。但是,向表达式添加类型签名,如(((), imgPtr) :: BlockPtrs ((), Word8))
给出预期类型BlockPtrs (EltRepr e0)
和实际类型BlockPtrs ((), Word8)
。
这里有人有经验fromPtr
吗?
编辑:越来越近。我之前尝试过使用构造函数 EltRepr,但我没有想到要导入它的原始模块。哦!但是,既然我已经这样做了,请将类型签名替换为:: BlockPtrs (EltRepr Word8)
:
Couldn't match type `BlockPtrs (EltRepr e0)' with `((), Ptr Word8)'
The type variable `e0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Expected type: BlockPtrs (EltRepr e0)
Actual type: BlockPtrs (EltRepr Word8)
编辑:由里德巴顿回答。它现在为我编译,谢谢!“最终”代码:
import AI.CV.CxCore
import AI.CV.HighGui
import Data.Array.Accelerate as A
import Data.Array.Accelerate.IO as A
import Data.Array.Accelerate.Array.Sugar (EltRepr)
main :: IO ()
main = do
capture <- cvCreateCameraCapture 0
frame <- cvQueryFrame capture
imgPtr <- cvGetImage frame
(arr :: Array (Z :. Int :. Int :. Int) Word8) <- A.fromPtr
(Z :. 480 :. 640 :. 3)
(((), imgPtr) :: BlockPtrs (EltRepr Word8))
return ()