作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要从 4 波段图像中提取 3 波段。我正在使用一个名为 NumpyArrayToRaster() 的函数,它最多只接受 3 个波段图像。我如何让它适用于 4 波段图像?
这是我现在的代码-
import arcpy
arcpy.CheckOutExtension("Spatial")
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
from skimage import io
from skimage.segmentation import quickshift
arcpy.env.overwriteOutput = True
# The input 4-band NAIP image
img = r'C:\Users\Alekhya\Desktop\Krishna\NRSC\processed image\newclip4\clip4.tif'
# Convert image to numpy array
imgarr = io.imread(img)
print imgarr
print imgarr.shape
print imgarr.dtype
# Run the quick shift segmentation
segments = quickshift(imgarr, kernel_size=3, convert2lab=False, max_dist=6, ratio=0.5)
print("Quickshift number of segments: %d" % len(np.unique(segments)))
# View the segments via Python
plt.imshow(segments)
print segments
print segments.shape
print type(segments)
print segments.dtype
# Get raster metrics for coordinate info
imgRaster = arcpy.sa.Raster(img)
# Lower left coordinate of block (in map units)
mx = imgRaster.extent.XMin
my = imgRaster.extent.YMin
sr = imgRaster.spatialReference
'''
# Note the use of arcpy to convert numpy array to raster
seg = arcpy.NumPyArrayToRaster(segments, arcpy.Point(mx,my), imgRaster.meanCellWidth, imgRaster.meanCellHeight)
outRaster = r'C:\Users\Alekhya\Desktop\Krishna\NRSC\processed image\newclip4\segments_clip4.tif'
seg_temp = seg.save(outRaster)
arcpy.DefineProjection_management(outRaster, sr)
'''
# Calculate NDVI from bands 4 and 3
b4 = arcpy.sa.Raster(r'C:\Users\Alekhya\Desktop\Krishna\NRSC\processed image\newclip4\clip4.tif\Band_4')
b3 = arcpy.sa.Raster(r'C:\Users\Alekhya\Desktop\Krishna\NRSC\processed image\newclip4\clip4.tif\Band_3')
ndvi = arcpy.sa.Float(b4-b3) / arcpy.sa.Float(b4+b3)
print ndvi
# Extract NDVI values based on image object boundaries
zones = arcpy.sa.ZonalStatistics(segments, "VALUE", ndvi, "MEAN")
zones.save(r'C:\Users\Alekhya\Desktop\Krishna\NRSC\processed image\newclip4\zones_clip4.tif')
# Classify the segments based on NDVI values
binary = arcpy.sa.Con(zones < 20, 1, 0)
binary.save(r'C:\Users\Alekhya\Desktop\Krishna\NRSC\processed image\newclip4\classified_clip4.tif')
最佳答案
在阅读您的代码时,我意识到您需要计算 NDVI 值。也许您可以稍微修改您的方法,而不是使用函数 NumpyArrayToRaster(
),您可以使用更简单的方法?
在这里我提供我的代码,其中:- 从多个日期(多波段合成)读取单独的 Sentinel 数据堆栈- 使用 rasterName/Band_X
识别 NDVI 计算所需的波段- 从波段计算 NDVI- 将每个日期的 NDVI 保存到输出文件夹,保留其名称和日期
在 Landsat 中,NDVI 的波段是 Band_4 和 Band_3
我的代码:
# calculate NDVI from sentinel data
# list raster and calculate NDVI per each raster individually
# import modules
import arcpy, string
# import environmental settings
from arcpy import env
from arcpy.sa import *
# check out spatial extension
arcpy.CheckOutExtension("spatial")
arcpy.env.overwriteOutput = True
# add workspace
env.workspace = "C:/Users/input"
# List rasters
rasters = arcpy.ListRasters("*", "TIF")
# Output directory
outWd = "C:/Users/output/ndvi"
# calculate ndvi for every sentinel raster
for raster in rasters:
# define ndvi outputs
outNDVI = outWd + "/"+ raster.replace(".tif", "_ndvi.tif")
print "outNDVI is " + outNDVI
# specify inputs for ndvi and final output
# NDVI takes NIR and Red, which are in Sentinel Band 4 and Band 8
Red = raster + '\Band_4'
NIR = raster + '\Band_8'
# Create Numerator and Denominator rasters as variables, and
# NDVI as output
Num = arcpy.sa.Float(Raster(NIR) - Raster(Red))
Denom = arcpy.sa.Float(Raster(NIR) + Raster(Red))
NDVI = arcpy.sa.Divide(Num, Denom)
print "NDVI calculating"
# save results output
NDVI.save(outNDVI)
print "NDVI saved"
关于python - 我如何使用 NumpyArrayToRaster() 从 4 波段图像中提取 3 波段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33332895/
我需要从 4 波段图像中提取 3 波段。我正在使用一个名为 NumpyArrayToRaster() 的函数,它最多只接受 3 个波段图像。我如何让它适用于 4 波段图像? 这是我现在的代码- imp
我是一名优秀的程序员,十分优秀!