Stitching Mask R CNN 预测由图像芯片分隔的地理空间多边形

问题描述 投票:0回答:1

我和我的团队最近使用 Mask-R-CNN 模型来利用卫星图像来预测农田的空间范围。该过程涉及将卫星图像分割成 512 X 512 像素芯片,从中预测场,然后将每个图像的多边形连接回去。我们发现模型没有在 512X512 芯片的边缘进行预测,这在图像芯片的边缘留下了没有数据的区域:

合并的数据集最终看起来像这样:

我想提出已分离的多边形以进行合并操作,提出了以下修复:

  • 我使用了较大的未裁剪图像的角坐标以及图像的空间分辨率,并创建了模拟 512X512 图像芯片的渔网多边形。
  • 然后我对每个鱼网的形状文件进行了子集化。
  • 我查看了子集中每个多边形顶点的坐标,找到了子集中距离边界 2 m 以内的每个东距和北距,并且重复了不止一次(北边和南边为北边,东边为北边)东部和西部边界)。我这样做是因为我听起来多边形在 2 m 标记周围被切断,并且它们被切断的方式最终成为一条直线 - 所以我假设它们会有重复的东距/北距)。
  • 对于包含这些东距和北距的每个多边形中的每个顶点,我将相应的坐标更改为鱼网的相关坐标。
  • 对每个鱼网内的多边形完成此操作后,我使用 gpd.unary_union 将整个数据集合并为一个多边形。这个合并的多边形的顶点已更改为位于鱼网边界的两侧。这将整个数据集更改为单个多边形。然后我爆炸了多边形。  The following is a picture of the image after this was applied.

该功能在大多数情况下都是成功的。在相当多的情况下,它只合并一部分多边形

这是可以接受的,因为它仍然合并成一个多边形并代表多边形的大部分区域,但并不理想。在少数情况下,其中一些多边形仍未合并

我想知道这里是否有人知道我应该如何修改我的代码以捕获其中一些混合多边形并合并它们,甚至修复部分合并的多边形。请让我知道你的想法。太感谢了!这是代码:

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)

gis geospatial polygon geopandas mask-rcnn
1个回答
0
投票

我还使用神经网络来检测正射图像上的内容,并为其开发了一些软件:orthoseg。为了避免图像边缘的检测质量较差,我在稍大的重叠图块上运行检测,并丢弃额外/重叠的像素。这可确保一致的检测质量,并避免您遇到的问题。

© www.soinside.com 2019 - 2024. All rights reserved.