How To Brighten A OpenCV Image

447 Views Asked by At

I have managed to get a saliency map of an image using the following code below. Here it obtains the saliency map as well as the inverse saliency map and then applies the results to the image in order to get the foreground and background of the image.

import cv2
import numpy as np

def SalienceMaps(image_path):
    
    image = cv2.imread(image_path)
    
    dimensions = (350, 450)    
    resized = cv2.resize(image, dimensions, interpolation = cv2.INTER_AREA)
    
    saliency = cv2.saliency.StaticSaliencyFineGrained_create()
    (success, saliencyMap) = saliency.computeSaliency(resized)
    
    (B, G, R) = cv2.split(resized)
    
    salBlue = B * saliencyMap.astype(saliencyMap.dtype)
    salGreen = G * saliencyMap.astype(saliencyMap.dtype)
    salRed= R * saliencyMap.astype(saliencyMap.dtype)
    
    salBlue = salBlue.astype("uint8")
    salGreen = salGreen.astype("uint8")
    salRed = salRed.astype("uint8")
    
    reduction = np.ones((450,350))
    inverse = reduction - saliencyMap
    
    inverseBlue = B * inverse.astype(inverse.dtype)
    inverseGreen = G * inverse.astype(inverse.dtype)
    inverseRed = R * inverse.astype(inverse.dtype)
    
    inverseBlue = inverseBlue.astype("uint8")
    inverseGreen = inverseGreen.astype("uint8")
    inverseRed = inverseRed.astype("uint8")
    
    main = cv2.merge((salBlue, salGreen, salRed))
    inverse = cv2.merge((inverseBlue, inverseGreen, inverseRed))

    return (main, inverse)

(main, inverse) = SalienceMaps("Content Image.jpg")
cv2.imshow("Image", main)
cv2.imshow("Image 2", inverse)
cv2.waitKey(0)

I get the following output for the foreground and background respectively when this image has been put in:

enter image description here enter image description hereenter image description here

I was wondering, how to improve the brightness or features of the foreground so the features are a lot more prominant and the image is brighter if that makes sense? Any idea what could be done? Cheers

1

There are 1 best solutions below

5
fmw42 On BEST ANSWER

You can use cv2.normalize() in Python/OpenCV to increase brightness.

Input:

enter image description here

import cv2
import numpy as np

# Read images
img = cv2.imread('img.png')

# stretch mask to full dynamic range
result = cv2.normalize(img, None, alpha=0, beta=350, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)

# write results
cv2.imwrite("img_enhanced.png", result)

# show results
cv2.imshow("img", img)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result:

enter image description here