First time here? Check out the FAQ!

Ask Your Question
1

hit and miss transform

asked Oct 10 '15

Terry gravatar image

updated Sep 30 '16

pklab gravatar image

I am looking at opencv functions. Could you please tell me how to do hit and miss transform - http://homepages.inf.ed.ac.uk/rbf/HIP...

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

Preview: (hide)

Comments

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

sturkmen gravatar imagesturkmen (Oct 10 '15)edit

1 answer

Sort by » oldest newest most voted
5

answered Oct 10 '15

LorenaGdL gravatar image

This is could be useful: http://opencv-code.com/tutorials/hit-... 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;
}
Preview: (hide)

Comments

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

pklab gravatar imagepklab (Oct 12 '15)edit
1

@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 (Oct 12 '15)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 (Oct 13 '15)edit
1

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 (Nov 11 '15)edit

Thank you for submitting this functionality!

StevenPuttemans gravatar imageStevenPuttemans (Nov 12 '15)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 (Aug 22 '16)edit
1

@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 (Aug 23 '16)edit
2

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

LorenaGdL gravatar imageLorenaGdL (Jan 5 '17)edit

Question Tools

1 follower

Stats

Asked: Oct 10 '15

Seen: 3,217 times

Last updated: Oct 10 '15