当拍摄一张纸时(例如使用手机摄像头),我得到以下结果(左图)(jpg下载here)。所需结果(使用图像编辑软件手动处理)位于右侧:
我想使用openCV处理原始图像,以自动获得更好的亮度/对比度(使背景更白)。
假设:图像为A4纵向格式(在本主题中,我们不需要对其进行透视扭曲),纸张为白色,文本/图像可能为黑色或彩色。
到目前为止我所做的:
各种自适应阈值方法,如高斯、OTSU(见OpenCV文档Image Thresholding)。它通常适用于大津:
ret, gray = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)
但它仅适用于灰度图像,而不直接适用于彩色图像。此外,输出是二进制的(白色或黑色),这是我不想要的:我更喜欢保持彩色的非二进制图像作为输出
根据这个answer(Histogram equalization not working on color image - OpenCV)或这个one(OpenCV Python equalizeHist colored image)的建议:
img3 = cv2.imread(f)
img_transf = cv2.cvtColor(img3, cv2.COLOR_BGR2YUV)
img_transf[:,:,0] = cv2.equalizeHist(img_transf[:,:,0])
img4 = cv2.cvtColor(img_transf, cv2.COLOR_YUV2BGR)
cv2.imwrite('test.jpg', img4)
或HSV:
img_transf = cv2.cvtColor(img3, cv2.COLOR_BGR2HSV)
img_transf[:,:,2] = cv2.equalizeHist(img_transf[:,:,2])
img4 = cv2.cvtColor(img_transf, cv2.COLOR_HSV2BGR)
不幸的是,结果相当糟糕,因为它在局部造成了可怕的微对比度(?)以下内容:
我也试过YCbCr,结果很相似。
我还尝试了CLAHE (Contrast Limited Adaptive Histogram Equalization)使用各种tileGridSize
从1
到1000
:
img3 = cv2.imread(f)
img_transf = cv2.cvtColor(img3, cv2.COLOR_BGR2HSV)
clahe = cv2.createCLAHE(tileGridSize=(100,100))
img_transf[:,:,2] = clahe.apply(img_transf[:,:,2])
img4 = cv2.cvtColor(img_transf, cv2.COLOR_HSV2BGR)
cv2.imwrite('test.jpg', img4)
但结果也同样糟糕。
按照问题How to apply CLAHE on RGB color images中的建议,使用LAB颜色空间执行此CLAHE方法:
import cv2, numpy as np
bgr = cv2.imread('_example.jpg')
lab = cv2.cvtColor(bgr, cv2.COLOR_BGR2LAB)
lab_planes = cv2.split(lab)
clahe = cv2.createCLAHE(clipLimit=2.0,tileGridSize=(100,100))
lab_planes[0] = clahe.apply(lab_planes[0])
lab = cv2.merge(lab_planes)
bgr = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
cv2.imwrite('_example111.jpg', bgr)
结果也不好。输出图像:
对每个通道分别进行自适应阈值化或直方图均衡化(R,G,B)不是一个选项,因为它会破坏颜色平衡,如here所述。
来自scikit-image
关于Histogram Equalization的教程中的“对比度拉伸”方法:
the image is rescaled to include all intensities that fall within the 2nd and 98th percentiles
稍微好一点,但离期望的结果还很远(请参见本问题上方的图片)。
TL;DR:如何使用OpenCV/Python自动优化一张纸的彩色照片的亮度/对比度?可以使用什么样的阈值/直方图均衡化/其他技术?
亮度和对比度可分别使用α(α)和β(β)进行调整。表达式可以写成
OpenCV已经将其实现为^{} ,因此我们可以将此函数与用户定义的
alpha
和beta
值一起使用。但问题是
本质上,问题是如何自动计算
alpha
和beta
。为此,我们可以查看图像的直方图。自动亮度和对比度优化计算alpha和beta,以便输出范围为[0...255]
。我们计算累积分布来确定颜色频率低于某个阈值(比如1%)的位置,并剪切直方图的左右两侧。这给了我们最小和最大的范围。这是一个可视化的直方图之前(蓝色)和之后剪辑(橙色)。请注意,在剪切之后,图像中更“有趣”的部分是如何发音的。为了计算
alpha
,我们在剪切后取最小和最大灰度范围,并将其与期望的输出范围255
分开为了计算beta,我们将其插入公式中,其中
g(i, j)=0
和f(i, j)=minimum_gray
在解决了这个问题之后
为了你的形象我们得到了这个
可能需要调整剪裁阈值以优化结果。下面是对其他图像使用1%阈值的一些示例结果
自动亮度和对比度代码
带有此代码的结果图像:
使用1%阈值的其他图像的结果
鲁棒的局部自适应软二值化!这就是我所说的
我以前也做过类似的工作,目的有点不同,所以这可能不完全适合您的需要,但希望它能有所帮助(我晚上写这段代码是为了个人使用,所以很难看)。从某种意义上说,与您的代码相比,这段代码旨在解决一个更为一般的情况,在这种情况下,我们可以在后台有很多结构化噪声(请参见下面的演示)。
摘要:这就是这个算法之后(之前和之后)页面的外观。注意,甚至颜色标记注释也不见了,所以我不知道这是否适合您的用例,但是代码可能很有用:
要获得一个完全干净的结果,您可能需要对过滤参数稍作修改,但正如您所看到的,即使使用默认参数,它也能很好地工作。
第0步:剪切图像使其与页面紧密配合
让我们看看你是怎么做到的(在你提供的例子中似乎是这样)。如果你需要一个手动注释和重写工具,只要下午我!^^此步骤的结果如下(我在这里使用的示例可能比您提供的示例更难,但可能与您的情况不完全匹配):
由此我们可以立即看到以下问题:
OpenCV
中可用的解决方案,以及它们的组合,但都没有成功!步骤1:伽马校正
这一步的理由是平衡整个图像的对比度(因为根据照明条件,您的图像可能会稍微曝光过度/曝光不足)。
一开始这看起来是一个不必要的步骤,但它的重要性不可低估:在某种意义上,它将图像标准化为相似的曝光分布,以便以后可以选择有意义的超参数(例如,下一节中的
DELTA
参数,噪声过滤参数,形态材料参数等)以下是伽玛调整的结果:
你可以看到,它是有点多…”平衡“现在。如果没有这一步,您将在后面的步骤中手动选择的所有参数都将变得不那么健壮!
步骤2:自适应二值化以检测文本块
在这一步中,我们将自适应地对文本blob进行二值化。稍后我将添加更多评论,但基本上是这样的:
BLOCK_SIZE
的块。诀窍是选择足够大的大小,这样你仍然可以得到大量的文本和背景(即比你拥有的任何符号都大),但要小到不受任何照明条件变化的影响(即“大,但仍然是局部的”)。BLOCK_SIZE
作为背景)。然后,我们进一步定义DELTA
——基本上只是一个阈值“我们仍将它视为背景距中值有多远?”。因此,函数
process_image
完成任务。此外,您可以修改preprocess
和postprocess
函数以满足您的需要(但是,正如您从上面的示例中看到的,该算法非常健壮,即它在不修改太多参数的情况下非常好地开箱即用)。此部分的代码假定前景比背景暗(即纸上的墨水)。但是你通过调整
preprocess
函数可以很容易地改变这种情况:只返回image
,而不是255 - image
。结果是像这样的漂亮斑点,紧跟着墨迹:
第三步:二值化的“软”部分
有了覆盖符号和一点点更多的斑点,我们终于可以做美白程序。
如果我们更仔细地观察带有文字的纸张(特别是那些有手写体的纸张)的照片,从“背景”(白纸)到“前景”(深色墨水)的转换不是很尖锐,而是非常渐进的。本节中的其他基于二值化的答案提出了一个简单的阈值(即使它们是局部自适应的,它仍然是一个阈值),这对于打印文本来说是可行的,但是对于手写文本来说不会产生那么好的结果。
因此,本节的动机是,我们希望保留从黑色到白色的渐进式传输的效果,就像使用天然墨水的纸张的自然照片一样。其最终目的是使其可打印。
其主要思想很简单:像素值(在上述阈值之后)与局部最小值的差异越大,它就越有可能属于背景。我们可以使用一系列Sigmoid函数来表示,重新缩放到局部块的范围(以便该函数在图像中自适应地缩放)。
有些东西是可选的,因此会被评论。
combine_process
函数接受上一步的掩码,并执行整个合成管道。你可以试着用它们来玩弄你的特定数据(图像)。结果很清楚:可能我会在这个答案中为代码添加更多的注释和解释。将在Github上上载整个内容(以及裁剪和扭曲代码)。
这种方法对您的应用程序应该很有效。首先在强度直方图中找到一个很好地分离分布模式的阈值,然后使用该值重新缩放强度。
我在这里使用Yen的方法,可以在this page上了解更多关于此方法的信息。
相关问题 更多 >
编程相关推荐