【问题标题】:I want to increase brightness and contrast of images in dynamic way so that the program is applicable for any new images我想以动态方式增加图像的亮度和对比度,以便该程序适用于任何新图像
【发布时间】:2019-05-31 05:07:43
【问题描述】:

我的图像很少,我需要以动态方式增加或减少图像的对比度和亮度,以便清晰可见。并且该程序需要是动态的,以便它甚至也适用于新图像。我也希望角色应该是黑暗的。

我能够增加亮度和对比度,但它不能对每张图像正常工作。

import cv2

import numpy as np

img = cv2.imread('D:\Bright.png')

image = cv2.GaussianBlur(img, (5, 5), 0)

#image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY)[1]

#kernel = np.ones((2,1),np.uint8)

#dilation = cv2.dilate(img,kernel)

cv2.imshow('test', image)

cv2.waitKey(0)

cv2.destroyAllWindows()

imghsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)


imghsv[:,:,2] = [[max(pixel - 25, 0) if pixel < 190 else min(pixel + 25, 255) for pixel in row] for row in imghsv[:,:,2]]

cv2.imshow('contrast', cv2.cvtColor(imghsv, cv2.COLOR_HSV2BGR))

#cv2.imwrite('D:\\112.png',cv2.cvtColor(imghsv, cv2.COLOR_HSV2BGR))

cv2.waitKey(0)

cv2.destroyAllWindows()

#raw_input()

我想要一个适用于每张图片的程序,并且文字稍微暗一点,以便于看到。

【问题讨论】:

  • 许多图形程序都有自动优化对比度/亮度设置。这对你有用吗?你想在opencv中实现类似的东西吗?它可能是基于直方图的。清晰可见到底是什么意思?至少对我来说,您提供的所有示例似乎都清晰可见。
  • @Trilarion 这里清晰可见基本上意味着不应该存在模糊,并且每个字符必须单独可读。我需要帮助将其实现到 Opencv 中

标签: python opencv image-processing brightness contrast


【解决方案1】:

正如 Tilarion 建议的那样,您可以尝试“自动亮度和对比度”,看看它是否运作良好。这背后的理论在解决方案部分中有很好的解释here。解决方案是在 C++ 中。我在 python 中编写了一个版本,您可以直接使用它,一次只能在 1 个通道上用于彩色图像:

def auto_brightandcontrast(input_img, channel, clip_percent=1):
    histSize=180
    alpha=0
    beta=0
    minGray=0
    maxGray=0
    accumulator=[]

    if(clip_percent==0):
        #min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(hist)
        return input_img

    else:
        hist = cv2.calcHist([input_img],[channel],None,[256],[0, 256])
        accumulator.insert(0,hist[0])    

        for i in range(1,histSize):
            accumulator.insert(i,accumulator[i-1]+hist[i])

        maxx=accumulator[histSize-1]
        minGray=0

        clip_percent=clip_percent*(maxx/100.0)
        clip_percent=clip_percent/2.0

        while(accumulator[minGray]<clip_percent[0]):
            minGray=minGray+1

        maxGray=histSize-1
        while(accumulator[maxGray]>=(maxx-clip_percent[0])):
            maxGray=maxGray-1

        inputRange=maxGray-minGray

        alpha=(histSize-1)/inputRange
        beta=-minGray*alpha

        out_img=input_img.copy()

        cv2.convertScaleAbs(input_img,out_img,alpha,beta)

        return out_img

【讨论】:

  • 我在我的项目中使用它,你在哪里失败了?还是输出不符合您的目的?
【解决方案2】:

在 Python Wand(基于 ImageMagick)中只需几行代码即可。这是一个脚本。

#!/bin/python3.7

from wand.image import Image    

with Image(filename='task4.jpg') as img:
    img.contrast_stretch(black_point=0.02, white_point=0.99)
    img.save(filename='task4_stretch2_99.jpg')

输入:

结果:

增加黑点值以使文本更暗和/或减少白点值以使较亮的部分更亮。

感谢 Eric McConville(Wand 开发人员)纠正我的论点以使代码正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    相关资源
    最近更新 更多