2

我正在尝试使用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.

查看BlockPtrsand的定义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 ()
4

1 回答 1

2
fromPtr :: (Shape sh, Elt e) => sh -> BlockPtrs (EltRepr e) -> IO (Array sh e)

GHC 需要知道e您要使用哪种类型fromPtr来提供Elt e实例。

显然EltRepr是类型族/关联类型,所以EltRepr e不确定e。(没有理由为什么不能有两种类型,e1并且它们是相同的类型。)所以 GHC 永远无法从参数的类型中得出使用哪种类型的结论。e2EltRepr e1EltRepr e2fromPtre

Array是一个普通的类型构造函数,因此Array sh e确定e. 因此,您应该arr改为使用类型归属。

  (arr :: Array (Z :. Int :. Int) Word8) <- A.fromPtr (Z :. 480 :. 640) ((), castPtr imgPtr)

(这将需要一些扩展,GHC 会让您知道。)或者,如果您实际使用arr的方式确定其元素的类型为Word8,则不需要此类型归属。

于 2014-02-24T21:52:25.260 回答