使用MATLAB';s poly2mask()与shapefile?

使用MATLAB';s poly2mask()与shapefile?,matlab,Matlab,我有一个shapefile(示例),我想用MATLAB的工具将它转换成一个二值感兴趣区域(ROI)掩码 MATLAB的描述如下: BW = poly2mask(x, y, m, n) BW=poly2mask(x,y,m,n)计算二元感兴趣区域(ROI) 来自ROI多边形的掩码BW,由向量x和y表示。这个 BW的大小为m×n。poly2mask在BW中设置位于 多边形(X,Y)设置为1,并将多边形外部的像素设置为0 如果多边形尚未闭合,poly2mask将自动关闭该多边形 这是我用来转换形状文

我有一个shapefile(示例),我想用MATLAB的工具将它转换成一个二值感兴趣区域(ROI)掩码

MATLAB的描述如下:

BW = poly2mask(x, y, m, n)
BW=poly2mask(x,y,m,n)计算二元感兴趣区域(ROI) 来自ROI多边形的掩码BW,由向量x和y表示。这个 BW的大小为m×n。poly2mask在BW中设置位于 多边形(X,Y)设置为1,并将多边形外部的像素设置为0

如果多边形尚未闭合,poly2mask将自动关闭该多边形

这是我用来转换形状文件的脚本:

s = 'D:\path\to\studyArea.shp'

shp = shaperead(s)
x = [shp.X];
y = [shp.Y];

% use bounding box to define m and n
m = shp.BoundingBox(2) - shp.BoundingBox(1)
n = shp.BoundingBox(3) - shp.BoundingBox(1) 

mask = poly2mask(x,y, m, n)

导致以下错误:

使用poly2mask时出错,要求输入编号1,X为有限

poly2mask中的错误(第49行) validateattributes(x,{'double'},{'real','vector','finite'},mfilename,'x',1)

createMask(第11行)mask=poly2mask(x,y,m,n)中的错误


我怀疑UTM坐标可能有问题,而不是Lat/Long,但是,我可以使用在这方面有经验的人的一些输入。我哪里出了问题

x和y值的最后一个坐标是NaN。 如果坐标中有NaN,poly2mask将不起作用。(因此错误“值必须是有限的”)

如果是这种情况,您可以使用快速修复

x = length(x)-1;
y = length(y)-1;
更多信息

-James

查看此处的更改-

s = 'studyArea.shp' %// Copy this file to working directory

shp = shaperead(s)
x = [shp.X];
y = [shp.Y];

%// Threw error before because there were NaNs there in x or y or both. So one assumption could be that you want to remove all x and y where any x or y is a NaN.  
cond1 = isnan(x) | isnan(y); 
x(cond1)=[];
y(cond1)=[];

% use bounding box to define m and n
m = shp.BoundingBox(2) - shp.BoundingBox(1)
n = shp.BoundingBox(3) - shp.BoundingBox(1) 

%mask = poly2mask(x,y, m, n); %// Threw error
mask = poly2mask(x,y, round(m/20), round(n/20)); %//Worked fine for smaller 3rd and 4th arguments

我感谢你的帮助。我对m和n的定义肯定也有问题,因为我不能使用image(mask)查看mask。你知道一个快速解决方法吗?