Problem creating a geoTIFF from a non-georeferenced image

I'm trying to create a geoTIFF file in Matlab from the attached png. I'm following the example provided in: https://uk.mathworks.com/help/map/examples/exporting-images-and-raster-grids-to-geotiff.html
but need to create georeferencing information from scratch, so using makerefmat and worldfilewrite to acheive this. The code below does not cause a crash, but generates a TIFF that image readers seem to struggle with, so I assume I'm doing something wrong. There may also be some redundancy as I haven't worked with TIFF tags before. Any help appreciated!
% Load image without georeferencing
RGB = imread('uk_dT.png');
% Create worldfile for image. At present this is done by first creating a
% reference matrix, then using these values to generate a worldfile.
% Longitude spans -17:10 (west to east), latitude 63:47 (north to south)
lonmin = -17; lonmax = 10; latmin = 47; latmax = 63;
DX = (lonmax-lonmin)/(length(RGB(1,:,1))); DY = (latmin-latmax)/(length(RGB(:,1,1)));
R = makerefmat(lonmin, latmax, DX, DY);
worldfilewrite(R,'uk_dT.tfw');
% Read worldfile, create geotiff
REF = worldfileread('uk_dT.tfw','geographic',size(RGB));
geotiffwrite('uk_dT.tif',RGB,REF)

 Accepted Answer

file = 'uk_dT.png' ;
[path,name,ext] = fileparts(file) ;
I = imread(file) ;
lonmin = -17; lonmax = 10; latmin = 47; latmax = 63;
x = linspace(lonmin,lonmax,size(I,2)) ;
y = linspace(latmin,latmax,size(I,1)) ;
% Write to geotiff
R = georasterref('RasterSize',size(I),'LatitudeLimits',[latmin,latmax],'LongitudeLimits',[lonmin,lonmax]);
tiffile = strcat(name,'.tif') ;
geotiffwrite(tiffile,I,R)

4 Comments

Thanks for the quick response and the improved code efficiency. Unfortunately the resultant tiff still doesn't seem to display properly in the couple of image viewers I've tried (Windows photo viewer and Corel Photopaint). I'd accept that they were unable to open geoTIFFs except that Matlab's worked example (and geoTIFFs written using ArcGIS) does not have this problem?
When we read data from images...we read pixel values....check the class in there..it is of class uint8. So we are writing this data into geo tiff file. YOu can reproduce the same image from generated geotiff in MATLAB. Wehn you are reading it in other softwares..you need to check with the class.
Hi All,
Thank you for the sharing. I am working with this thing as well but I used UTM zone 50S coord instead of latlong. Could you please tell me what function to be used which is similar with georasterref but for UTM coord?
Thank you in advance.

Sign in to comment.

More Answers (0)

Asked:

on 7 Sep 2017

Edited:

on 18 Sep 2020

Community Treasure Hunt

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

Start Hunting!