opencv - 大黑字的自适应阈值处理
问题描述
我有一个包含非常大和非常小的字母的文档,我正在对其应用自适应阈值。
cvtColor(mbgra, dst, CV_BGR2GRAY);
GaussianBlur(dst, dst, Size(11, 11), 0);
adaptiveThreshold(dst, dst, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 11, 3);
该算法运行良好,但我有一个关于大黑色字母的小问题,因为它像这样从内部变得空心
原始图像有那些用黑色填充的字母
问题是如何在不增加过滤器块大小的情况下使这些字母像原始图像中一样用黑色填充,因为这与小字母效果不佳!
当然欢迎任何想法或建议!
解决方案
以下代码:
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()
也许尝试从这里开始,选择原始图像中有很多黑色像素的区域......
推荐阅读
- ionic-framework - Ionic 3 外部链接仅在 appbrowser 外部的系统浏览器中打开一次
- vba - 从小计组创建命名范围
- android - 管理活动状态,即使它被终止
- java - 如何在 Java 中绘制带星号的倒金字塔
- mysql - 如何在centos 7上恢复新数据库中的mysql数据库文件
- angular - Angular HTTP 请求错误:“发布有效请求”
- wpf - 拦截 Xamarin 表单 WPF 中的后退按钮
- sql - Oracle - 禁用大规模更新
- lapack - 使用 Lapack 子程序执行 Fortran 文件(程序退出代码:127)
- windows - 从批处理文件显示气球通知