Ask Your Question

hit and miss transform

asked 2015-10-10 06:28:24 -0500

Terry gravatar image

updated 2016-09-30 04:27:52 -0500

pklab gravatar image

I am looking at opencv functions. Could you please tell me how to do hit and miss transform -

It appears I can create my structuring element. However, I can only do erode, dilate, open, close, tophat, blackhat, gradient.

Given my own structuring element, how do I (use opencv to efficiently ) perform hitandmiss? Thanks T

edit retag flag offensive close merge delete


@Terry thanks for the link. you can post it as an answer here.

sturkmen gravatar imagesturkmen ( 2015-10-10 07:27:00 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-10-10 07:23:51 -0500

LorenaGdL gravatar image

This is could be useful: 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;
edit flag offensive delete link more


Great LorenaGdL.. Why don't include in OpenCV tree ?

pklab gravatar imagepklab ( 2015-10-12 05:20:35 -0500 )edit

@pklab - well, credit should be granted to the provided link, I just added a minor fix. But you're right, this might be simple enough to be included. I'll take a look and see if I can provide a PR

LorenaGdL gravatar imageLorenaGdL ( 2015-10-12 05:28:30 -0500 )edit

You can always add credit when submitting the PR or by adding a link to the tutorial in the header, that is used to generate the docs!

StevenPuttemans gravatar imageStevenPuttemans ( 2015-10-13 07:34:42 -0500 )edit

For anyone reading this thread in the future, hit-miss operation has been added to OpenCV 2.4 and 3.0 versions (PRs #5515 and #5622)

LorenaGdL gravatar imageLorenaGdL ( 2015-11-11 12:02:21 -0500 )edit

Thank you for submitting this functionality!

StevenPuttemans gravatar imageStevenPuttemans ( 2015-11-12 02:33:43 -0500 )edit

@LorenaGdL In open cv all morphology operation is inverted. We should use dilate instead of erode, and or operator instead of and (&).

dpietrek gravatar imagedpietrek ( 2016-08-21 20:18:12 -0500 )edit

@dpietrek you have already been answered in your post, but OpenCV works with white as foreground and black as background. Operations are not inverted, they just follow the computer vision convention

LorenaGdL gravatar imageLorenaGdL ( 2016-08-23 08:42:46 -0500 )edit

Future readers: now a tutorial is available on the official documentation to use the OpenCV HitorMiss function

LorenaGdL gravatar imageLorenaGdL ( 2017-01-05 10:24:39 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-10-10 06:28:24 -0500

Seen: 1,729 times

Last updated: Oct 10 '15