I am currently trying to load a .TIF file in python and then save it as RGB and NIR image.
This is the code I use/I tried:
%matplotlib notebook
import tifffile as tiff
import cv2
from matplotlib import pyplot as plt
import skimage.io
import numpy as np
import os
def load_images_from_folder(folder):
images = []
for filename in os.listdir(folder):
img = None
print(filename)
if filename.endswith('.TIF'):
img = cv2.imread(os.path.join(folder,filename), -1)
#img = tiff.imread(os.path.join(folder,filename))
#img = skimage.io.imread(os.path.join(folder,filename), plugin='tifffile')
if img is not None:
images.append(img)
b,g,r,nir = cv2.split(img)
image_rgb = np.stack([b,g,r], axis=2)
nir_normalized = cv2.normalize(nir,None,0,255,cv2.NORM_MINMAX)
image_rgb_normalized = cv2.normalize(image_rgb,None,0,255,cv2.NORM_MINMAX)
skimage.io.imsave("satellite_imagery_split/" + filename[:-4] + "_nir_normalized.png", np.array(nir_normalized, dtype = np.uint8 ))
skimage.io.imsave("satellite_imagery_split/" + filename[:-4] + "_rgb_normalized.png", np.array(image_rgb_normalized, dtype = np.uint8 ))
cv2.imwrite("satellite_imagery_split/" + filename[:-4] + "_nir_normalized.png", np.array(nir_normalized, dtype = np.uint8 ))
cv2.imwrite("satellite_imagery_split/" + filename[:-4] + "_rgb_normalized.png", np.array(image_rgb_normalized, dtype = np.uint8 ))
return images
images = load_images_from_folder('satellite_imagery/')
but no variation works properly. With OpenCV i have weird bars all over the image:
And with tiff image and scikit-image if have this weird color effect:
I tried different combinations with the code above, but nothing gives me the proper results. But what is very weird is that with some images (2 out of 20) the result is as expected, so the RGB and NIR image look right, but with all of the others I experience this weird behavior.
Could somebody tell me what I am doing wrong, because I opened all images also with Adobe Lightroom and there all original four-channel images look right.