How to handle image extent error?

The following matlab script clips a larger mask image to the bounding box of a smaller grayscale image and masks out the area of the smaller image. This works great as long as the smaller image is within the extent of the larger image. However, if the smaller image is beyond the extent of the larger image (attached screenshot), I get the following error:

Index exceeds matrix dimensions.

Error in createMask (line 33) 
In = I(NI(1):NI(2),NI(3):NI(4));  % clipped binary

I would like any pixel value of the small image outside the extent of the larger image converted to 0. What methods can I use to handle this image extent error?


在这里输入图像描述


%% 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);

the easiest way I can think of is to use logical indexing to check for index validity. Here's a small example to show how it could be done:

I = ones(10);
NI = [-2 3 8 12];

% Get your rows and columns
iRows = NI(1):NI(2);
iCols = NI(3):NI(4);
desiredSize = [numel(iRows), numel(iCols)];

% Check validity
ilValidRows = iRows > 0 & iRows <= size(I, 1);
ilValidCols = iCols > 0 & iCols <= size(I, 2);

% set everything to zero
In = zeros(desiredSize);

% assign the values based on logical indexing
In(ilValidRows, ilValidCols) = I(iRows(ilValidRows), iCols(ilValidCols));

You end up with the following value for In

In =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     1     1     1     0     0
     1     1     1     0     0
     1     1     1     0     0
链接地址: http://www.djcxy.com/p/61168.html

上一篇: 从两侧的一个点沿着与x轴的角度生长线

下一篇: 如何处理图像范围错误?