我和我的团队最近使用 Mask-R-CNN 模型来利用卫星图像来预测农田的空间范围。该过程涉及将卫星图像分割成 512 X 512 像素芯片,从中预测场,然后将每个图像的多边形连接回去。我们发现模型没有在 512X512 芯片的边缘进行预测,这在图像芯片的边缘留下了没有数据的区域:
合并的数据集最终看起来像这样:
我想提出已分离的多边形以进行合并操作,提出了以下修复:
该功能在大多数情况下都是成功的。在相当多的情况下,它只合并一部分多边形

这是可以接受的,因为它仍然合并成一个多边形并代表多边形的大部分区域,但并不理想。在少数情况下,其中一些多边形仍未合并
我想知道这里是否有人知道我应该如何修改我的代码以捕获其中一些混合多边形并合并它们,甚至修复部分合并的多边形。请让我知道你的想法。太感谢了!这是代码:
import os
import pandas as pd
import geopandas as gpd
import rasterio
from shapely.geometry import Polygon
from collections import Counter
from pathlib import Path
def stitchshp(img, polys, endpath):
img = rasterio.open(img)
polys = gpd.read_file(polys)
#Making Fishnets from the image
firsteast = img.bounds.left
firstnorth = img.bounds.top
easting = [img.bounds.left]
northing = [img.bounds.top]
def funeast(image):
while image < img.bounds.right:
image = image +(0.5*512)
easting.append(image)
return funeast(image)
def funnorth(image):
while image > img.bounds.bottom:
image = image -(0.5*512)
northing.append(image)
return funnorth(image)
funeast(firsteast)
funnorth(firstnorth)
geom = []
for i in range(len(northing)):
for j in range(len(easting)):
coords = [(easting[j], northing[i]), (easting[j]+(0.5*512), northing[i]), (easting[j]+(0.5*512), northing[i]-(0.5*512)), (easting[j], northing[i]-(0.5*512))]
geom.append(Polygon(coords))
table = gpd.GeoDataFrame({"geometry": geom}, crs ={'init' :'epsg:32644'} )
#using fishnets to subset manipulate, and stitch polygons
allpolyslist = []
for fish in range(len(table.geometry)):
net = table.geometry[fish]
east = max([i[0] for i in [*net.exterior.coords]])
west = min([i[0] for i in [*net.exterior.coords]])
north = max([i[1] for i in [*net.exterior.coords]])
south = min([i[1] for i in [*net.exterior.coords]])
subpolys = polys[polys.intersects(net)]
net = gpd.GeoSeries(net)
maxeast = []
mineast = []
maxnorth = []
minnorth = []
for sequ in [[*i.exterior.coords] for i in subpolys.geometry]:
maxeast.append(max(i[0] for i in sequ))
mineast.append(min(i[0] for i in sequ))
maxnorth.append(max(i[1] for i in sequ))
minnorth.append(min(i[1] for i in sequ))
eastdict = Counter(maxeast)
eastchange = [key for key, value in eastdict.items() if key >east-2 and key <east+2 and value>1]
westdict = Counter(mineast)
westchange = [key for key, value in westdict.items() if key <west+2 and key >west-2 and value>1]
northdict = Counter(maxnorth)
northchange = [key for key, value in northdict.items() if key >north-2 and key <north+2 and value>1]
southdict = Counter(minnorth)
southchange = [key for key, value in southdict.items() if key <south+2 and key >south-2 and value>1]
polylists = [[*i.exterior.coords] for i in subpolys.geometry]
properpolys = []
for poly in polylists:
if len(poly)>6:
properpolys.append(poly)
for poly in properpolys:
for j in range(len(poly)):
if poly[j][0] in eastchange:
poly[j] = (east, poly[j][1])
if poly[j][0] in westchange:
poly[j] = (west, poly[j][1])
if poly[j][1] in northchange:
poly[j] = (poly[j][0], north)
if poly[j][1] in southchange:
poly[j] = (poly[j][0], south)
allpolyslist = allpolyslist + properpolys
print("ok, iterated thru!")
#gpd.GeoSeries([Polygon(i) for i in allpolyslist]).plot()
allpolylist = [Polygon(i) for i in allpolyslist]
print("converted to polygons!")
allpolydict = gpd.GeoDataFrame({"geometry": allpolylist}, crs = {'init' :'epsg:32644'})
merge = allpolydict.unary_union
merge = gpd.GeoSeries(merge)
merge = merge.explode()
print("exploded polys!")
united = gpd.GeoDataFrame(geometry = merge)
print("Made final df!")
united.to_file(endpath, crs = allpolydict.crs)
我还使用神经网络来检测正射图像上的内容,并为其开发了一些软件:orthoseg。为了避免图像边缘的检测质量较差,我在稍大的重叠图块上运行检测,并丢弃额外/重叠的像素。这可确保一致的检测质量,并避免您遇到的问题。