Using the threshold functions in open CV on an image to get a binary image, with Otsu's thresholding I get a image that has white spots due to different lighting conditions in parts of the imageenter image description here

或者使用自适应阈值来固定光照条件,它无法准确地表示大津实际能够表示的铅笔填充的气泡.

enter image description here

How can I get both the filled bubbles represented and a fixed lighting conditions without patches? Here's the original image enter image description here

这是我的密码

    #binary image conversion
    thresh2 = cv2.adaptiveThreshold(papergray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 21, 13)
    thresh = cv2.threshold(papergray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

    cv2.imshow("Binary", thresh) #Otsu's
    cv2.imshow("Adpative",thresh2)

推荐答案

您的方法存在的问题:

您try 过的方法:

  1. Otsu threshold是基于整个图像中的所有像素值确定的(全局技术).如果查看图像的左下角,会出现灰色阴影,这可能会对确定阈值产生不利影响.
  2. Adaptive threshold分:here is a recent answer分关于为什么没有帮助.简而言之,它就像一个用于较小内核大小的边缘检测器

您可以try 的内容:

OpenCV的ximgproc模块具有专门的二进制图像生成方法.其中一种方法是流行的Niblack阈值技术.

这是一种依赖于统计度量的局部阈值技术.它将图像分为大小由用户预定义的块(子图像).基于每个块的101个像素值设置阈值.k由用户决定.

Code:

img =cv2.imread('omr_sheet.jpg')
blur = cv2.GaussianBlur(img, (3, 3), 0)
gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
niblack = cv2.ximgproc.niBlackThreshold(gray, 255, cv2.THRESH_BINARY, 41, -0.1, binarizationMethod=cv2.ximgproc.BINARIZATION_NICK)

Result:

enter image description here

100

  1. 了解更多关于cv2.ximgproc.niBlackThreshold的信息

  2. 您可能还想探索其他binarization techniques available个.它还包含研究论文的链接,详细解释了每种技术.

Edit:

See Prashant's answer.

Python相关问答推荐

具有多个组的条形图的不同y标度

如何在矩阵上并行化简单循环?

如何让pyparparsing匹配1天或2天,但1天和2天失败?

如何将Matplotlib的fig.add_axes本地坐标与我的坐标关联起来?

用gekko解决的ADE方程系统突然不再工作,错误消息异常:@错误:模型文件未找到.& &

替换字符串中的多个重叠子字符串

时间序列分解

如何比较numPy数组中的两个图像以获取它们不同的像素

不允许访问非IPM文件夹

调用decorator返回原始函数的输出

为什么Django管理页面和我的页面的其他CSS文件和图片都找不到?'

让函数调用方程

在Python中使用if else或使用regex将二进制数据如111转换为001""

如何在Pyplot表中舍入值

Flask Jinja2如果语句总是计算为false&

巨 Python :逆向猜谜游戏

提高算法效率的策略?

polars:有效的方法来应用函数过滤列的字符串

Polars map_使用多处理对UDF进行批处理

将一个双框爆炸到另一个双框的范围内