Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

This is could be useful: http://opencv-code.com/tutorials/hit-or-miss-transform-in-opencv/ However, to avoid the glitch presented in this post, I'd suggest the following modified function:

void hitmiss(cv::Mat& src, cv::Mat& dst, cv::Mat& kernel)
{
    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::Point(-1, -1), 1, cv::BORDER_CONSTANT, cv::Scalar(0));
    cv::erode(1-src, e2, k2, cv::Point(-1, -1), 1, cv::BORDER_CONSTANT, cv::Scalar(0));
    if (countNonZero(k2) <= 0){
        e2 = src;
    }
    dst = e1 & e2;
}