Ask Your Question
0

working hit-or-miss implementation

asked 2014-02-13 09:23:27 -0600

kamilk gravatar image

updated 2016-09-30 04:28:11 -0600

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?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-08-22 09:14:29 -0600

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);
edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-02-13 09:23:27 -0600

Seen: 1,526 times

Last updated: Aug 22 '16