首页 > 解决方案 > 大黑字的自适应阈值处理

问题描述

我有一个包含非常大和非常小的字母的文档,我正在对其应用自适应阈值。

cvtColor(mbgra, dst, CV_BGR2GRAY);
GaussianBlur(dst, dst, Size(11, 11), 0);
adaptiveThreshold(dst, dst, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 11, 3);

该算法运行良好,但我有一个关于大黑色字母的小问题,因为它像这样从内部变得空心 在此处输入图像描述

原始图像有那些用黑色填充的字母

在此处输入图像描述

问题是如何在不增加过滤器块大小的情况下使这些字母像原始图像中一样用黑色填充,因为这与小字母效果不佳!

当然欢迎任何想法或建议!

标签: opencvimage-processingadaptive-thresholdimage-thresholding

解决方案


以下代码:

import numpy as np
import cv2
import matplotlib.pyplot as plt

image = cv2.imread("FYROJ.png")
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 3)

im_contours, contours, hier = cv2.findContours(thresh, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)
hier = hier[0]
kept_contours = [contour for idx, contour in enumerate(contours) if hier[idx][2] >= 0]

drawing = np.zeros_like(gray)
cv2.drawContours(drawing, kept_contours, -1, color=255)

ret, markers = cv2.connectedComponents(drawing)

watershed_res = cv2.watershed(image, np.int32(markers))

plt.imshow(watershed_res)
plt.show()

将生成此图像:在此处输入图像描述

也许尝试从这里开始,选择原始图像中有很多黑色像素的区域......


推荐阅读