gpt4 book ai didi

opencv - haskell-opencv 中的 GaussianBlurImage(Haskell 绑定(bind)到 OpenCV-3.1)

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

啊!

我正在使用 haskell-opencv图书馆。我不知道是否有其他人在使用它,或者对它有所了解。

我尝试使用 gaussianBlurImage,但出现了一个我无法识别的错误。

我有这个:

cropped image = do 
resized <- resizeImage image
gaussianBlurred <- gaussianBlurImage ((M.unsafeCoerceMat . getImageFromEither) resized)

resizedEither CV.Exception (M.Mat shape channels depth) 我从这里得到 ((M.unsafeCoerceMat . getImageFromEither) resized ) 一个 Mat shape (S channels) (S depth) as gaussianBlurImage function needs.

我定义的 gaussianBlurImage 是这样的:

gaussianBlurImage image = runExceptT $ CV.pureExcept $ CV.gaussianBlur (V2 13 13::V2 Int32) 0 0 图像

对我来说,它看起来不错。与此处类似:blur .但是我得到这个错误:

Couldn't match expected type ‘'True’ with actual type ‘Elem depth0 '[Word8, Word16, Float, Double]’

我注意到,我的 gaussianBlurImage 有这种类型:

gaussianBlurImage :: (M.Mat shape0 ('S channels0) ('S depth0)) -> Either CV.CvException (Either CV.CvException (M.Mat shape0 ('S channels0) ('S depth0)))

我喜欢这个:

gaussianBlurImage :: (M.Mat shape0 ('S channels0) ('S depth0)) -> Either CV.CvException (M.Mat shape0 ('S channels0) ('S depth0))

也许它有什么作用。

我用 medianBlur、blur 和 gaussianBlur 试过了。我使用了一个未调整大小的图像,一个具有三个 channel 的图像和另一个具有 2 个 channel (颜色和灰色)的图像,但我总是遇到相同的错误。我想知道错误是否在 M.unsafeCoerceMat image 中。为什么 'True 是预期的类型?我不知道

最佳答案

haskell-opencv是一个优秀的库,但有时很难使用,因为有很多文档,但很难理解,而且没有太多示例。

在那里,每个模糊函数都需要一个(M.Mat shape ('S channels) ('S depth))矩阵,但矩阵通常是这样的:(M.Mat shape channel 深度),其中 '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))

调整图像大小:

{-# 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

模糊图像:

{-# 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

就是这样。 :) 我希望它可以帮助别人。

关于opencv - haskell-opencv 中的 GaussianBlurImage(Haskell 绑定(bind)到 OpenCV-3.1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39661287/

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