overwrite pixel value in tiff

4 views (last 30 days)
Raphael
Raphael on 23 Jan 2022
Commented: Raphael on 29 Sep 2023
Hi,
I have a geo-referenced TIFF file in which the values of some pixels need to be replaced and I want to save the new image as a geo-referenced TIFF file that inherits some attributes of the original image (in particular GPSInfo and DateTime). Subsequent processing is done outside Matlab so it is imperative that the new file is formatted exactly as a geo-referenced TIFF. Any suggestions on how to do that?
Thanks
Raphael

Answers (1)

Milan Bansal
Milan Bansal on 29 Sep 2023
Hi,
As per my understanding you want to change the pixel values of a geo-reference TIFF File and save the modified while retaining the spatial reference information.
Use the "readgeoraster" function to read the TIFF file. Also, get the "info" using "georasterinfo" function.
[originalImg,R] = readgeoraster('filename.tif');
info = georasterinfo("filename.tif");
"R" in the above code is the spatial reference.
Modify the image by changing the pixel values.
modifiedImage = A;
modifiedImage(modifiedImage < 100) = 200; % (example)
Get the Geo Tags.
geoTags = info.GeoTIFFTags.GeoKeyDirectoryTag;
Write the modified TIFF file as shown below. Set the spatial reference as the original image.
geotiffwrite("mod.tif",modifiedImage,R,'GeoKeyDirectoryTag',geoTags)
Refer to the below MathWorks documentation link to learn more about "readgeoraster" function.
Refer to the below MathWorks documentation link to learn more about "georasterinfo" function.
Refer to the below MathWorks documentation link to learn more about "geotiffwrite" function.
Hope it helps!

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!