Home > Software engineering >  How can i load image correctly using Data Generator with preprocessing_function?
How can i load image correctly using Data Generator with preprocessing_function?

Time:09-26

im trying to apply CLAHE to my dataset, but Im not sure if it is applied correctly. Im trying to visualize the result after applying CLAHE, but I can only visualize if I use plt.imshow(img[0].astype('uint8')). So I am not sure if it is being applied correctly. Ill be very grateful if someone can help me.

BSZ=64
tsize=(48,48)    

clahe = cv2.createCLAHE(clipLimit=0.01, tileGridSize=(8,8))    
def claheImage(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = gray.astype(np.uint16)
    eq = clahe.apply(gray)
    eq = cv2.cvtColor(eq, cv2.COLOR_GRAY2BGR)
    eq = eq.astype(np.float32)
    return eq

val_generator = ImageDataGenerator(rescale=1/255,
                                  preprocessing_function=claheImage)

tr_generator = ImageDataGenerator(rescale = 1/255,
                                  zoom_range=0.3,
                                  shear_range=0.3,
                                  horizontal_flip=True,
                                  rotation_range=15,
                                  fill_mode="nearest",
                                  preprocessing_function=claheImage)

val_data = val_generator.flow_from_directory("./test",
                                             batch_size= BSZ,
                                             target_size=tsize,
                                             color_mode="rgb",
                                             interpolation="nearest")
tr_data = tr_generator.flow_from_directory("./train",
                                          batch_size= BSZ,
                                          target_size=tsize,
                                          color_mode="rgb",
                                          interpolation="nearest")

When I use this code I get an empty image and warning: "Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers)"

for _ in range(1):
    img, label = next(val_data)
    print(img.shape)
    plt.imshow(img[0])
    type(img[0])
    plt.show()

But when I use astype('unit8') I can display it. So, Im not sure if CLAHE is being applied correctly.

for _ in range(1):
    img, label = next(val_data)
    print(img.shape)
    #plt.imshow(img[0])
    plt.imshow(img[0].astype('uint8'))
    type(img[0])
    plt.show()

More details can be found in the following link.

https://colab.research.google.com/drive/1RguAZ9_9pREQNDkP6ort2mY1ffsFOLms?usp=sharing

CodePudding user response:

This is because of Matplotlib. The range of pixel values should be between [0, 255] for integers and [0., 1.] for floating point. Your preprocessing function must not respect this.

I suggest you remove rescale=1/255, but make sure the range of values that's output from your function is between 0 and 1.

def claheImage(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = gray.astype(np.uint16)
    eq = clahe.apply(gray)
    eq = cv2.cvtColor(eq, cv2.COLOR_GRAY2BGR)
    eq = eq.astype(np.float32)
    eq = eq / np.max(eq) # this makes sure the output is between 0 and 1
    return eq
  • Related