增加图像亮度而不会溢出

增加图像亮度而不会溢出

本文介绍了增加图像亮度而不会溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试提高图像亮度时遇到了问题。

I got a problem when trying to increase image brightness.

以下是原始图片:

我想要的图片是这样的:

The image I wanted to get is like this:

现在使用以下代码增加亮度:

Now to increase the brightness with the following code:

    image = cv2.imread("/home/wni/vbshare/tmp/a4_index2.png",0)

    if sum(image[0])/len(image[0])<200:
        new = np.where((255-image)<image,255,image*2)
    else:
        new = image
    return new

而且,我得到了以下图片:

And, I got the following image:

所以,似乎有些人的亮度积分溢出。

So, seems brightness of some points overflowed.

我试图将阈值从200更改为其他某个数字,例如125,100,140 .etc
然而,图像亮度几乎保持相同的暗或溢出。

And I tried to change the threshold from 200 to some other number, e.g. 125, 100, 140 .etcHowever, the image brightness stays either almost same dark or overflow.

环境:

Python:2.7.10

Python: 2.7.10

Opencv:3.2.0

Opencv: 3.2.0

对此有任何建议非常感谢。

Any suggestion for this is appreciated.

谢谢。

推荐答案

这是我拍摄的照片清理特定图像的简单算法。随意玩它并进一步调整以获得所需的结果。

Here's my shot at a simple algorithm for cleaning up that particular image. Feel free to play with it and tweak it further to get the desired result.

NB :显示的代码应与2.4一起使用。 OpenCV的x和3.x分支。

NB: The code shown should work both with the 2.4.x and 3.x branches of OpenCV.

将输入图像加载为灰度。

Load the input image as grayscale.

img = cv2.imread('paper.jpg',0)



步骤1



扩大图像,以摆脱文本。
此步骤有助于保留条形码。

Step 1

Dilate the image, in order to get rid of the text.This step somewhat helps to preserve the bar code.

dilated_img = cv2.dilate(img, np.ones((7,7), np.uint8))

使用适当大小的内核中位数模糊结果以进一步抑制任何文本。

Median blur the result with a decent sized kernel to further suppress any text.

这应该让我们得到一个包含所有阴影和/或变色的相当好的背景图片。

This should get us a fairly good background image that contains all the shadows and/or discoloration.

bg_img = cv2.medianBlur(dilated_img, 21)

计算我们刚刚获得的原始和背景之间的差异。相同的位将是黑色(接近0差异),文本将是白色(大差异)。

Calculate the difference between the original and the background we just obtained. The bits that are identical will be black (close to 0 difference), the text will be white (large difference).

因为我们想要白色的黑色,我们反转结果。

Since we want black on white, we invert the result.

diff_img = 255 - cv2.absdiff(img, bg_img)

标准化图像,以便我们使用完整的动态范围。

Normalize the image, so that we use the full dynamic range.

norm_img = diff_img.copy() # Needed for 3.x compatibility
cv2.normalize(diff_img, norm_img, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)

此时我们仍然有点灰色的纸张。我们可以将其截断,并重新规范化图像。

At this point we still have the paper somewhat gray. We can truncate that away, and re-normalize the image.

_, thr_img = cv2.threshold(norm_img, 230, 0, cv2.THRESH_TRUNC)
cv2.normalize(thr_img, thr_img, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)

完成...

嗯,至少对我来说;)你可能想要裁剪它,并做任何你想要的其他后期处理。

Well, at least for me ;) You will probably want to crop it, and do whatever other post-processing you desire.

注意:在获得差异图像后,可能值得切换到更高的精度(16+位int或float),以便最大限度地减少重复归一化中的累积舍入误差。

Note: It might be worth switching to higher precision (16+ bit int or float) after you get the difference image, in order to minimize accumulating rounding errors in the repeated normalizations.

这篇关于增加图像亮度而不会溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:46