pythonopencvimage-processingcontrastimage-enhancement

Doing Contrast Enhancement Using A Map


So I've had a go at different way of manipulating the current image. I have the following image and subsequent mask for the image:

enter image description here

enter image description here

Recently I have been pointed to a method such as contrast enhancement of an image. I have looked potential ways to do it such as hsv splitting and appling the mask but have not been getting the results I'm looking for. Is there a way increase the contrast of the image so the areas of the image which have the saliency are brighter and the areas of low saliency aren't so bright. For example the image below, I want to try and get the same kind of result. I've looked at the following Automatic contrast and brightness adjustment of a color photo of a sheet of paper with OpenCV but haven't had much luck in regards to anything.

enter image description here


Solution

  • Here is the hard light composition in Python/OpenCV using an intensity modified saliency map. You can adjust the arguments in the rescale_intensity to adjust as desired.

    Image:

    enter image description here

    Saliency:

    enter image description here

    import cv2
    import numpy as np
    import skimage.exposure
    
    # read image 1
    img12 = cv2.imread('img12.png')
    hh, ww = img12.shape[:2]
    
    # read saliency mask as grayscale and resize to same size as img1
    mask = cv2.imread('hard_light_mask.png')
    mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
    mask = cv2.resize(mask, (ww,hh))
    mask = cv2.merge([mask,mask,mask])
    
    # adjust mask contrast and brightness
    mask = skimage.exposure.rescale_intensity(mask, in_range=(0,255), out_range=(92,192)).astype(np.uint8)
    print(mask.dtype)
    
    # threshold mask at mid gray and convert to 3 channels
    thresh = cv2.threshold(mask, 128, 255, cv2.THRESH_BINARY)[1]
    
    # do hard light composite of img12 and mask
    # see CSS specs at https://www.w3.org/TR/compositing-1/#blendinghardlight
    img12f = img12.astype(np.uint8)/255
    maskf =  mask.astype(np.uint8)/255
    threshf =  thresh.astype(np.uint8)/255
    threshf_inv = 1 - threshf
    low = 2.0 * img12f * maskf
    high = 1 - 2.0 * (1-img12f) * (1-maskf)
    result = ( 255 * (low * threshf_inv + high * threshf) ).clip(0, 255).astype(np.uint8)
    
    # save results
    cv2.imwrite('img12_reduced_hardlight.png', result)
    
    # show results
    cv2.imshow('img12', img12)
    cv2.imshow('mask', mask)
    cv2.imshow('thresh', thresh)
    cv2.imshow('result', result)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    Result:

    enter image description here