How Can I Write Subset of GeoTIFF File to New GeoTIFF File with No Perpendicular to X and Y Axis?

2 views (last 30 days)
Matlab provides a series of geotiff-region image processing functions, such as “geotiffread”, “geotiffwrite”, “geotiffinfo” and more.
Among that, using the syntax [A, R] = geotiffread(filename) reads a georeferenced grayscale, RGB, or multispectral image or data grid from the GeoTIFF file specified by filename into A and constructs a spatial referencing object, R. A is an aspect of image processing and R is an aspect of spatial referencing.
On the other hand, using the syntax geotiffwrite(filename,A,R) writes a georeferenced image or data grid, A, spatially referenced by R, into an output file, filename. I would like to Write Subset of GeoTIFF File to New GeoTIFF File from MathWorks Documentation Examples.
%Read GeoTIFF file.
[A, R] = geotiffread('boston.tif');
%Specify subset of data that you want to write.
m = size(A,1);
n = 1024;
firstrow = m-n+1;
lastrow = m;
firstcol = 1;
lastcol = n;
subImage = A(firstrow:lastrow, firstcol:lastcol, :);
xi = [firstcol - .5, lastcol + .5];
yi = [firstrow - .5, lastrow + .5];
[xlimits, ylimits] = intrinsicToWorld(R, xi, yi);
subR = R;
subR.RasterSize = size(subImage);
subR.XLimWorld = sort(xlimits);
subR.YLimWorld = sort(ylimits);
info = geotiffinfo('boston.tif');
%Write subset to file.
filename = 'boston_subimage.tif';
geotiffwrite(filename, subImage, subR, 'GeoKeyDirectoryTag', info.GeoTIFFTags.GeoKeyDirectoryTag);
figure
mapshow(filename);
Currently, I use geotiffread to separate image data and spatial data, and subset AOI(Area Of Interest) shape from A, then I suffer a helpless difficulty regarding how spatial referenced information endue processing image.
Due to my AOI is NOT perpendicular to x-axis and y-axis, namely, the AOI is rotating. Consult the above code and I think that these two cases are not compatible. The syntaxes both "subR.XLimWorld = sort(xlimits);" and "subR.YLimWorld = sort(ylimits);" specifies separately xlimits and YLimWorld must be a size 1x2 ,or display error "Expected xWorldLimits to be of size 1x2 when it is actually size 1x3". It would imply a limitation of subset AOI(Area Of Interst) shape either square or rectangle and must be perpendicular(no rotating).
Would any expert on the area give me some helpful ideas or share anything you can assist, I really enthusiastic in the world which is created by MATLAB.
I am looking forward to your response.
Thanks for your reading :)

Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!