C# 如何使用 opencv 均衡图像的对比度和亮度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10561222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I equalize contrast & brightness of images using opencv?
提问by tofi9
I've got an image that I've scanned, but the white paper is not white on the screen. Is there a way to equalize the contract/brightness to make the background whiter?
我有一张已扫描的图像,但屏幕上的白纸不是白色的。有没有办法平衡合同/亮度以使背景更白?
Update
更新
I've tried the suggested Image._EqualizeHist function from EmguCv:
我已经尝试了 EmguCv 建议的 Image._EqualizeHist 函数:
string file = @"IMG_20120512_055533.jpg";
Image<Bgr, byte> originalColour = new Image<Bgr, byte>(file);
Image<Bgr, byte> improved = originalColour.Clone();
improved._EqualizeHist();
But get an even worse result (also when first gray scaled):
但得到更糟糕的结果(也是在第一次灰度化时):
Am I missing other parameters?
我是否缺少其他参数?
采纳答案by Abid Rahman K
I have discussed some techniques here : How can I adjust contrast in OpenCV in C?
我在这里讨论了一些技术:如何在 C 中调整 OpenCV 中的对比度?
Please check it. Below are the results i got when i tried last two methods on your image
请检查一下。以下是我在您的图像上尝试最后两种方法时得到的结果
1) Thresholding:
1)阈值:
Thresholding gives a binary image. If that is what you want you can apply threshold function
阈值化给出一个二值图像。如果这是你想要的,你可以申请threshold function
2) If grayscale image needed:
2)如果需要灰度图像:


Additional :
额外的 :
Morphological closingalso work good in your case
Morphological closing在你的情况下也很好用
img = cv2.imread('home.jpg',0)
kernel1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
close = cv2.morphologyEx(gray,cv2.MORPH_CLOSE,kernel1)
div = np.float32(gray)/(close)
res = np.uint8(cv2.normalize(div,div,0,255,cv2.NORM_MINMAX))
(code in Python API)
(Python API 中的代码)
Result Below:
结果如下:


回答by Sam
It is called equalizeHist. I do not know its name in emgu, but the result should be exactly what you need - brighter background, and darker text.
它被称为equalizeHist。我不知道它在 emgu 中的名称,但结果应该正是您所需要的 - 更亮的背景和更暗的文本。
EDIT
编辑
To extract only the edges (which is very different from the image enhancement techniques) you can simply apply Canny. Select the two thresholds as 20 and 60, for start, and then increase (or decrease them) keeping a ration of 3:1 between them, until you have a good-looking edge image.
要仅提取边缘(这与图像增强技术非常不同),您可以简单地应用 Canny。选择两个阈值为 20 和 60,作为开始,然后增加(或减少)它们之间的比例为 3:1,直到你有一个好看的边缘图像。
回答by fraxel
I suggest using AdaptiveThreshold. It works by doing local neighbourhood thresholding for every pixel in the image (this really is a big deal when there are gradient backgrounds, a bit stronger than in your image). The blockSizeparameter is the size of the neighbourhood, and the processed pixels value must be greater than the average neighbourhood value minus param1.
我建议使用AdaptiveThreshold。它的工作原理是对图像中的每个像素进行局部邻域阈值处理(当有渐变背景时,这真的很重要,比图像中的要强一点)。该blockSize参数是邻域的大小,并且将处理后的像素值必须是比平均附近值减去更大param1。


Here is how to do it in python (it should be very easy to convert to c):
以下是在python中的操作方法(转换为c应该很容易):
import cv
im = cv.LoadImage("9jU1Um.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE)
cv.AdaptiveThreshold(im, im, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C,
cv.CV_THRESH_BINARY, blockSize=31, param1=15)
cv.ShowImage('image', im)
cv.WaitKey(0)
回答by Mzk
Either way, you can also check every each pixel. Set it to 0 if less then a defined value and set to 255 if exceed the define value.
无论哪种方式,您还可以检查每个像素。如果小于定义值,则将其设置为 0,如果超过定义值,则将其设置为 255。
回答by Gustavo Kaneto
To change brightness and contrast, you can multiply your pixel values and then add some constant to them. (More info on Changing the contrast and brightness of an image, in OpenCV docs.)
要更改亮度和对比度,您可以乘以像素值,然后向它们添加一些常量。(有关更改图像的对比度和亮度的更多信息,请参见 OpenCV 文档。)
Using python and numpy:
使用 python 和 numpy:
import cv2 as cv
import numpy as np
img = cv.imread('b.jpg',0) # loads in grayscale
alpha = 1
beta = 0
res = cv.multiply(img, alpha)
res = cv.add(res, beta)
You can also just use:
你也可以只使用:
res = cv.convertScaleAbs(img, alpha = alpha, beta = beta)
In your image, you can check in histogram that the maximum values are around 170 (actually, it is 172, if you use img.max()). So, you can multiply your image by 255/172 = 1.48to increase brightness.
在您的图像中,您可以检查直方图中的最大值约为 170(实际上,如果使用 ,则为 172 img.max())。因此,您可以将图像乘以255/172 = 1.48增加亮度。
See the results below:
查看以下结果:
And the histograms, respectively:
和直方图,分别为:

