简体   繁体   English

使用python增加图像中特定像素的亮度

[英]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).请注意,您已将图像从 RGB(到 HSV)转换,并且需要将其从 BGR(到 HSV)转换。

If you only want to increase the brightness of the purple, then use cv2.inRange() for the purple color to create a mask.如果只想增加紫色的亮度,则使用 cv2.inRange() 为紫色创建蒙版。 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.所以这是在 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我的解决方案基于此链接: https : //docs.opencv.org/3.4/d3/dc1/tutorial_basic_linear_transform.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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