2

So I have done some RGB-channel extraction from a .tif file, meaning I took UAV_image.tif and I have extracted R,G,B channels on three different .tif files. Next, I did some calculations such as result = ((red^2)+(blue^2))/(blue). For all the previous operations I used mainly cv2.imwrite and tiff.imread commands. The problem is that the resulted .tif does not contains coordinates.. How can I copy the coordinates from the initial UAV_image.tif to result.tif? I tried to followed this tutorial:

Use gdalsrsinfo to get the srs of the tiff that still has the projection:

gdalsrsinfo -o wkt tiffwithsrs.tiff Then copy the output and use gdal_translate to apply it to a new tiff:

gdal_translate -a_srs '...' tiffwithoutsrs.tif newfixedtif.tif just paste your projection after the -a_srs option

(source: Using GDAL command line to copy projections)

But the result was to copy both coordinates and colors...!!! Did I do something wrong?

Also, this guy here: "Copy" Image coordinates to another image that is nd.array has a an approach that does not fit my problem, because I do not do any kind of Machine Learning training (as I see to his example - correct me If I am wrong) and I do not use nd.array.

So, the question is how do I copy coordinates from a .tif image that has coordinates to a .tif image that does not have coordinates?

just_learning
  • 137
  • 2
  • 9

1 Answers1

2

You could do something like this in python using rasterio.

UAV_image = rasterio.open('UAV_image.tif')
new_tif = rasterio.open('new.tif','w',
                        driver='Gtiff',
                        height = UAV_image.height,
                        width = UAV_image.width, 
                        count = 1,
                        crs = UAV_image.crs,
                        transform = UAV_image.transform, 
                        dtype = UAV_image.dtypes)

new_tif.write(result, 1) #result from calculations UAV_image.close() new_tif.close()

This should take the geo-information from the original tif and write it to the new one.

DNy
  • 169
  • 5
  • Why do I get this error? File "copy_coordinates.py", line 11, in <module> dtype = UAV_image.dtype) AttributeError: 'DatasetReader' object has no attribute 'dtype' – just_learning Nov 02 '21 at 13:23
  • 1
    @just_learning Try with .dtypes , sorry spelling error. If that doesn't work you can write the datatype your new image has as a string 'uint8', 'float32' etc. – DNy Nov 02 '21 at 13:28
  • 1
    I assumed you had some array named result as calculated in the original question. That result will then be written to this new_tif file. – DNy Nov 02 '21 at 14:06
  • One last question, which I am trying to fix for over a week...Why the end result .tif loses the number of bands, when I try this command: img.shape. It gives me only height, width! Shouldn't give me also 1 as the number of channels? – just_learning Nov 02 '21 at 14:22
  • 1
    Maybe you are looking for img.counthttps://rasterio.readthedocs.io/en/latest/quickstart.html?highlight=count#opening-a-dataset-in-writing-mode – DNy Nov 02 '21 at 14:44
  • Last question, (sorry for the many questions so far...), but what should I change in the above if I need to copy only coordinates from A.tif to B.tif?? In other words I do not need to store any result. I tried some code but it does not work and I got confused...!! – just_learning Nov 08 '21 at 15:34
  • The problem is that I need to inherit the coordinates from A.tif (which is 4 band image) to B.tif (which is 3 band image). I tried configurations on count, and the band numbers on write command, but I get errors like this guy over here: link. I have read the tutorial: link2 without finding any solution to my problem...Any ideas/help what to do? – just_learning Nov 09 '21 at 12:08
  • 1
    I'm not really sure sorry. Maybe try adding an extra empty band to the 3 band image? The solutions you linked look useful also. – DNy Nov 09 '21 at 12:50