簡體   English   中英

Python 2.7 - CV2,Rasterio 收到錯誤 numpy.ndarray 對象不可調用

[英]Python 2.7 - CV2, Rasterio gets an error numpy.ndarray object is not callable

我正在嘗試使用 geojson 中的形狀來掩蓋圖像。

我做的第一件事是獲取代表圖像中經緯度坐標的像素。

當我獲得這些信息時,我會使用該坐標構建一個“多邊形”,然后嘗試裁剪圖像,但出現此錯誤:

“numpy.ndarray”對象不可調用

請注意,絕對路徑是指.jp2文件。

geoms = [{'type':'Polygon', 'coordinates': [[(x1,y1),(x2,y2),(x3,y3),(x4,y4)]]}]

with rasterio.open(absolutePath) as src:
    out_image,out_transform = mask(src,geoms,crop=True)
out_meta = src.meta.copy()

我也嘗試過使用第一個坐標作為第五個坐標來“密封”多邊形,但沒有任何改變。

我得到的唯一錯誤消息是:

TypeErrorTraceback (most recent call last)
<ipython-input-13-796462428f9b> in <module>()
      2 
      3 with rasterio.open(absolutePath) as src:
----> 4     out_image,out_transform = mask(src,geoms,crop=True)
      5 out_meta = src.meta.copy()

TypeError: 'numpy.ndarray' object is not callable

這是我所有的全部代碼:

from snappy import ProductIO
from snappy import PixelPos, GeoPos
import numpy as np
import geojson
import cv2
import os
import rasterio
from rasterio.mask import mask

path = '/home/.../x.SAFE'
product = ProductIO.readProduct(path)
sg = product.getSceneGeoCoding()

pathGeoJson = '/home/.../x.geojson'
with open(pathGeoJson) as f:
    gj = geojson.load(f)
features = gj['features'][0]
latlon1 = features.geometry.coordinates[0][0]
latlon2 = features.geometry.coordinates[0][1]
latlon3 = features.geometry.coordinates[0][2]
latlon4 = features.geometry.coordinates[0][3]

geoms = gj['features'][0].geometry
geoms

def LatLon_from_XY(ProductSceneGeoCoding, x, y):
    geoPos = ProductSceneGeoCoding.getGeoPos(PixelPos(x,y),None)
    lat = geoPos.getLat()
    lon = geoPos.getLon()
    return lat,lon

def XY_from_LatLon(ProductSceneGeoCoding, latitude, longitude):
    pixelPos = ProductSceneGeoCoding.getPixelPos(GeoPos(latitude, longitude),None)
    x = np.round(pixelPos.getX())
    y = np.round(pixelPos.getY())
    return x,y

x1,y1 = XY_from_LatLon(sg,latlon1[0], latlon1[1])
x2,y2 = XY_from_LatLon(sg,latlon2[0], latlon2[1])
x3,y3 = XY_from_LatLon(sg,latlon3[0], latlon3[1])
x4,y4 = XY_from_LatLon(sg,latlon4[0], latlon4[1])

print(x1,y1)
print(x2,y2)
print(x3,y3)
print(x4,y4)

pathToJP2 = '/home/.../IMG_DATA/'
arr = os.listdir(pathToJP2)

absolutePath = pathToJP2+arr[1]
arr[1]
print(absolutePath)

geoms = [{'type':'Polygon', 'coordinates': [[(x1,y1),(x2,y2),(x3,y3),(x4,y4)]]}]

with rasterio.open(absolutePath) as src:
    out_image,out_transform = mask(src,geoms,crop=True)
out_meta = src.meta.copy()

您可以使用opencv進行裁剪甚至應用蒙版, 這里最好回答

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM