how to check if curves are similar
How to compare two black curves on a white background? How to do it as fast? For example t开发者_Go百科his
and this
are similar, but this
and this
are not
First curve I'll draw on JPanel, second is image.
I have an idea. Not sure that it is the best one, but somehow it allows to evaluate the coefficient of similarity of two cruves.
Lets make a matrix for each cruve putting 1 for black pixels and 0 for white ones. Now if we want to compare two cruves with matrixes a
and b
at first we should construct third matrix c
where:
if(a[i][j] == 0 && b[i][j] == 0)
c[i][j] = 0;
else if(a[i][j] == 0 || b[i][j] == 0)
c[i][j] = 1;
else
c[i][j] = 2;
Then we will denote by S
count of cells where c[i][j] != 0
, and by T
count of cells where c[i][j] == 2
. And at last two cruves are similar if T / S > 1 - eps
, where eps
you should choose yourself according what accuracy you want to get.
For each point of curve1 find nearest point on curve2, calculate maximal or average distance. Then swap curves, repeat and take maximal result.
If you want to take direction into account - use modified distance function which includes comparison of directions.
- Remove thickness.
- Replace original 'analogue' line with long strokes.
- Define set of possible 'shapes'. I can suggest the next set: 4 lines (horizontal, vertical, and 2 dianals), 4 curves - with breaks in up, left, right or down side, and ellipse.
- Iterate through the strokes of each line and make a decision about its shape.
This algorithm is not very accurate. You'll probably have to make additional analysis like:
- If the break in a curve is too small then the shape is ellipse.
- If the first/last stroke is short and directed differently from the rest strokes you should ignore it.
精彩评论