简体   繁体   中英

Increase brightness of specific pixels in an image using python

I would like to increase the brightness/vividness of the purple color in the following image:

在此处输入图片说明

Here is the color palette

在此处输入图片说明

Here is what I tried: but this increases the brightness of the whole image:

def increase_brightness(img, value=20):
    hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
    h, s, v = cv2.split(hsv)

    lim = 255 - value
    v[v > lim] = 255
    v[v <= lim] += value

    final_hsv = cv2.merge((h, s, v))
    img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
    plt.imsave('img_new.png', img)

    return img

how to create a mask to modify brightness only the pixels in the input that corresponds to purple?

Note you have converted the image from RGB (to HSV) and need to convert it from BGR (to HSV).

If you only want to increase the brightness of the purple, then use cv2.inRange() for the purple color to create a mask. Then modify the input image everywhere with your current method. Then use the mask to combine the input and modified images so as to only show the enhancement for the purple colors corresponding to the white in the mask.

So this is one to do that in Python/OpenCV.

Input:

在此处输入图片说明

import cv2
import numpy as np

# read image
img = cv2.imread('purple.png')

# set value
value = 20

# convert image to hsv colorspace
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)

# create mask on purple color and also its inverted mask
low_range = (80,160,50)
high_range = (150,230,120)
mask = cv2.inRange(hsv,low_range,high_range)
inv_mask = cv2.bitwise_not(mask)
mask = cv2.merge([mask,mask,mask])
inv_mask = cv2.merge([inv_mask,inv_mask,inv_mask])

# enhance the value channel of the hsv image
lim = 255 - value
v[v > lim] = 255
v[v <= lim] += value

# convert it back to BGR colors
final_hsv = cv2.merge((h, s, v))
bgr = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)

# use bit_wise_and and its inverse to combine the original and enhanced versions
bgr = cv2.bitwise_and(bgr,mask)
img = cv2.bitwise_and(img,inv_mask)
result = cv2.add(bgr,img)

# display IN and OUT images
cv2.imshow('IMAGE', img)
cv2.imshow('HSV', hsv)
cv2.imshow('MASK', mask)
cv2.imshow('RESULT', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

# save output image
cv2.imwrite('purple_enhanced.png', result)

Result:

在此处输入图片说明

If you alternate viewing of the input and output, you will see that the output is brighter everywhere.

You can add a contrast to your image. It is not possible to reuse the code, but create one that considers the contrast:

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

image = cv.imread('image.png')

def increase_brightness(image, alpha, beta):
    # Simple contrast control(alpha)
    # Simple brightness control(betha)

    new_image = np.zeros(image.shape, image.dtype)

    for y in range(image.shape[0]):
        for x in range(image.shape[1]):
            for c in range(image.shape[2]):
                new_image[y,x,c] = np.clip(alpha*image[y,x,c] + beta, 0, 255)

    plt.imsave('img_new.png', new_image)
    return new_image

I tested the following case:

increase_brightness(image, 1.0, 4)

Old image:

在此处输入代码

New image:

在此处输入图片说明

My solution is based on this link: https://docs.opencv.org/3.4/d3/dc1/tutorial_basic_linear_transform.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM