Monday 10 May 2021

opencv 45 detect and improve low contrast image


original

contrast level 1

contrast level 2

original, contrast level 1 - 5

final enhanced image
#low_contrast.py
from skimage.exposure import is_low_contrast
from imutils.paths import list_images
import argparse
import imutils
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input", required=True,
                help="path to input directory of images")
ap.add_argument("-t", "--thresh", type=float, default=0.35,
                help="threshold for low contrast")
args = vars(ap.parse_args())


def increase_contrast(image, level):
    lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
    l, a, b = cv2.split(lab)

    clahe = cv2.createCLAHE(clipLimit=0.01 + level, tileGridSize=(8, 8))
    cl = clahe.apply(l)

    limg = cv2.merge((cl, a, b))
    final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)

    return final


# grab images from low contrast directory
imagePaths = sorted(list(list_images(args["input"])))

# loop over the image paths
for (i, imagePath) in enumerate(imagePaths):
    # load the input image from disk, resize it, and convert it to
    # grayscale
    print("[INFO] processing image {}/{}".format(i + 1,
                                                 len(imagePaths)))
    image = cv2.imread(imagePath)
    image = imutils.resize(image, width=450)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    constrast_level = 0
    
    while True:
        # update the text and color
        text = "Low contrast: Yes"
        color = (0, 0, 255)

        image_copy = image.copy()

        # draw the text on the output image
        cv2.putText(image_copy, text, (5, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.8,
                    color, 2)

        if constrast_level == 0:
            cv2.imshow("original", image_copy)

        constrast_level += 1
        image_increased_contrast = increase_contrast(image, constrast_level)

        gray = cv2.cvtColor(image_increased_contrast, cv2.COLOR_BGR2GRAY)

        if not is_low_contrast(gray, fraction_threshold=args["thresh"]):
            # is *not* low contrast
            text = "Low contrast: No"
            color = (0, 255, 0)

            # draw the text on the output image
            cv2.putText(image_increased_contrast, text, (5, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.8,
                            color, 2)

            cv2.imshow("contrast level " + str(constrast_level), image_increased_contrast)

            break

        else:
            # draw the text on the output image
            cv2.putText(image_increased_contrast, text, (5, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.8,
                        color, 2)

            cv2.imshow("contrast level " + str(constrast_level), image_increased_contrast)

    cv2.waitKey(0)

---------------------
#logs
(venv) C:\Users\zchen\PycharmProjects\opencv>python low_contrast.py -i assets/low_contrast
[INFO] processing image 1/2
[INFO] processing image 2/2

reference:

increase image contrast

pip install skimage, DLL load failed while importing _remap

download & install visual studio 2019
In visual studio installer, check python development

No comments:

Post a Comment