【发布时间】:2016-05-16 15:40:24
【问题描述】:
我正在尝试执行不太复杂的图像分析,以尝试找到不同的形状并计算它们的一些参数,例如面积和周长(以像素为单位),我正在尝试在 Haskell 中执行此操作(我想这样做是为了尝试并使用函数式编程语言)。
第一个任务是计算图像上勺子的数量: 我正在使用 Friday Haskell 包来处理图像。
我的想法是使用周五的边缘检测,然后用它的填充功能填充所有封闭区域。第一个需要我迭代图像的像素,直到我偶然发现一个黑色像素。比我填充该区域并继续在图像中搜索(现在填充了其中一个对象)。我可以用随机颜色为不同的对象着色,并将这些颜色与其对象相关联,以找到它们的面积和周长。
我无法找到迭代所有像素的方法。 我在以下包中找到了那些 read 和 readLinear 函数:https://hackage.haskell.org/package/friday-0.2.2.0/docs/Vision-Image-Mutable.html#v:linearRead,但我不确定如何使用它们,我无法从它们的类型签名,因为我对 Haskell 非常陌生。
这是完成所有图像读取、灰度和边缘检测的代码:
{-# LANGUAGE ScopedTypeVariables #-}
import Prelude hiding (filter)
import System.Environment (getArgs)
import Vision.Detector.Edge (canny)
import Vision.Image
import Vision.Image.Storage.DevIL (Autodetect (..), load, save)
detectEdges :: RGBA -> Grey
detectEdges img =
let grey = convert img :: Grey
-- Img blurring --
blurRadius = 2
blurred = gaussianBlur blurRadius (Nothing :: Maybe Double) grey :: Grey
-- Sobel applying --
sobelRadius = 2
lowThreshold = 256
highThreshold = 1024
in (canny sobelRadius lowThreshold highThreshold blurred) :: Grey
processImg :: RGBA -> RGBA
processImg img =
let edges = detectEdges img
-- Here goes all of the important stuff
in convert edges :: RGBA
main :: IO ()
main = do
[input, output] <- getArgs
io <- load Autodetect input
case io of
Left err -> do
putStrLn "Unable to load the image:"
print err
Right (img :: RGBA) -> do
mErr <- save Autodetect output (processImg img)
case mErr of
Nothing ->
putStrLn "Success."
Just err -> do
putStrLn "Unable to save the image:"
print err
提前谢谢你。
【问题讨论】:
-
你会一直有这个背景,还是应该使用任意图像?不幸的是,我不知道 Haskell
-
很不幸,因为问题更多是关于 Haskell 的。
-
您能介绍一下您当前的读取、灰度和边缘检测的代码吗?与您的上一个问题一样,我想回答,但我太懒了,无法重新生成初始步骤。
-
我已按照您的要求添加了初始步骤。
标签: image haskell image-processing