关于C#:从haskell调用一个C opencv函数

Calling a C opencv function from haskell

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

现在我正在这样做:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{# 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

我从 Haskell 阅读了这篇 CPlusPlus 帖子,因此我得到了名称 _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 烹饪书但我无法在那里得到我的问题的答案。

有什么想法吗?


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

这是一个使用示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 我添加了这个:

1
2
3
4
5
packages:    
  location:
    git: https://github.com/LumiGuide/haskell-opencv.git
    commit: 07afde39fa16f7a4282d4a812e70b9ed33d0f3ef
  ‘.’

因此看起来是 cabal 文件的一部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
library  
 hssourcedirs:      src
  exposedmodules:     Lib
  builddepends:       base >= 4.7 && < 5
                     , opencv
                     , bytestring
  defaultlanguage:    Haskell2010

 executable simpleexe
  hssourcedirs:      app
  mainis:             Main.hs
  ghcoptions:         threaded rtsopts withrtsopts=N
  builddepends:       base
                     , simple
                     , opencv
                     , bytestring
  defaultlanguage:    Haskell2010

 testsuite simpletest
  type:                exitcodestdio1.0
  hssourcedirs:      test
  mainis:             Spec.hs
  builddepends:       base
                     , simple
                     , opencv
                     , bytestring
  ghcoptions:         threaded rtsopts withrtsopts=N
  defaultlanguage:    Haskell2010

我希望它可以帮助某人。


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/268884.html

(0)
上一篇 2022年6月20日 12:11
下一篇 2022年6月20日 12:12

相关推荐

发表回复

登录后才能评论