简体   繁体   English

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

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

I'm trying to mask an image with a shape from a geojson.我正在尝试使用 geojson 中的形状来掩盖图像。

First thing I do is get the pixels that represent my lat,long coordinates in the image.我做的第一件事是获取代表图像中经纬度坐标的像素。

When I have that information I build a 'Polygon' with that coordinates and then I try to crop the image, but I'm getting this error:当我获得这些信息时,我会使用该坐标构建一个“多边形”,然后尝试裁剪图像,但出现此错误:

'numpy.ndarray' object is not callable “numpy.ndarray”对象不可调用

Note that the absolute path refers to a .jp2 file.请注意,绝对路径是指.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()

I've also tried it using the first coordinate as the 5th to "seal" the polygon, but nothing changed.我也尝试过使用第一个坐标作为第五个坐标来“密封”多边形,但没有任何改变。

The only error message I got is this:我得到的唯一错误消息是:

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

Here all my entire code:这是我所有的全部代码:

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