haskell-opencv是一個很好的庫,但有時很難使用,因爲有很多文檔,但很難理解,並且沒有很多示例。
在那裏,每個模糊功能需要一個(M.Mat shape ('S channels) ('S depth))
矩陣,但矩陣通常是這樣的:(M.Mat shape channels depth)
,其中'S
告訴它是靜態的。
我用這個函數:
M.coerceMat
有這樣一個矩陣。
coerceMAt :: (ToShapeDS (Proxy shapeOut), ToChannelsDS (Proxy
channelsOut), ToDepthDS (Proxy depthOut))
=> Mat shapeIn channelsIn depthIn
-> CvExcept (Mat shapeOut channelsOut depthOut)
因爲它返回CVExcept
,我用exceptError :: CvExcept a -> a
得到公正的形象。
但最重要的是函數的定義。我的代碼中存在這個問題。 depth
不能含糊,這就是爲什麼我說:
forall height0 width0 channels depth . (depth `In` '[Word8, Word16, Float, Double] , channels `In` '[1, 3, 4]) => M.Mat ('S '[height0, width0]) ('S channels) ('S depth) -> IO (M.Mat ('S '[height0, width0]) ('S channels) ('S depth))
這裏有一個完整的例子。我得到一張圖片,調整它的大小,模糊它並顯示出來。
主要:
module Main where
import Lib
import qualified OpenCV.Internal.Core.Types.Mat as M
import Control.Monad (void)
import qualified OpenCV as CV
import qualified Data.ByteString as B
main :: IO()
main = do
test <- controller
CV.withWindow "test" $ \window -> do
CV.imshow window test
void $ CV.waitKey 10000
庫:
{-# LANGUAGE TypeFamilies #-}
module Lib
(controller
) where
import BlurImage
import ResizeImage
import Utils
import Control.Monad (void)
import Data.Word
import qualified OpenCV.Internal.Core.Types.Mat as M
import qualified OpenCV as CV
import qualified Data.ByteString as B
controller :: IO (CV.Mat (CV.S '[CV.D, CV.D]) (CV.S 1) (CV.S Word8))
controller = do
file <- B.readFile "path/to/image.jpg"
img <- return $ CV.imdecode CV.ImreadGrayscale file
resized_little_img <- resizeImage img --little image for making a blur in and find the receipt
blurImage ((CV.exceptError $ M.coerceMat resized_little_img) :: M.Mat (CV.S '[ CV.D, CV.D]) (CV.S 1) (CV.S Word8))
ResizeImage:
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
module ResizeImage
(
resizeImage
) where
import Utils
import Control.Monad (void)
import Control.Monad.Except
import Data.Functor.Identity
import Data.Word
import Data.Proxy
import qualified OpenCV as CV
import Linear.V2
import OpenCV.TypeLevel
import qualified OpenCV.Internal.Core.Types.Mat as M
import qualified OpenCV.Core.Types.Size as S
import qualified OpenCV.ImgProc.GeometricImgTransform as GIT
import GHC.Int (Int32)
resizingImage :: (M.Mat (CV.S [CV.D, CV.D]) CV.D CV.D) -> CV.CvExcept (M.Mat (CV.S [CV.D, CV.D]) CV.D CV.D)
resizingImage image = GIT.resize (GIT.ResizeAbs $ S.toSize $ (getSize w h Nothing (Just 500))) CV.InterCubic image
where
[h, w] = getHandW image
resizeImage :: (M.Mat (S '[CV.D, CV.D]) CV.D CV.D) -> IO(M.Mat (CV.S [CV.D, CV.D]) CV.D CV.D)
resizeImage image = do
resized <- return $ resizingImage image
return $ CV.exceptError $ resized
BlurImage:
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
module BlurImage
(
blurImage
) where
import Utils
import Control.Monad (void)
import Control.Monad.Except
import qualified Data.ByteString as B
import Data.Word
import Data.Proxy
import qualified OpenCV as CV
import Linear.V2
import OpenCV.TypeLevel
import qualified OpenCV.Internal.Core.Types.Mat as M
import qualified OpenCV.Core.Types.Size as S
import qualified OpenCV.ImgProc.GeometricImgTransform as GIT
import GHC.Int (Int32)
medianBlurImage :: (depth `In` '[Word8, Word16, Float], channels `In` '[1, 3, 4]) => (M.Mat shape ('S channels) ('S depth)) -> CV.CvExcept (M.Mat shape ('S channels) ('S depth))
medianBlurImage image = CV.medianBlur image 13
gaussianBlurImage :: (depth `In` '[Word8, Word16, Float, Double], channels `In` '[1, 3, 4]) => (M.Mat shape ('S channels) ('S depth)) -> CV.CvExcept (M.Mat shape ('S channels) ('S depth))
gaussianBlurImage image = CV.gaussianBlur (V2 13 13 :: V2 Int32) 0 0 image
blurImage :: forall height0 width0 channels depth . (depth `In` '[Word8, Word16, Float, Double] , channels `In` '[1, 3, 4]) => M.Mat ('S '[height0, width0]) ('S channels) ('S depth) -> IO (M.Mat ('S '[height0, width0]) ('S channels) ('S depth))
blurImage image = do
gaussianBlurred <- return $ gaussianBlurImage image
return $ CV.exceptError $ gaussianBlurred
就是這樣。 :)我希望它可以幫助別人。
你能提供一個最小的完整例子嗎?你提供的片段不是任何人可以粘貼到文件中並嘗試編譯自己的東西。 –
這裏有一個要點:[gist](https://gist.github.com/Chuck-Aguilar/2a4a0506c2924c4d72134a45dc19935c) 我看到問題是與「深度」的類型,並補充說:'(深度'In' '[Word8,Word16,Float],頻道'In''[1,3,4])=>'。但是,它仍然無法正常工作。我得到這個錯誤:'無法匹配類型'IO'與'任一CV.CvException'預期類型:CV.CvException()實際類型:IO()'這很奇怪,因爲我應該能夠顯示圖片。 –