OpenCV:将曲线转换为Shapely Linestrings

huangapple go评论75阅读模式
英文:

Opencv : convert curves to shapely Linestrings

问题

我有一张预处理过的图像,其中包含代表曲线的白色像素串。有些是闭合的,有些不是。线条的厚度为1像素。

我想能够将每一个转换成一个Shapely Linestring。

我已经尝试使用“findContours”函数。这在闭合曲线上效果非常好,但在开放曲线上,OpenCV返回一个闭合轮廓。

这会在我的后续代码中出现问题,例如,当我需要获取这些曲线的长度时。(例如,它大致返回每个曲线的两倍长度)

请注意,我绝对需要Linestrings。我不能简单地计算图像中白色像素的数量。

是否有一种简单的方法可以使用OpenCV提取这些线条,还是我必须自己编写算法(我真的不想采用这种解决方案,因为我没有太多时间来做,而且性能对这个应用程序来说非常重要)

示例图像:
OpenCV:将曲线转换为Shapely Linestrings

英文:

I have a pre-processed image containing strands of white pixels representing curves. Some closed, some not closed. The thickness is 1 pixel

I would like to be able to convert each one of these to a shapely Linestring.

I already tried to use the "findContours" function. This works very well on the closed curves, however on the open ones, opencv returns a closed contour.

This breaks my code later when, for instance, i need to get the length of these curves. (For instance, it roughly returns twice the length of each curve)

Note that i absolutely need linestrings. I can not simply count the number of white pixels in the image.

Is there a simple way to extract these lines using opencv or i will have to write the algorithm myself (i'd really like to avoid this solution as i dont have much time to do it and performance is kind of important for this app)

example image:
OpenCV:将曲线转换为Shapely Linestrings

答案1

得分: 1

我对 shapely 不熟悉,但你似乎想要获得某种矢量表示,因此以下可能会有所帮助。这对于一条评论来说有点长。

你可以使用 potrace 将位图数据转换为矢量格式。它接受 PNM/PGM/PBM/PPM 或 BMP 格式的输入,所以我正在使用 ImageMagick 将你的 PNG 转换为 PGM,然后将其输入到 potrace 中。我正在裁剪图像左上角的形状,这样你就可以看到单个轮廓的结果:

magick 6JVH1.png -crop 176x200+0+0 pgm: | potrace - --svg > a.svg

这是在图像左上角从 0,0 (顶部左侧) 开始裁剪出一个 176px 宽、200px 高的矩形,并将其制作成 PGM,然后将其传送到 potrace 并选择 SVG 后端,我假设你可以将其转换成 shapely 线串。

结果如下:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
 "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
 width="176.000000pt" height="200.000000pt" viewBox="0 0 176.000000 200.000000"
 preserveAspectRatio="xMidYMid meet">
<metadata>
Created by potrace 1.16, written by Peter Selinger 2001-2019
</metadata>
<g transform="translate(0.000000,200.000000) scale(0.100000,-0.100000)"
fill="#000000" stroke="none">
<path d="M0 1000 l0 -1000 880 0 880 0 0 1000 0 1000 -880 0 -880 0 0 -1000z
 m1150 -165 c6 -71 18 -192 25 -269 31 -288 15 -338 -82 -267 -23 16 -101 86
 -174 155 -158 148 -357 341 -285 275 28 -25 114 -105 191 -179 251 -239 324
 -294 346 -259 14 22 11 119 -11 329 -28 283 -40 499 -38 715 1 186 1 184 9
 -90 4 -154 12 -338 19 -410z"/>
</g>
</svg>
英文:

I'm not familiar with shapely but you seem to want to get some kind of vector representation, so the following may help. It is a bit long for a comment.

You can use potrace to convert bitmap data to vector format. It accepts input in PNM/PGM/PBM/PPM or BMP format, so I am using ImageMagick to convert your PNG into PGM and then pump it into potrace. I am chopping out the shape at the top-left of your image so you can see the results for a single contour:

magick 6JVH1.png -crop 176x200+0+0 pgm: | potrace - --svg &gt; a.svg

That is cropping out a 176px wide by 200px tall rectangle starting at 0,0 (top-left) and making it into a PGM, then piping into potrace and selecting the SVG backend which I assume is something you can make into a shapely line-string.

The result is this:

&lt;?xml version=&quot;1.0&quot; standalone=&quot;no&quot;?&gt;
&lt;!DOCTYPE svg PUBLIC &quot;-//W3C//DTD SVG 20010904//EN&quot;
 &quot;http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd&quot;&gt;
&lt;svg version=&quot;1.0&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;
 width=&quot;176.000000pt&quot; height=&quot;200.000000pt&quot; viewBox=&quot;0 0 176.000000 200.000000&quot;
 preserveAspectRatio=&quot;xMidYMid meet&quot;&gt;
&lt;metadata&gt;
Created by potrace 1.16, written by Peter Selinger 2001-2019
&lt;/metadata&gt;
&lt;g transform=&quot;translate(0.000000,200.000000) scale(0.100000,-0.100000)&quot;
fill=&quot;#000000&quot; stroke=&quot;none&quot;&gt;
&lt;path d=&quot;M0 1000 l0 -1000 880 0 880 0 0 1000 0 1000 -880 0 -880 0 0 -1000z
m1150 -165 c6 -71 18 -192 25 -269 31 -288 15 -338 -82 -267 -23 16 -101 86
-174 155 -158 148 -357 341 -285 275 28 -25 114 -105 191 -179 251 -239 324
-294 346 -259 14 22 11 119 -11 329 -28 283 -40 499 -38 715 1 186 1 184 9
-90 4 -154 12 -338 19 -410z&quot;/&gt;
&lt;/g&gt;
&lt;/svg&gt;

huangapple
  • 本文由 发表于 2023年6月22日 17:22:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定