OpenCV中的形态重建

use*_*984 4 c++ opencv morphological-analysis

在 OpenCV 中处理带有文本的图像时,我的打开操作不会导致正确的输出数据。该问题与本文中描述的问题非常相似:http : //www.cpe.eng.cmu.ac.th/wp-content/uploads/CPE752_06part2.pdf

PDF 部分的屏幕截图

我所看到的,人们建议使用重建操作。OpenCV 或一些已知的库/代码中是否有任何内置机制来实现这一点?

Sem*_*ime 5

下面是我的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
Run Code Online (Sandbox Code Playgroud)