我在OpenCV的图像处理过程中遇到了一些问题。我的图像上有文字,在使用打开操作时,我的输出不正确。该问题与本文中提供的问题非常相似:http://www.cpe.eng.cmu.ac.th/wp-content/uploads/CPE752_06part2.pdf。
我可以看到,人们建议在这种情况下使用重建操作。我的问题是OpenCV中是否有任何内置机制或某些已知的库/代码实现这一点?谢谢你的帮助。
答案 0 :(得分:3)
这里的my Python3 implementation与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
答案 1 :(得分:1)
这个答案迟到了,但这里是重构不足的基本算法:
它不是最优的算法,但它只使用基本操作。