Export an Image as a Geotiff that is not aligned with Lat/Long Grid

18 次查看(过去 30 天)
I am trying to use geotiffwrite to export an image. I know the lat/long values for the image corner points, but the image is rotated and not aligned with the lat/long grid. Can I use geotiffwrite to export a rotated image not aligned with north-south-east-west? I can add an transparent alpha channel to the image to align it, but geotiffwrite does not seem to support an alpha channel. Can I export an image using geotiffwrite with an alpha channel?

采纳的回答

Umar
Umar 2025-9-6,5:39

Hi @Richard,

Thank you for your detailed question. Based on your description, here’s how you can handle your rotated image and alpha channel in MATLAB:

1. Exporting a Rotated Image with `geotiffwrite`

MATLAB's `geotiffwrite` function assumes that the image is aligned with the north-south-east-west axes. It does not natively support exporting images that are rotated relative to the latitude/longitude grid. However, you can work around this limitation by embedding your rotated image into a larger, axis-aligned image with transparency. This approach allows you to maintain the spatial reference while accommodating the rotation.

2. Adding a Transparent Alpha Channel

While `geotiffwrite` does not natively support alpha channels, you can add transparency by setting the RGB values of the transparent areas to `[NaN NaN NaN]`. When viewed in MATLAB, these NaN values are interpreted as transparent. However, please note that other software may not recognize this transparency unless they specifically support it.

3. Practical Workflow

Here’s a MATLAB-based workflow to achieve your goal:

1. Embed the Rotated Image:

   % Create a larger, axis-aligned image
   largeImage = NaN(height, width, 3);  % Adjust dimensions as needed
   % Place your rotated image into the center
   largeImage(yOffset:yOffset+rotatedHeight-1, 
   xOffset:xOffset+rotatedWidth-1, :) = rotatedImage;

2. Set Transparent Areas:

   % Define transparent areas (e.g., where the image is NaN)
   transparentAreas = isnan(largeImage(:,:,1));
   % Set RGB values of transparent areas to NaN
   largeImage(repmat(transparentAreas, [1 1 3])) = NaN;

3. Save as GeoTIFF:

     % Define spatial referencing object R
     R = georefcells(latlim, lonlim, size(largeImage(:,:,1)));
     % Write the image to a GeoTIFF
     geotiffwrite('output.tif', largeImage, R);

This workflow embeds your rotated image into a larger, axis-aligned image and sets transparent areas to NaN, allowing you to export it as a GeoTIFF.

4. Alternative: Using GDAL for True Rotated GeoTIFF

For true rotated georeferencing, you may need to use external tools like GDAL. GDAL allows you to assign specific corner coordinates to your image, enabling accurate georeferencing even for rotated images.

If you need further assistance with any of these steps or have additional questions, feel free to ask.

  2 个评论
Richard
Richard 2025-9-8,1:07
I tried using you advise, but Google Earth would not properly import the GeoTiff image. I used the overlay function in the KML Toolbox found in the File Exchange tab. This worked, but it creates to files, the image (PNG file) and the KML file.
imwrite(outputImage,'test.png','Alpha',255*uint8(mask));
kmlObj = kml('Orthorectified Image');
kmlObj.overlay(min(lonlim),max(lonlim),min(latlim),max(latlim),'file','test.png','name',fname);
kmlObj.save('test')
Umar
Umar 2025-9-8,3:33

Hi @Richard,

That behavior is expected. MATLAB’s geotiffwrite cannot encode arbitrary rotation or an alpha band, which is why Google Earth won’t import your GeoTIFF correctly. Your PNG+KML overlay works because KML supports both rotation and transparency, but as you noticed it requires two files (PNG + KML, or zipped together as a KMZ).

If you need a single rotated GeoTIFF with alpha, the recommended approach is to export the PNG with alpha from MATLAB and then use GDAL (gdal_translate + gdalwarp) to assign the corner coordinates and embed the alpha channel. This produces a standalone GeoTIFF that most GIS tools (including Google Earth Pro) can read properly.

Hope this helps.

请先登录,再进行评论。

更多回答(0 个)

标签

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by