我正在尝试使用fromPtr
从OpenCV中获取图像,并将其放入加速数组。这个特性的文档是钝的,而且这个例子不会编译(我不能安装加速-因为标准的例子)。此代码:
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
和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 ()
发布于 2014-02-24 21:52:25
fromPtr :: (Shape sh, Elt e) => sh -> BlockPtrs (EltRepr e) -> IO (Array sh e)
GHC需要知道要使用e
的哪个类型来提供Elt e
实例。
显然,EltRepr
是一个类型家族/关联类型,因此EltRepr e
不确定e
。(没有理由不能有两种类型的e1
和e2
,对于这两种类型,EltRepr e1
和EltRepr e2
是相同的。)因此,GHC永远不能从fromPtr
的参数类型中得出要使用的e
类型。
Array
是一个普通的类型构造函数,所以Array sh e
确实决定了e
。因此,您应该将类型归属放在arr
上。
(arr :: Array (Z :. Int :. Int) Word8) <- A.fromPtr (Z :. 480 :. 640) ((), castPtr imgPtr)
(这将需要一些扩展,GHC将告诉您有关的情况。)或者,如果您实际使用arr
的方式确定其元素的类型为Word8
,则不需要这种类型归属。
https://stackoverflow.com/questions/21977729
复制相似问题