| 1 | #include <cvd/convert_image.h> |
| 2 | #include <cvd/distance_transform.h> |
| 3 | #include <cvd/image_io.h> |
| 4 | #include <random> |
| 5 | #include <utility> |
| 6 | using namespace CVD; |
| 7 | |
| 8 | using std::cerr; |
| 9 | using std::cout; |
| 10 | using std::endl; |
| 11 | using std::mt19937; |
| 12 | using std::uniform_int_distribution; |
| 13 | |
| 14 | int main() |
| 15 | { |
| 16 | |
| 17 | mt19937 engine; |
| 18 | uniform_int_distribution<int> rand_size(50, 1050); |
| 19 | //Generate some random point sets. |
| 20 | for(int i = 0; i < 100; i++) |
| 21 | { |
| 22 | ImageRef s(rand_size(engine), rand_size(engine)); |
| 23 | |
| 24 | Image<byte> in(s); |
| 25 | |
| 26 | Image<Rgb<byte>> o(s); |
| 27 | |
| 28 | o.zero(); |
| 29 | in.zero(); |
| 30 | |
| 31 | uniform_int_distribution<int> rand_x(0, in.size().x - 1); |
| 32 | uniform_int_distribution<int> rand_y(0, in.size().y - 1); |
| 33 | |
| 34 | for(int i = 1; i < 1 + rand() % 10000; i++) |
| 35 | in[rand_y(engine)][rand_x(engine)] = 1; |
| 36 | |
| 37 | Image<int> out(in.size()); |
| 38 | Image<ImageRef> adt(in.size()); |
| 39 | |
| 40 | euclidean_distance_transform_sq(in, out, adt); |
| 41 | |
| 42 | //Make sure that the closest point matches the distance transform. |
| 43 | //Note that sometimes it fails to get anything for the closest point |
| 44 | //Also, note that this doesn't actually test to see if the closest point |
| 45 | //AND distance are correct, just consistent. |
| 46 | for(ImageRef p(-1, 0); p.next(in.size());) |
| 47 | { |
| 48 | if(adt[p] != ImageRef(-1, -1)) |
| 49 | { |
| 50 | double d = ((p - adt[p]).mag_squared()); |
| 51 | |
| 52 | if(abs(d - out[p]) != 0) |
| 53 | { |
| 54 | cerr << "Error.\n"; |
| 55 | cerr << d << endl; |
| 56 | cout << out[p] << endl; |
| 57 | cout << adt[p] << endl; |
| 58 | return 1; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |