在 OpenCV 中处理带有文本的图像时,我的打开操作不会产生正确的输出数据。该问题与本文中描述的问题非常相似: http ://www.cpe.eng.cmu.ac.th/wp-content/uploads/CPE752_06part2.pdf
我所看到的,人们建议使用重建操作。OpenCV 中是否有任何内置机制或一些已知的库/代码可以实现这一点?
在 OpenCV 中处理带有文本的图像时,我的打开操作不会产生正确的输出数据。该问题与本文中描述的问题非常相似: http ://www.cpe.eng.cmu.ac.th/wp-content/uploads/CPE752_06part2.pdf
我所看到的,人们建议使用重建操作。OpenCV 中是否有任何内置机制或一些已知的库/代码可以实现这一点?
这是我的Python3
实现,类似于 MatLab 的imreconstruct
算法:
import cv2
import numpy as np
def imreconstruct(marker: np.ndarray, mask: np.ndarray, radius: int = 1):
"""Iteratively expand the markers white keeping them limited by the mask during each iteration.
:param marker: Grayscale image where initial seed is white on black background.
:param mask: Grayscale mask where the valid area is white on black background.
:param radius Can be increased to improve expansion speed while causing decreased isolation from nearby areas.
:returns A copy of the last expansion.
Written By Semnodime.
"""
kernel = np.ones(shape=(radius * 2 + 1,) * 2, dtype=np.uint8)
while True:
expanded = cv2.dilate(src=marker, kernel=kernel)
cv2.bitwise_and(src1=expanded, src2=mask, dst=expanded)
# Termination criterion: Expansion didn't change the image at all
if (marker == expanded).all():
return expanded
marker = expanded
这个答案来晚了,但这是重建不足的基本算法:
它不是最优化的算法,但它只使用基本操作。