0

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:

OpenCV RGB image

And with tiff image and scikit-image if have this weird color effect:

Tiff and scikit RGB image

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.

KalvinB
  • 305
  • 1
  • 3
  • 11
  • Those images look like one or more of the byte offsets are wrong. The tif file standard is flexible (aka notorious for being violated by different companies), and potentially the source of your images is doing something weird with the file header. If you look closely, the "color effect" is due to a misalignment of the three channels, with the the green and blue channels being increasingly shifted towards the top w.r.t the red channel. If lightroom can still read the image fine, then I would re-export them using that. ImageJ would be another option, which would also support batch processing. – Paul Brodersen Nov 06 '19 at 11:29
  • If this is not a one-off job and ImageJ/Fiji is not cutting it, I would fork `tifffile` and create my own custom reader that is able to handle whatever weirdness is happening in the file header. `tifffile`'s source code is pretty readable and in our lab, we have created multiple forks to deal with different microscopy image sources that were abusing the header and causing problems downstream in the analysis. – Paul Brodersen Nov 06 '19 at 11:31
  • Please share one of your TIFF files and I'll take a look. Google "Mark Setchell photography" and you will find my Email address if Email is easier for you - please add a link to this question so I know what it's related to. – Mark Setchell Nov 14 '19 at 09:40

0 Answers0