| 1025 | } |
| 1026 | |
| 1027 | void test_filtering2(int nr, int nc, dlib::rand& rnd) |
| 1028 | { |
| 1029 | print_spinner(); |
| 1030 | dlog << LINFO << "test_filtering2(): " << nr << " " << nc; |
| 1031 | array2d<float> img(302,301); |
| 1032 | for (long r = 0; r < img.nr(); ++r) |
| 1033 | { |
| 1034 | for (long c = 0; c < img.nc(); ++c) |
| 1035 | { |
| 1036 | img[r][c] = rnd.get_random_gaussian(); |
| 1037 | } |
| 1038 | } |
| 1039 | matrix<float> filt = matrix_cast<float>(randm(nr,nc,rnd)); |
| 1040 | |
| 1041 | matrix<float> out = xcorr_same(mat(img),filt); |
| 1042 | matrix<float> out2 = subm(conv(mat(img),flip(filt)), filt.nr()/2, filt.nc()/2, img.nr(), img.nc()); |
| 1043 | // make sure xcorr_same does exactly what the docs say it should. |
| 1044 | DLIB_TEST(max(abs(out-out2)) < 1e-7); |
| 1045 | |
| 1046 | // Now compare the filtering functions to xcorr_same to make sure everything does |
| 1047 | // filtering in the same way. |
| 1048 | array2d<float> imout(img.nr(), img.nc()); |
| 1049 | assign_all_pixels(imout, 10); |
| 1050 | rectangle rect = spatially_filter_image(img, imout, filt); |
| 1051 | border_enumerator be(get_rect(imout),rect); |
| 1052 | while (be.move_next()) |
| 1053 | { |
| 1054 | DLIB_TEST(imout[be.element().y()][be.element().x()] == 0); |
| 1055 | } |
| 1056 | DLIB_TEST_MSG(max(abs(subm(mat(imout),rect) - subm(out,rect))) < 1e-5, max(abs(subm(mat(imout),rect) - subm(out,rect)))); |
| 1057 | |
| 1058 | |
| 1059 | assign_all_pixels(imout, 10); |
| 1060 | out = 10; |
| 1061 | rect = spatially_filter_image(img, imout, filt,2,true,true); |
| 1062 | be = border_enumerator(get_rect(imout),rect); |
| 1063 | while (be.move_next()) |
| 1064 | { |
| 1065 | DLIB_TEST(imout[be.element().y()][be.element().x()] == 10); |
| 1066 | } |
| 1067 | out += abs(xcorr_same(mat(img),filt)/2); |
| 1068 | DLIB_TEST(max(abs(subm(mat(imout),rect) - subm(out,rect))) < 1e-7); |
| 1069 | |
| 1070 | |
| 1071 | assign_all_pixels(imout, -10); |
| 1072 | out = -10; |
| 1073 | rect = spatially_filter_image(img, imout, filt,2,false,true); |
| 1074 | be = border_enumerator(get_rect(imout),rect); |
| 1075 | while (be.move_next()) |
| 1076 | { |
| 1077 | DLIB_TEST(imout[be.element().y()][be.element().x()] == -10); |
| 1078 | } |
| 1079 | out += xcorr_same(mat(img),filt)/2; |
| 1080 | DLIB_TEST_MSG(max(abs(subm(mat(imout),rect) - subm(out,rect))) < 1e-5, max(abs(subm(mat(imout),rect) - subm(out,rect)))); |
| 1081 | |
| 1082 | |
| 1083 | |
| 1084 |
no test coverage detected