我正在尝试执行不太复杂的图像分析,以尝试找到不同的形状并计算它们的一些参数,例如面积和周长(以像素为单位),我正在尝试在 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
先感谢您。
最佳答案
您可以使用 Vision.Image.Contour
中的轮廓跟踪来获取所有轮廓周长。首先让我们从获得像你这样的边缘开始:
{-# LANGUAGE ScopedTypeVariables #-}
import Prelude as P
import System.Environment (getArgs)
import Vision.Detector.Edge (canny)
import Vision.Image
import Vision.Primitive.Shape
import Vision.Image.Storage.DevIL (Autodetect (..), load, save)
import Vision.Image.Transform(floodFill)
import Control.Monad.ST (runST, ST)
import Vision.Image.Contour
-- Detects the edge of the image with the Canny's edge detector.
--
-- usage: ./canny input.png output.png
main :: IO ()
main = do
[input, output] <- getArgs
-- Loads the image. Automatically infers the format.
io <- load Autodetect input
case io of
Left err -> do
putStrLn "Unable to load the image:"
print err
Right (grey :: Grey) -> do
let blurred, edges :: Grey
edges = canny 2 256 1024 blurred :: Grey
这是我们获取轮廓的地方。由于我稍后使用的 draw 函数中的错误,我将首先模糊以获得具有不同内点和外点的轮廓。这最终会得到修补......
cs = contours (blur 2 edges :: Grey)
goodContours = P.filter goodSize (allContourIds cs)
现在我们有一个
Contours
类型的值,它包括每个连接组件的有效 ContourId
。对于每个 ContourId
,您可以使用 contourSize
获取其面积,使用 contourPerimeter
获取周长。周长的大小就是周长点列表的长度。我只是做了一个非常定制的过滤器,称为
goodSize
来获取勺子,但是您可以随意使用面积和周长: goodSize x = let ((xmin,xmax),(ymin,ymax)) = contourBox cs x
in xmax-xmin > 60 && xmax-xmin < 500 &&
ymax-ymin > 100 && ymax-ymin < 500
final, filledContours :: RGBA
filledContours =
convert $ drawContours cs (shape edges) Fill goodContours
或者,对于每个轮廓,使用 floodFill 来获取颜色。在这里,我只使用三种颜色并填充列表中第一个的轮廓。轮廓列表是从上到下从左到右排序的,所以这看起来很奇怪。您可以
sortBy xmin goodContours
获得左右排序。 floodStart = concatMap (take 1 . contourPerimeter cs) goodContours
colors = cycle [RGBAPixel 255 0 0 255, RGBAPixel 0 255 0 255, RGBAPixel 0 0 255 255]
final = runST doFill
填充操作使用的是
ST
monad,你可以在 StackOverflow 上找到很多关于这里的问题。 doFill :: forall s. ST s RGBA
doFill = do
m <- thaw filledContours :: ST s (MutableManifest RGBAPixel s)
mapM_ (\(p,c) -> floodFill p c m) (zip floodStart colors)
return =<< unsafeFreeze m
-- Saves the edges image. Automatically infers the output format.
mErr <- save Autodetect output final
case mErr of
Nothing ->
putStrLn "Success."
Just err -> do
putStrLn "Unable to save the image:"
print err
contourBox cs x =
let ps = contourPerimeter cs x
(xs,ys) = unzip $ P.map (\(Z :. x :. y) -> (x,y)) ps
in ((minimum xs, maximum xs), (minimum ys, maximum ys))
最终结果是:
关于image - 用随机颜色填充封闭区域 - Haskell - Friday,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35244866/