So, the idea is that I have a .tif UAV image. I need to get R,G,B,A channels into 4 separate .tif files by maintaining the georeferencing. So, for instance: Red.tif, Green.tif, Blue.tif and Alpha.tif. I am using this approach:
(red, green, blue, alpha) = np.transpose(img, axes = (2,0,1))
Next, I want to do some calculations with the channels. For instance:
result = ((red**2)+(blue**2))/(blue)
I use this code in order to make the result.tif...
import rasterio
with rasterio.open('path/to/Red_channel.tif') as f:
red = f.read()
profile = f.profile
with rasterio.open('path/to/Blue_channel.tif') as f:
blue = f.read()
result = ((red2)+(blue2))/(blue)
with rasterio.open('path/to/Output.tif', 'w', **profile) as dst:
dst.write(result)
(source: Calculations with .tif images using matplotlib or rasterio)
Now, the problem is that when I try to use the Output.tif with gdalinfo to see the real coordinates, it does not show real coordinates, it shows pixel coordinates for each corner!! Any idea what is wrong and how I fix this?
Update: I store each of the bands after:
(red, green, blue, alpha) = np.transpose(f, axes = (2,0,1))
like this:
with rasterio.open('path/to/Green.tif', 'w', **profile) as dst:
dst.write(green)
and I have set:
profile["count"] = 4
This is the error I got:
Traceback (most recent call last):
File "code.py", line 126, in bands
(red, green, blue, alpha) = np.transpose(f, axes = (2,0,1))
File "<__array_function__ internals>", line 5, in transpose
File "/home/UbuntuUser/.local/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 653, in transpose
return _wrapfunc(a, 'transpose', axes)
File "/home/UbuntuUser/.local/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 55, in _wrapfunc
return _wrapit(obj, method, *args, **kwds)
File "/home/UbuntuUser/.local/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 44, in _wrapit
result = getattr(asarray(obj), method)(*args, **kwds)
ValueError: axes don't match array
f.read()reads the raster data to a numpy array. Hence, geospatial information is lost at this point. – Manuel Popp Oct 27 '21 at 13:26(red, green, blue, alpha) = np.transpose(img, axes = (2,0,1))with rasterio?? – just_learning Oct 28 '21 at 10:37np.transposecomes in. I split a GTiff into R, G, and B and used your code to execute the calculation and save the output. QGIS displayed the resulting GTiff right above the original one, so I assume the georeferences are correct. Did you usenp.transposeto generate the separate bands from a 4-band tiff? – Manuel Popp Oct 28 '21 at 11:06np.transposeto generate the separate bands from a 4-band tiff. – just_learning Oct 28 '21 at 13:43profile = f.profileline from where you read the red band and add the other part of code I suggested. This will get the information from the original RGBa raster (instead fromthe red band which has lost its geo-information), which should be the information you need. Just change the number of bands as I pointed out. – Manuel Popp Oct 28 '21 at 14:00(red, green, blue, alpha) = np.transpose(img, axes = (2,0,1))to split the image. – Manuel Popp Oct 28 '21 at 14:59R = os.path.join(wd, "xR.tif")stores the Red.tif , right? – just_learning Oct 28 '21 at 15:11R = os.path.join(wd, "xR.tif")creates the variable R with the value"C:\\Users\\Manuel\\Desktop\\test\\xR.tif"since I don't want to have to put the full paths everywhere. The function just pastes thewdstring and the filename"xR.tif"and inserts the path separator ("\" on Windows and "/" on Linux) – Manuel Popp Oct 28 '21 at 15:28