Python 2.7 - CV2,Rasterio 收到错误 numpy.ndarray 对象不可调用



我正在尝试用来自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()

我也尝试使用第一个坐标作为第 5 个坐标来"密封"多边形,但没有任何变化。

我收到的唯一错误消息是这样的:

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进行裁剪甚至应用蒙版,这里回答得更好

最新更新