我试图执行不是非常复杂的图像分析来尝试找到不同的形状并计算它们的一些参数,如面积和周长(以像素为单位),我试图在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
提前谢谢。
答案 0 :(得分:2)
如何查找连接组件的面积和周长?
您可以使用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
我们在这里获取轮廓。由于我稍后使用的绘图功能中的一个错误,我将首先模糊以获得具有不同内部和外部点的轮廓。这最终会得到补丁......
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))
最终结果是:
答案 1 :(得分:1)
我在你的帖子中没有看到问题,最近似乎是:
我无法找到迭代所有像素的方法。
你可以使用map
迭代所有像素,但是......那只是一张地图,你将没有任何状态(毕竟它不是一个折叠)。因此,您可能希望创建自己的原始递归函数,并使用与index
相同的!
索引每个像素。
你也说过:
我的想法是使用星期五的边缘检测,然后用它的填充函数填充所有封闭区域。
如果您填写“所有已完成的区域”,那么您的整个图片将 white 填充外部轮廓取决于填充策略的任何内容 - 请注意包含整个图像的较大矩形。我建议你做一个轮廓痕迹并过滤一些简单的东西,如周长,高度,宽度或比例。我的轮廓模块还没有被推到hackage但你可以从github获得它。请注意,drawContour
函数在正常运行后应该对您有用(部分原因是我不会将其推送到Hackage)。