如何用n像素裁剪图像?

时间:2015-02-05 17:43:18

标签: image matlab crop geotiff

我正在尝试将地理位置图像修剪为500像素(500米)。以下是我到目前为止所尝试的内容,它读取地理位置并描述各种地理位置指标,例如边界框的x-min和y-min。我尝试使用imcrop(),但在尝试将输出写入geotiff时遇到错误。

在各方面修剪地平线500米(或像素)的最佳方法是什么?


%% Specify code and data locations
datadir = 'X:\temp\binary_data_temp\';     % set this to the directory where the data are stored; 1-band tiled images here
outputdir = 'X:\temp\temp_matlab_out2\';   % set this to the directory where files will be written

%%Load data
files = dir(fullfile(datadir, '*.tif'));
fileIndex = find(~[files.isdir]);

parfor ix = 1:length(fileIndex)         
    fileName = files(fileIndex(ix)).name;
    [Z, R]=geotiffread([datadir fileName]);

    %% Get geotiff metrics
    tiffdata = geotiffinfo([datadir fileName]);
    ncols = tiffdata.Width;
    nrows = tiffdata.Height;
    xllcorner = tiffdata.BoundingBox(1);
    yllcorner = tiffdata.BoundingBox(3);
    cellsize = tiffdata.UOMLengthInMeters;

    %% Crop image
    x500 = xllcorner + 500
    y500 = yllcorner + 500
    ncols500 = ncols - 1000
    nrows500 = nrows - 1000
    rectangle = [x500 y500 ncols nrows]

    clipped = imcrop(Z, rectangle)
    clipped2 = uint8(clipped)

    %% Write arrays to geotiffs
    [flout] = strread(fileName, '%s', 'delimiter','.')

    % Write clipped to .tif  
    outfilename = [outputdir 'clipped_geotiff_' char(flout(1)) '.tif'];  
    geotiffwrite(outfilename, clipped2, R, 'GeoKeyDirectoryTag', tiffdata.GeoTIFFTags.GeoKeyDirectoryTag) 

end   

这些是我在运行时收到的错误消息:

>> clip_raster
Starting parallel pool (parpool) using the 'local' profile ... connected to 8 workers.
Error using geotiffwrite>validateImage (line 383)
Expected input number 2, A, to be nonempty.

Error in geotiffwrite>validateInputs (line 337)
[A, cmap] = validateImage(A, cmap);

Error in geotiffwrite (line 235)
[filename, A, cmap, R, Params] = validateInputs( ...

Error in clip_raster (line 16)
parfor ix = 1:length(fileIndex)         % Note this is the MATLAB parallel processing version of a for loop

0 个答案:

没有答案