在 OpenCV 中处理带有文本的图像时,我的打开操作不会导致正确的输出数据。该问题与本文中描述的问题非常相似:
http://www.cpe.eng.cmu.ac.th/wp-content/uploads/CPE752_06part2.pdf
c++ - OpenCV中的形态重建-LMLPHP
我所看到的,人们建议使用重建操作。 OpenCV 或一些已知的库/代码中是否有任何内置机制来实现这一点?

最佳答案

这是 my Python3 implementation 中的 ojit_a 类比 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

关于c++ - OpenCV中的形态重建,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29104091/

10-11 22:42
查看更多