i have a picture of words , each word is bounded by a rectangular contour. i need the rectangular contour vector to be sorted so i can do some operation on it.
Here is the picture of words(with contours). image with contours
i have tried to sort it and my compare function is
bool my_compare(Rect2d rect1, Rect2d rect2)
{ if(((rect1.tl().y) <= rect2.tl().y) &&(rect1.tl().y < rect2.br().y))
return (rect1.tl().x < rect2.tl().x);
return (rect1.tl().y < rect2.tl().y);
}
also i tried this
if((rect1.tl().y == rect2.tl().y))
return (rect1.tl().x < rect2.tl().x);
return (rect1.tl().y < rect2.tl().y);
the result is not sorted properly (it doesnt start from very top left or it starts but it skips a contour in the same line ).
The reason is probably because rectangles in the same row has different height. Let's say a rectangle is followed by a taller rectangle in the same row, the taller rectangle is going to be sorted earlier than the left one.
+--------------> x
| ****
| **** * *
| * * * *
| **** ****
| rect1 rect2
v
y
Using your first compare function, rect1.tl().y <= rect2.tl().y
is false and rect1.tl().y < rect2.br().y)
is true, thus return (rect1.tl().y < rect2.tl().y)
which will put the right rectangle smaller than the left one
One suggestion to overcome this is if the rectangles top left corner difference in y is within a threshold, consider them to be on the same row and compare by x instead.
bool my_compare(Rect2d rect1, Rect2d rect2)
{
if (fabs(rect1.tl().y - rect2.tl().y) < threshold)
return (rect1.tl().x < rect2.tl().x);
return (rect1.tl().y < rect2.tl().y);
}
fabs
is the floating point absolute function under <cmath>
. One way to calculate threshold is by using average height of the rectangle.
threshold = ((rect1.br().y - rect1.tl().y) + (rect2.br().y - rect2.tl().y)) / 2 * K;
K is a constant which you can experiment to see which perform best, it should be between 0 and 1.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.