Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在haskell opencv中为coreceMatM定义适当的类型_Opencv_Haskell_Image Processing - Fatal编程技术网

在haskell opencv中为coreceMatM定义适当的类型

在haskell opencv中为coreceMatM定义适当的类型,opencv,haskell,image-processing,Opencv,Haskell,Image Processing,我试着去理解图书馆 我为我的图像导入了orb检测: {-# LANGUAGE DataKinds #-} {-# LANGUAGE OverloadedStrings #-} import Control.Monad import Linear.V4 import Linear.V2 import OpenCV as CV import OpenCV.Internal.Mutable import qualified Data.ByteString as B main = do im

我试着去理解图书馆

我为我的图像导入了orb检测:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
import Control.Monad
import Linear.V4
import Linear.V2
import OpenCV as CV
import OpenCV.Internal.Mutable
import qualified Data.ByteString as B


main = do
    img <- imdecode ImreadColor <$> B.readFile "input.jpg"
    let orb = mkOrb defaultOrbParams
    let imgData = exceptError $ do
            (kpts, _descs) <- orbDetectAndCompute orb img Nothing
            let mi = matInfo img
                clr = (toScalar $ V4 (255::Double) 255 255 0)::(Scalar)
                shape = toShape $ miShape mi
                chan = toChannels $ miChannels mi
                depth = toDepth $ miDepth mi
            resImg <- withMatM shape chan depth clr $ \imgM -> do
                let img' = exceptError $ coerceMatM imgM
                    img'' = (unMut img'')::CV.Mat ('S ['D, 'D]) 'D 'D
                    img''' = Mut img''
                -- let img''' = (exceptError $ coerceMatM imgM)::(CV.Mut (CV.Mat ('S ['D, 'D]) 'D 'D) (PrimState (ST s)))
                void $ matCopyToM img''' (V2 0 0) img Nothing
                forM_ kpts $ \kpt -> do
                    let kptRec = keyPointAsRec kpt
                    circle img''' (round <$> kptPoint kptRec) 5 (V4 (255::Double) 0 0 255) 1 LineType_AA 0
                    return ()
            imencode OutputBmp resImg
    B.writeFile "output.bmp" imgData
我用
unMut
解包,然后打包回去

我试图指定
Mut(Mat…
)的类型(参见上面的注释行):

但我发誓:

src/exmpl.hs:29:60: error:
    • Couldn't match type ‘s’ with ‘s2’
      ‘s’ is a rigid type variable bound by
        a type expected by the context:
          forall s.
          Mut
            (Mat
               (ShapeT (Data.Vector.Vector GHC.Int.Int32))
               (ChannelsT GHC.Int.Int32)
               (DepthT Depth))
            (PrimState (ST s))
          -> CvExceptT (ST s) ()
        at src/exmpl.hs:23:27
      ‘s2’ is a rigid type variable bound by
        an expression type signature:
          forall s2. Mut (Mat ('S '['D, 'D]) 'D 'D) (PrimState (ST s2))
        at src/exmpl.hs:29:67
      Expected type: Mut (Mat 'D (ChannelsT GHC.Int.Int32) 'D) s2
        Actual type: Mut
                       (Mat
                          (ShapeT (Data.Vector.Vector GHC.Int.Int32))
                          (ChannelsT GHC.Int.Int32)
                          (DepthT Depth))
                       (PrimState (ST s))

那么,如何为
img''确定正确的类型?

我解决了这个问题。需要使用
pureException
和作用域类型变量:

(imgM''':: Mut (Mat (S [D, D]) D D) s) <- pureExcept $ coerceMatM imgM

(imgM''::Mut(Mat(S[D,D])dd)S我明白了。需要使用
pureException
和作用域类型变量:

(imgM''':: Mut (Mat (S [D, D]) D D) s) <- pureExcept $ coerceMatM imgM
(imgM''::Mut(Mat(S[D,D])D)S)
(imgM''':: Mut (Mat (S [D, D]) D D) s) <- pureExcept $ coerceMatM imgM
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Control.Monad
import Linear.V2
import Linear.V4
import OpenCV as CV
import qualified Data.ByteString as B


main = do
    img <- imdecode ImreadColor <$> B.readFile "input.jpg"
    let orb = mkOrb defaultOrbParams
    let imgData = exceptError $ do
            (kpts, _descs) <- orbDetectAndCompute orb img Nothing
            let mi = matInfo img
                clr = (toScalar $ V4 (255::Double) 255 255 0)::(Scalar)
                shape = toShape $ miShape mi
                chan = toChannels $ miChannels mi
                depth = toDepth $ miDepth mi
            resImg <- withMatM shape chan depth clr $ \imgM -> do
                (imgM':: Mut (Mat (S [D, D]) D D) s) <- pureExcept $ coerceMatM imgM
                void $ matCopyToM imgM' (V2 0 0) img Nothing
                forM_ kpts $ \kpt -> do
                    let kptRec = keyPointAsRec kpt
                    circle imgM' (round <$> kptPoint kptRec)
                                 5
                                 (V4 (255::Double) 0 0 255)
                                 1
                                 LineType_AA
                                 0
                    return ()
            imencode OutputBmp resImg
    B.writeFile "output.bmp" imgData