【发布时间】:2014-05-17 03:04:45
【问题描述】:
以下 matlab 脚本将较大的蒙版图像剪辑到较小灰度图像的边界框,并屏蔽掉较小图像的区域。只要较小的图像在较大图像的范围内,这就会很好。但是,如果较小的图像超出了较大图像的范围(附截图),我会收到以下错误:
Index exceeds matrix dimensions.
Error in createMask (line 33)
In = I(NI(1):NI(2),NI(3):NI(4)); % clipped binary
我希望将大图范围之外的小图的任何像素值转换为 0。我可以使用哪些方法来处理此图像范围错误?
%% this program clips a larger image using the extent of a smaller image..
% resample the images to match the pixel size and multiply with each other
%This program requires that the clip image is entirely within the larger image
[I B] = geotiffread('MASK.tif'); % larger image
[Ic R] = geotiffread('NDVI.tif'); % clip image
infoNDVI = geotiffinfo('NDVI.tif'); % geoinfo of clip layer
infoMASK = geotiffinfo('MASK.tif'); % geoinfo of larger layer
ncolsMASK = infoMASK.Width;
nrowsMASK = infoMASK.Height;
nrowsNDVI = infoNDVI.Height;
ncolsNDVI = infoNDVI.Width;
B1 = infoMASK.BoundingBox;
Bm = infoNDVI.BoundingBox;
res = infoMASK.PixelScale(1); % res is the pixel dimension
% Calculate the X and Y distances of the four corners of the clipping image from the origin of the larger image
d1 = fix([ (Bm(1) -B1(1)) (Bm(3) -B1(3))]/res);
d2 = fix([ (Bm(1) -B1(1)) (Bm(4) -B1(3))]/res);
d3 = fix([ (Bm(2) -B1(1)) (Bm(4) -B1(3))]/res);
d4 = fix([ (Bm(2) -B1(1)) (Bm(3) -B1(3))]/res);
% calculate row column indices of the clip layer and extract values for the clip extent
% Downscale resolution of the larger image to match the clip image
% Multiply the binary images and write that output as a geotiff file
NI = [ (nrowsMASK - d3(2)) (nrowsMASK - d1(2)) d1(1) d3(1) ];
In = I(NI(1):NI(2),NI(3):NI(4)); % clipped binary
resampledIn = imresize(In, [nrowsNDVI, ncolsNDVI]); % resample the binary layer to the size of clipped layer
intersected = immultiply(resampledIn,Ic);
outfilename = ['clipIntersect1' '.tif'];
geotiffwrite('OUTPUT.tif', intersected, R, 'GeoKeyDirectoryTag', infoC.GeoTIFFTags.GeoKeyDirectoryTag);
【问题讨论】:
标签: matlab image-processing raster bounding-box