【发布时间】:2016-03-31 00:00:05
【问题描述】:
我正在尝试将 4 波段(RGB 和 nr 红外)光栅图像转换为 ArcMap 中的 numPy 数组。成功转换为 numpy 数组后,我想计算图像上没有数据的像素数。在 ArcMap 中检查时,这些像素颜色被标记为“无”,它们显示为黑色,但它们缺少带 1,2 或 3 的红色、绿色和/或蓝色通道数据。我需要找到它们。
这是我目前所拥有的:
import numpy
import os
myDir = "C:\\Temp\\temp"
# myFile = "4_pixel_test.tif"
myFile = "4band.tif"
# import 4band (R,G,B & nr Infrared) image
fName = os.path.join(myDir, myFile)
head, tail = os.path.split(fName)
# Convert Raster to Array, Default using LowerLeft as Origin
rasArray = arcpy.RasterToNumPyArray(fName)
# find out the number of bands in the image
nbands = rasArray.shape[0] # int
# print nbands (int)
blackCount = 0 # count the black pixels per image
th = 0 # Threhold value
# print rasArray
r, g, b, a = rasArray # not working
rCheck = numpy.any(r <= th)
gCheck = numpy.any(g <= th)
bCheck = numpy.any(b <= th)
aCheck = numpy.any(a == 0)
print rCheck
print gCheck
print bCheck
print aCheck
# show the results
if rCheck:
print ("Black pixel (red): %s" % (tail))
elif gCheck:
print ("Black pixel (green): %s" % (tail))
elif bCheck:
print ("Black pixel (blue): %s" % (tail))
else:
print ("%s okay" % (tail))
if aCheck:
print ("Transparent pixel: %s" % (tail))
运行时错误 回溯(最近一次通话最后): 文件“”,第 14 行,在 文件“c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy__init__.py”,第 1814 行,位于 RasterToNumPyArray return _RasterToNumPyArray(*args, **kwargs) RuntimeError: ERROR 999998: 意外错误。
# previous code which might have incorrect numpy import
# options so I'm going with default options until I know better
# import numpy
# import os
#
# myDir = "C:\\Temp\\temp"
# myFile = "4_pixel_test.tif"
# fName = os.path.join(myDir, myFile)
#
# Convert Raster to Array
# rasArray = arcpy.RasterToNumPyArray(fName)
# maxVal = rasArray.max()
# minVal = rasArray.min()
# maxValpos = numpy.unravel_index(rasArray.argmax(),rasArray.shape)
# minValpos = numpy.unravel_index(rasArray.argmin(),rasArray.shape)
#
# desc = arcpy.Describe(fName)
# utmX = desc.extent.upperLeft.X + maxValpos[0]
# utmY = desc.extent.upperLeft.Y - maxValpos[1]
#
# for pixel in numpy.nditer(rasArray):
# # r,g,b = pixel # doesn't work - single dimension array
# print pixel
#
我能够将光栅图像从代码 here 更改为 numPY 数组。
不确定 numPY 数组是如何存储的,但是当迭代它时,数据会从 y 轴开始打印出来(逐列)图像而不是 x(逐行)。
我需要切换它,这样我才能从左上角到右下角逐个像素 (RGBA) 读取数据。但是,我对 numPy 知之甚少,无法做到这一点。
我认为有问题的错误可能是由于有问题的 tiff 的大小:它在 2.5MB 时工作正常,但在 4GB 时会下降。 :(
【问题讨论】:
-
这可能是 ArcMap 的限制,如果它下降到 >2GB。 ArcMap 是大多数版本的 32 位应用程序(不确定最新版本...)。因此,它不能处理超过 2GB 的内存。使用 32 位版本的 Python 根本不可能将 >2GB 的文件加载到内存中。如果你愿意,你可以使用 GDAL 和 64 位的 python 版本来代替。