SimaGuanxing
SimaGuanxing

Reputation: 691

Distance Transform Behaviors Differently Between Matlab and OpenCV

I am currently translating code from Matlab to OpenCV but found the distance transform function behaviors differently between Matlab and OpenCV. Take the simple matrix as an example bw =

 0     0     0     0     0
 0     1     0     0     0
 0     0     0     0     0
 0     0     0     1     0
 0     0     0     0     0

Matlab version distance transform assigns a number that is the distance between that pixel and the nearest nonzero pixel of BW, which makes sense and I got

1.4142    1.0000    1.4142    2.2361    3.1623
1.0000         0    1.0000    2.0000    2.2361
1.4142    1.0000    1.4142    1.0000    1.4142
2.2361    2.0000    1.0000         0    1.0000
3.1623    2.2361    1.4142    1.0000    1.4142

In OpenCV, I choose the DIST_L2 (the simple euclidean distance). it gives me

1.3692    0.9550    1.3692    2.3242    3.2792
0.9550         0    0.9550    1.9100    2.3242
1.3692    0.9550    1.3692    2.3242    1.3692
2.3242    1.9100    0.9550         0    0.9550
3.2792    2.3242    1.3692    0.9550    1.3692

I don't understand why and it doesn't make sense to me. I realized that OpenCV compute the pixel with nearest zero pixel, so I already inverted the input matrix.

Upvotes: 2

Views: 177

Answers (1)

SimaGuanxing
SimaGuanxing

Reputation: 691

maskSize – Size of the distance transform mask. It can be 3, 5, or CV_DIST_MASK_PRECISE (the latter option is only supported by the first function).

It looks like OpenCV version distance transform is doing some normalization using maskSize. Set it to 0 (Even the documentation didn't mention it) and it solves the issue.

Upvotes: 4

Related Questions