简体   繁体   中英

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.

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

Note that the absolute path refers to a .jp2 file.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM