【发布时间】:2014-03-26 20:29:55
【问题描述】:
我正在尝试将 MATLAB 中的栅格处理限制为仅包含 shapefile 边界内的区域,类似于 ArcGIS Spatial Analyst 函数如何使用 mask。这是我正在使用的一些(可重现的)示例数据:
- 4-band NAIP image(警告 169MB 下载)
- shapefile of study area boundaries(File Dropper 上的压缩 shapefile)
这是我用来计算 NDVI 的 MATLAB 脚本:
file = 'C:\path\to\doi1m2011_41111h4nw_usda.tif';
[I R] = geotiffread(file);
outputdir = 'C:\output\'
% Calculate NDVI
NIR = im2single(I(:,:,4));
red = im2single(I(:,:,1));
ndvi = (NIR - red) ./ (NIR + red);
double(ndvi);
imshow(ndvi,'DisplayRange',[-1 1]);
% Stretch to 0 - 255 and convert to 8-bit unsigned integer
ndvi = floor((ndvi + 1) * 128); % [-1 1] -> [0 256]
ndvi(ndvi < 0) = 0; % not really necessary, just in case & for symmetry
ndvi(ndvi > 255) = 255; % in case the original value was exactly 1
ndvi = uint8(ndvi); % change data type from double to uint8
% Write NDVI to .tif file (optional)
tiffdata = geotiffinfo(file);
outfilename = [outputdir 'ndvi_' 'temp' '.tif'];
geotiffwrite(outfilename, ndvi, R, 'GeoKeyDirectoryTag', tiffdata.GeoTIFFTags.GeoKeyDirectoryTag)
下图说明了我想使用 MATLAB 完成的工作。对于此示例,我使用 ArcGIS raster calculator (Float(Band4-Band1)/Float(Band4+Band1)) 来生成右侧的 NDVI。我还将研究区域 shapefile 指定为 mask in the environment settings。
问题:
如何在 MATLAB 中使用多边形 shapefile 作为空间掩码来限制栅格处理范围以复制图中所示的结果?
我不成功尝试了什么:
roipoly 和 poly2mask,尽管我似乎无法正确应用这些功能(考虑到这些是空间数据)来产生所需的效果。
编辑:
我尝试了以下方法将 shapefile 转换为蒙版,但没有成功。不知道我哪里出错了......
s = 'C:\path\to\studyArea.shp'
shp = shaperead(s)
lat = [shp.X];
lon = [shp.Y];
x = shp.BoundingBox(2) - shp.BoundingBox(1)
y = shp.BoundingBox(3) - shp.BoundingBox(1)
x = poly2mask(lat,lon, x, y)
错误信息:
Error using poly2mask
Expected input number 1, X, to be finite.
Error in poly2mask (line 49)
validateattributes(x,{'double'},{'real','vector','finite'},mfilename,'X',1);
Error in createMask (line 13)
x = poly2mask(lat,lon, x, y)
【问题讨论】:
-
这取决于处理。例如,什么样的功能?
-
@chappjc 我的工作流程中的两个处理步骤包括计算 NDVI 和运行图像过滤器 (imfilter()) 以计算树冠覆盖率。
标签: image matlab image-processing shapefile arcpy