增加图像亮度不溢出 [英] Increase image brightness without overflow
问题描述
我在尝试增加图像亮度时遇到问题.
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 等但是,图像亮度几乎保持不变或溢出.
And I tried to change the threshold from 200 to some other number, e.g. 125, 100, 140 .etc However, 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.
注意:显示的代码应该适用于 OpenCV 的 2.4.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+ 位整数或浮点数),以最大程度地减少重复归一化中累积的舍入误差.
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.
这篇关于增加图像亮度不溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!