【问题标题】:Polygonization of disjoint segments不相交段的多边形化
【发布时间】:2021-06-15 14:36:43
【问题描述】:

问题如下:我有一个 png 文件: example.png

  • 我使用 skimage.segmentation.chan_vese 的 chan vese 过滤

    • 它返回一个黑白的 png 文件。
  • 我用cv2.ximgproc.createFastLineDetector()检测我的新png文件周围的片段

    • 它返回一个列表一个段

但段列表表示不相交的段。

我使用两种简单的方法来多边形化这个段列表:

-似乎cv2.ximgproc.createFastLineDetector() 创建了一个几乎连续的列表,所以我只是通过创建新段来加入:

import import skimage.segmentation as seg
import  skimage.filters as filters 
from PIL import Image, ImageDraw, ImageOps
import numpy as np
from skimage import data, img_as_float
import cv2 as cv2

points =Image.open(png_file)
points = ImageOps.grayscale(points)
points = np.asarray(points)
image_gray = points
points = img_as_float(points)
points = seg.chan_vese(points, mu=0.01, lambda1=2, lambda2=1, tol=1e-10, max_iter=2000,init_level_set="checkerboard")
print("seg done")
Image.fromarray(points).save("chan_vese.png")

输出是output.png

points =Image.open("chan_vese.png")
points = ImageOps.grayscale(points)
points = np.asarray(points)
#Create default parametrization LSD
lsd = cv2.ximgproc.createFastLineDetector()
#Detect lines in the image
lines = lsd.detect(points)
#Draw detected lines in the image
drawn_img = lsd.drawSegments(image_gray,lines)
#Show image
Image.fromarray(drawn_img).save(output_png) 
#we got the segments now, but they are disjoints 

我得到了这张带有片段的图片,以便有一个想法:output_segments.png

list_segment = []
for segment in self.lines:
     list_segment.append(segment[0])

new_lines = []
for i, segment in enumerate(list_segment):
     if i < len(list_segment)-1:
            new_lines.append([[list_segment[i][2], list_segment[i][3], list_segment[i+1][0], list_segment[i+1][1]]])
     else: 
            new_lines.append([[list_segment[i][2], list_segment[i][3], list_segment[0][2], list_segment[0][3]]])

但列表不是连续的,所以我得到了一些类似的人工制品 我得到了这个输出:output.png

  • 第二个是取norm2中最接近的一个:
        for i, segment in enumerate(list_segment):
            new_lines.append([segment])
            dist_min = float("inf")
            for j , segment2 in enumerate(list_segment):
                if np.array_equal(segment, segment2):
                    distance1 = calcul_distance_points(segment[2], segment[3], segment2[0], segment2[1])
                    if distance1 < dist_min:
                        dist_min = distance1
                        new_lines.append(([[segment[0], segment[1], list_segment[j][0], list_segment[j][1]]]))

这个方法的问题是我可以跳边。

我发现这个paper 对我的问题做出了友好的回应,但我正在寻找一种简单的方法来解决。

您有什么建议这样做,并避免这种潜在的跳跃吗?

PS:这个算法的目的是最后用这个多边形创建一个蒙版。

【问题讨论】:

  • 请分享您的输入图像以及您的中间和最终结果,以便我们了解您所指的内容。此外,请尝试确保您的代码可运行 - 使用 import 语句等。
  • 我编辑了新东西,希望这能提高您对我的问题的理解!

标签: python image-processing geometry


【解决方案1】:

所以我使用另一个库来解决这个问题:OpenCV-python

我们还检测到了段(它们不是不相交的),但是具有函数findContours 的层次结构。层次结构很有用,因为该函数检测不同的多边形。这意味着我们可以使用其他方法(如帖子中的解释)进行连接没有问题

【讨论】:

  • 我认为如果你想创建一个具有不相交段的多边形,你需要实现论文(帖子中的链接)。如果您出于求知欲而这样做,我会对此非常感兴趣
猜你喜欢
  • 1970-01-01
  • 2013-10-16
  • 2012-01-15
  • 2013-07-23
  • 1970-01-01
  • 2016-11-18
  • 2017-07-05
  • 1970-01-01
  • 2015-12-15
相关资源
最近更新 更多