gpt4 book ai didi

c++ - 从 haskell 调用 C opencv 函数

转载 作者:太空宇宙 更新时间:2023-11-03 22:25:49 34 4
gpt4 key购买 nike

我正在尝试将 OpenCV 与 Haskell 结合使用。我的想法是从 Haskell 调用 c++ 函数。

现在我正在这样做:

{-# LANGUAGE ForeignFunctionInterface #-}

module Lib
(
someFunc
) where

import Foreign.C
import Foreign.C.String
import Foreign.C.Types
import Foreign.Ptr

data LplROI = LplROI {
coi :: CInt,
xOffset :: CInt,
yOffset :: CInt
}

data LpImage = LpImage {
align :: CInt,
alphaChannel :: CInt,
borderConst :: CInt,
borderMode :: CInt,
channelSeq :: CChar,
colorModel :: CChar,
dataOrder :: CInt,
depth :: CInt,
height :: CInt,
id :: CInt,
imageData :: CChar,
mageDataOrigin :: CChar,
imageId :: CChar,
imageSize :: CInt,
maskROI :: LpImage,
nChannels :: CInt,
nSize :: CInt,
origin :: CInt,
roi :: LplROI,
tileInfo :: CChar,
width :: CInt,
widthStep :: CInt
}

foreign import ccall "_ZN2cv6imreadERKNS_6StringEi" imRead_ImRead :: CString -> CInt -> IO (Ptr LpImage)

someFunc = do
filename <- newCString "/home/chuck/Pictures/such-a-bad-day.jpg"
imRead_ImRead filename 1

我读了这篇文章 CPlusPlus from Haskell ,所以我得到了名称 _ZN2cv6imreadERKNS_6StringEi。在 2.1.1 中查找损坏的命名

但是 GHCi 说:

ByteCodeLink: can't find label During interactive linking, GHCi couldn't find the following symbol: _ZN2cv6imreadERKNS_6StringEi

在 Python 中我应该“导入 cv2”,但我不知道它在 Haskell 上是如何工作的。

我还阅读了:FFI cook book但我无法在那里得到我的问题的答案。

有什么想法吗?

最佳答案

我尝试的方法不起作用。OpenCV api 是用 C++ 编写的。这就是为什么调用它的方式在这里:C++ from haskell .但是当你在“/usr/lib/”中安装 OpenCV 时,你不会得到像 cv2.so 这样的 .so 文件。这意味着使用 foreign import ccall 非常困难,因为未构建 c 名称。所以,我使用了@ReidBarton 所说的“haskell-opencv”并且效果很好。或者至少比其他方式更容易。

这是一个使用示例:

module Lib
( someFunc
) where

import Control.Monad ( void )
import qualified OpenCV as CV
import qualified Data.ByteString as B

someFunc :: IO ()
someFunc = do
img <- CV.imdecode CV.ImreadUnchanged <$> B.readFile "/some_pic.jpg"
CV.withWindow "test" $ \window -> do
CV.imshow window img
void $ CV.waitKey 10000

stack.yaml 中我添加了这个:

packages:    
- location:
git: https://github.com/LumiGuide/haskell-opencv.git
commit: 07afde39fa16f7a4282d4a812e70b9ed33d0f3ef
- '.'

所以看起来是 cabal 文件的一部分:

library  
hs-source-dirs: src
exposed-modules: Lib
build-depends: base >= 4.7 && < 5
, opencv
, bytestring
default-language: Haskell2010

executable simple-exe
hs-source-dirs: app
main-is: Main.hs
-- ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends: base
, simple
, opencv
, bytestring
default-language: Haskell2010

test-suite simple-test
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Spec.hs
build-depends: base
, simple
, opencv
, bytestring
-- ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010

希望对大家有所帮助。

关于c++ - 从 haskell 调用 C opencv 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39505855/

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