从图像(地图)中提取多边形坐标 [英] Extract polygon coordinates from image (map)

查看:576
本文介绍了从图像(地图)中提取多边形坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下地图:

我想提取多边形坐标(像素),我正在使用以下代码段,但是内标的图像全为0(否):

I want to extract the polygon coordinates (pixls), I am using the following code snipt, but the inteverted labeled image is all 0's (False):

import numpy as np
from skimage import io, measure, morphology
from skimage.io import imsave, imread

img = io.imread('map.png', as_gray=True)
imsave("test.png", img)

img = morphology.binary_dilation(img, selem=np.ones((5,5)))

img_inverted = np.invert(img)
img_inverted_labeled = measure.label(img_inverted)

n_lbls = np.unique(img_inverted_labeled)[1:]

pols = []
for i in n_lbls:
  img_part = (img_inverted_labeled == i)
  pols.append(measure.find_contours(img_part, level=0)[0])

反转的图像如下:

我相信,该行的探针位于selem的值中:

I belive the probem is in the value of the selem in this line:

img = morphology.binary_dilation(img, selem=np.ones((5,5)))

能否请您告知此代码有什么问题.

Could you please advise what is the problem in this code..

编辑 反转图像(灰度)时的唯一值:

EDIT The unique values if the inverted image (grayscaled):

[235, 227, 219, 212, 204, 230, 215, 199, 207, 188, 184, 172, 176, 196, 192, 179, 223, 211, 203, 173, 191, 228, 216, 232, 200, 208, 171, 183, 175, 180, 195, 236, 221, 234, 233, 226, 220]

我认为我需要根据某个阈值将这些值分为两类(白色/黑色).您能否确认我的发现?如果可以,我该如何计算该值?

I think I need to classify these value into two categories (white/black) based on some threshold value. Could you please confirm my finding, and if it is so how can I calculate this value?

推荐答案

是的,这里的阈值是可行的.查看图像0.7的最小值和最大值似乎是合理的:

Yes a threshold here would work. Having a look at the minimum and maximum values of the image 0.7 seems reasonable:

import numpy as np
from skimage import io, measure, morphology
from skimage.io import imsave, imread
from matplotlib import pyplot as plt

img = io.imread('map.png', as_gray=True)
# do thresholding
mask = img < 0.7

plt.matshow(mask, cmap='gray')

# ij coords of perimeter
coords = np.nonzero(mask)
coords
>>> (array([ 61,  61,  61, ..., 428, 428, 428]),
     array([200, 201, 202, ..., 293, 294, 295]))

如果您只想要边界线而不是面积(因为它具有宽度),则可以执行以下操作:

And if you just want the perimeter line rather than area (as it has a width) then you could do:

from skimage.morphology import skeletonize

fig, ax = plt.subplots(dpi=150)
ax.matshow(skeletonize(mask), cmap='gray')

这篇关于从图像(地图)中提取多边形坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆