OpenCV中的形态重建

时间:2015-03-17 16:07:43

标签: c++ opencv morphological-analysis

我在OpenCV的图像处理过程中遇到了一些问题。我的图像上有文字,在使用打开操作时,我的输出不正确。该问题与本文中提供的问题非常相似:http://www.cpe.eng.cmu.ac.th/wp-content/uploads/CPE752_06part2.pdf

我可以看到,人们建议在这种情况下使用重建操作。我的问题是OpenCV中是否有任何内置机制或某些已知的库/代码实现这一点?谢谢你的帮助。

2 个答案:

答案 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)

这个答案迟到了,但这里是重构不足的基本算法:

  1. 输入是两个图像:ImReference和ImMarker,标记为< = reference
  2. 中级图片:ImRec
  3. 输出图像:ImResult
  4. 将ImMarker复制到ImRec
  5. 将ImRec复制到ImResult
  6. ImDilated =扩张(ImResult)
  7. ImRec =最小(ImDilated,ImReference)
  8. 如果ImRec!= ImResult,则返回步骤5.
  9. 它不是最优的算法,但它只使用基本操作。