Ask Your Question
0

working hit-or-miss implementation

asked Feb 13 '14

kamilk gravatar image

updated Sep 30 '16

pklab gravatar image

I need an implementation of hit-or-miss transform done in OpenCV. Searching the internet, all I found is this program on opencv-code.com. The implementation, however, is broken. As an example, take the kernel

0 0 0
0 1 0
0 0 0

That is, a one in the middle and don't-cares everywhere else. The expected behaviour would be to simply copy the input image and that's what the MATLAB function bwhitmiss does. The provided snippet, however, zeros a few values.

Original:

 [0, 0,  0,  0,  0, 0, 0,  0;
  0, 1,  1,  1,  0, 0, 0,  0;
  0, 1 , 0, *1*, 0, 0, 0,  1;
  0, 1, *1*, 1,  0, 1, 0,  0;
  0, 0 , 1,  0,  0, 0, 0,  0;
  0, 0,  1,  0,  0, 1, 1,  0;
  0, 1,  0, *1*, 0, 0,*1*, 0;
  0, 1, *1*, 1,  0, 0, 0,  0]

Hit-and-miss result:

 [0, 0,  0,  0,  0, 0, 0,  0;
  0, 1,  1,  1,  0, 0, 0,  0;
  0, 1,  0, *0*, 0, 0, 0,  1;
  0, 1, *0*, 1,  0, 1, 0,  0;
  0, 0,  0,  0,  0, 0, 0,  0;
  0, 0,  1,  0,  0, 1, 1,  0;
  0, 1,  0, *0*, 0, 0,*0*, 0;
  0, 1, *0*, 1,  0, 0, 0,  0]

Does someone have an idea for a fix?

Preview: (hide)

1 answer

Sort by » oldest newest most voted
1

answered Aug 22 '16

dpietrek gravatar image

Morphology in opencv works on set of white pixels. So all dual operations are inverted. If you want classic hit or miss, do this:

Mat kernel; // Define your kernel
Mat negkernel;
bitwise_not(element,negkerne);
negkernel = negkernel- 254;

Mat erode;
Mat negerode;

morphologyEx(img, erode, MORPH_DILATE, kernel);
morphologyEx(negerode, negerode, MORPH_DILATE, negkernel);

Mat output;
bitwise_or(erode,negerode,output);
Preview: (hide)

Question Tools

Stats

Asked: Feb 13 '14

Seen: 1,562 times

Last updated: Aug 22 '16