【问题标题】:How to handle image extent error?如何处理图像范围错误?
【发布时间】: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


    【解决方案1】:

    我能想到的最简单的方法是使用逻辑索引来检查索引的有效性。下面是一个小例子来展示它是如何完成的:

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

    您最终得到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
    

    【讨论】:

    • 太好了,感谢您的帮助。当我尝试将所有这些放在一起时,我收到以下错误:A map.rasterref.MapCellsReference object was provided to function GEOTIFFWRITE, but its RasterSize value is not consistent with the raster size vector。关于我哪里出错的任何想法?
    • 不,我从未使用过该功能。我会检查功能文档
    猜你喜欢
    • 1970-01-01
    • 2015-05-24
    • 2017-05-18
    • 1970-01-01
    • 2016-07-20
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多