Morphological Hit Miss Transform

image-processing, opencv

Solution

A simple implementation of hit-and-miss can be found here:

#include <opencv2/imgproc/imgproc.hpp>

// Hit-or-miss transform function
void hitmiss(cv::Mat& src,    // Source image, 8 bit single-channel matrix
             cv::Mat& dst,    // Destination image 
             cv::Mat& kernel) // Kernel. 1=foreground, -1=background, 0=don't care
{
    CV_Assert(src.type() == CV_8U && src.channels() == 1);

    cv::Mat k1 = (kernel == 1) / 255;
    cv::Mat k2 = (kernel == -1) / 255;

    cv::normalize(src, src, 0, 1, cv::NORM_MINMAX);

    cv::Mat e1, e2;
    cv::erode(src, e1, k1);
    cv::erode(1 - src, e2, k2);

    dst = e1 & e2;
}

But i think that you can solve the problem only with dilation, as the example in page 7 of this slide (it is taken from "Digital Image Processing" book from Gonzales et al.)

Problem

I am using OpenCV for my image processing algorithms and am trying to fix up ragged edges in characters. I read that the morphological Hit-Miss transform is a very good solution for this. Is there any open source implementation of this? Or is there any other algorithm that can be used to fix ragged edges?

Original source