1

I'm a beginner in Python and OpenCV. I want to read multiple images from a specific directory and preprocess them using CLAHE(Contrast Limited Adaptive histogram equalization) and finally save them (Preprocessed images) to another directory. I tried but it will work on a single image. How do I fix it? Here is my code below-

import numpy as np
import cv2
import glob

img = cv2.imread('00000001_000.png',0)

#create a CLAHE object (Arguments are optional).
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(20,20))
cl1 = clahe.apply(img)
cv2.imwrite('clahe_21.jpg',cl1)
Jacob
  • 27
  • 1
  • 7

1 Answers1

2

Try having a list of path to all the images and iterate over them :

all_img = glob.glob('.')
other_dir = 'new_path'
for img_id, img_path in enumerate(all_img):
    img = cv2.imread(img_path,0)

    #create a CLAHE object (Arguments are optional).
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(20,20))
    cl1 = clahe.apply(img)
    cv2.imwrite(f'{other_dir}/clahe_21_{img_id}.jpg',cl1)
Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
  • @Jacob This error might be related to path issue, not related to looping over images, Check [this](https://stackoverflow.com/questions/1347791/unicode-error-unicodeescape-codec-cant-decode-bytes-cannot-open-text-file) if it helps. – Arkistarvh Kltzuonstev Apr 26 '20 at 19:38
  • Thanks for the tremendous help, it will work @Arkistarvh Kltzuonstev – Jacob Apr 26 '20 at 19:42
  • one more question- if I resize all images before or after applying CLAHE then what should I do? – Jacob Apr 26 '20 at 19:56
  • @Jacob You can resize like this : `cv.resize(img, your_desired_shape, interpolation = cv.INTER_CUBIC)` where shape should be a tuple having width and height. – Arkistarvh Kltzuonstev Apr 26 '20 at 19:59