gpt4 book ai didi

image - 用随机颜色填充封闭区域 - Haskell - 星期五

转载 作者:行者123 更新时间:2023-12-01 13:47:01 24 4
gpt4 key购买 nike

我正在尝试执行不是很复杂的图像分析,以尝试找到不同的形状并计算它们的一些参数,如面积和周长(以像素为单位),我正在尝试在 Haskell 中执行此操作(我想这样做是为了尝试并使用函数式编程语言)。

第一个任务是计算图像上勺子的数量: Image of 6 spoons我正在使用 Friday用于处理图像的 Haskell 包。

我的想法是使用 Friday 的边缘检测,然后用它的填充函数填充所有封闭区域。第一个要求我遍历图像的像素,直到我偶然发现一个黑色像素。比我填充该区域并继续在图像中搜索(现在已经填充了其中一个对象)。我可以用随机颜色为不同的物体着色,并将这些颜色与它们的物体相关联,以找出它们的面积和周长。

以下是我对它应用边缘检测后该图像的外观: enter image description here

虽然我无法找到遍历所有像素的方法。我在以下包中找到了那些readreadLinear 函数: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

提前谢谢你。

最佳答案

How do I find area and perimeter of connected components?

您可以使用 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))

最终结果是:

RGB Spoons

关于image - 用随机颜色填充封闭区域 - Haskell - 星期五,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35244866/

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