mirror of
https://github.com/opencv/opencv.git
synced 2024-12-13 16:09:23 +08:00
12738deaef
imgproc: add optimized warpPerspective linear kernels for inputs of type CV_8U/16U/32F+C1/C3/C4 Merge with https://github.com/opencv/opencv_extra/pull/1214 ## Performance ### Apple Mac Mini (M2, 16GB memory) ``` Geometric mean (ms) Name of Test base patch patch vs base (x-factor) WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 8UC1) 0.397 0.119 3.34 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 16UC1) 0.412 0.155 2.65 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 32FC1) 0.382 0.134 2.85 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 8UC3) 0.562 0.201 2.80 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 16UC3) 0.580 0.279 2.07 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 32FC3) 0.477 0.269 1.78 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 8UC4) 0.536 0.221 2.43 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 16UC4) 0.662 0.328 2.02 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 32FC4) 0.511 0.325 1.57 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 8UC1) 0.433 0.171 2.54 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 16UC1) 0.452 0.204 2.21 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 32FC1) 0.410 0.180 2.27 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 8UC3) 0.624 0.243 2.57 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 16UC3) 0.636 0.331 1.92 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 32FC3) 0.511 0.315 1.62 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 8UC4) 0.604 0.281 2.15 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 16UC4) 0.712 0.393 1.81 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 32FC4) 0.552 0.368 1.50 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 8UC1) 0.768 0.214 3.58 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 16UC1) 0.743 0.260 2.86 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 32FC1) 0.722 0.235 3.07 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 8UC3) 1.091 0.333 3.28 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 16UC3) 1.035 0.453 2.29 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 32FC3) 0.955 0.442 2.16 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 8UC4) 1.097 0.364 3.01 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 16UC4) 1.141 0.547 2.09 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 32FC4) 1.015 0.591 1.72 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 8UC1) 1.012 1.006 1.01 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 16UC1) 0.996 1.060 0.94 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 32FC1) 0.930 0.993 0.94 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 8UC3) 1.560 1.260 1.24 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 16UC3) 1.374 1.410 0.97 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 32FC3) 1.212 1.292 0.94 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 8UC4) 1.702 1.354 1.26 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 16UC4) 1.554 1.522 1.02 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 32FC4) 1.342 1.435 0.94 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 8UC1) 1.561 0.364 4.29 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 16UC1) 1.444 0.406 3.56 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 32FC1) 1.423 0.394 3.61 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 8UC3) 2.177 0.533 4.08 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 16UC3) 2.006 0.689 2.91 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 32FC3) 1.907 0.688 2.77 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 8UC4) 2.213 0.581 3.81 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 16UC4) 2.238 0.810 2.76 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 32FC4) 2.072 1.055 1.96 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 8UC1) 2.201 2.908 0.76 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 16UC1) 2.108 2.951 0.71 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 32FC1) 1.997 2.840 0.70 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 8UC3) 3.444 3.293 1.05 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 16UC3) 2.889 3.417 0.85 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 32FC3) 2.671 3.354 0.80 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 8UC4) 3.765 3.767 1.00 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 16UC4) 3.247 3.962 0.82 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 32FC4) 2.993 3.669 0.82 ``` ### Desktop (i7-12700K, 64GB memory) ``` Geometric mean (ms) Name of Test base patch patch vs base (x-factor) WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 8UC1) 0.274 0.076 3.62 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 16UC1) 0.299 0.058 5.14 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 32FC1) 0.299 0.043 6.90 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 8UC3) 0.330 0.115 2.87 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 16UC3) 0.480 0.109 4.39 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 32FC3) 0.608 0.180 3.37 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 8UC4) 0.317 0.143 2.21 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 16UC4) 0.704 0.139 5.07 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 32FC4) 0.508 0.141 3.60 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 8UC1) 0.293 0.064 4.57 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 16UC1) 0.311 0.061 5.07 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 32FC1) 0.299 0.057 5.29 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 8UC3) 0.373 0.135 2.75 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 16UC3) 0.501 0.129 3.87 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 32FC3) 0.403 0.123 3.26 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 8UC4) 0.372 0.163 2.28 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 16UC4) 0.582 0.161 3.63 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 32FC4) 0.459 0.152 3.03 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 8UC1) 0.558 0.099 5.63 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 16UC1) 0.607 0.098 6.20 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 32FC1) 0.599 0.090 6.65 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 8UC3) 0.636 0.198 3.22 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 16UC3) 0.806 0.187 4.31 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 32FC3) 1.329 0.227 5.85 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 8UC4) 0.643 0.238 2.70 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 16UC4) 1.401 0.233 6.02 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 32FC4) 1.083 0.229 4.72 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 8UC1) 0.682 0.358 1.91 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 16UC1) 0.695 0.350 1.99 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 32FC1) 0.666 0.334 2.00 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 8UC3) 0.895 0.502 1.78 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 16UC3) 1.035 0.492 2.11 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 32FC3) 0.924 0.466 1.98 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 8UC4) 0.969 0.551 1.76 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 16UC4) 1.201 0.550 2.18 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 32FC4) 0.948 0.544 1.74 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 8UC1) 1.018 0.174 5.84 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 16UC1) 0.973 0.173 5.63 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 32FC1) 1.002 0.164 6.13 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 8UC3) 1.100 0.297 3.71 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 16UC3) 1.197 0.278 4.30 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 32FC3) 3.108 0.296 10.49 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 8UC4) 1.086 0.340 3.20 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 16UC4) 2.987 0.336 8.88 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 32FC4) 2.249 0.835 2.69 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 8UC1) 1.330 1.007 1.32 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 16UC1) 1.352 0.974 1.39 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 32FC1) 1.241 0.933 1.33 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 8UC3) 1.896 1.287 1.47 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 16UC3) 2.071 1.288 1.61 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 32FC3) 1.870 1.211 1.54 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 8UC4) 2.059 1.362 1.51 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 16UC4) 2.366 1.395 1.70 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 32FC4) 1.859 1.416 1.31 ``` ### Khadas VIM3 (A311D, 4xA73+2xA53, no fp16 vector intrinsics support, 4GB memory) ``` Geometric mean (ms) Name of Test base patch patch vs base (x-factor) WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 8UC1) 2.543 0.702 3.62 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 16UC1) 3.175 0.727 4.37 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 32FC1) 2.877 0.777 3.70 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 8UC3) 4.059 1.192 3.41 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 16UC3) 4.689 1.642 2.86 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 32FC3) 4.071 2.064 1.97 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 8UC4) 3.897 1.501 2.60 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 16UC4) 5.485 2.106 2.60 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_CONSTANT, 32FC4) 4.611 2.938 1.57 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 8UC1) 2.717 0.912 2.98 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 16UC1) 3.426 0.885 3.87 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 32FC1) 3.009 0.979 3.07 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 8UC3) 4.409 1.488 2.96 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 16UC3) 5.236 1.901 2.75 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 32FC3) 4.445 2.232 1.99 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 8UC4) 4.400 1.816 2.42 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 16UC4) 6.211 2.390 2.60 WarpPerspective::TestWarpPerspective::(640x480, INTER_LINEAR, BORDER_REPLICATE, 32FC4) 4.779 3.154 1.52 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 8UC1) 5.487 1.599 3.43 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 16UC1) 6.589 1.652 3.99 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 32FC1) 4.916 1.779 2.76 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 8UC3) 7.676 2.465 3.11 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 16UC3) 8.783 3.020 2.91 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 32FC3) 8.468 4.314 1.96 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 8UC4) 7.670 2.944 2.60 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 16UC4) 9.364 3.871 2.42 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_CONSTANT, 32FC4) 9.297 5.770 1.61 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 8UC1) 6.809 5.359 1.27 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 16UC1) 9.010 4.745 1.90 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 32FC1) 8.501 4.712 1.80 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 8UC3) 10.652 7.345 1.45 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 16UC3) 12.319 7.647 1.61 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 32FC3) 10.466 7.849 1.33 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 8UC4) 11.659 8.226 1.42 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 16UC4) 13.157 8.825 1.49 WarpPerspective::TestWarpPerspective::(1280x720, INTER_LINEAR, BORDER_REPLICATE, 32FC4) 11.557 9.869 1.17 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 8UC1) 14.773 3.081 4.79 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 16UC1) 14.971 3.135 4.78 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 32FC1) 14.795 3.321 4.45 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 8UC3) 20.823 4.319 4.82 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 16UC3) 22.128 5.029 4.40 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 32FC3) 22.583 8.036 2.81 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 8UC4) 20.141 5.018 4.01 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 16UC4) 23.505 6.132 3.83 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_CONSTANT, 32FC4) 20.226 10.050 2.01 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 8UC1) 18.904 15.189 1.24 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 16UC1) 22.749 12.979 1.75 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 32FC1) 19.685 12.981 1.52 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 8UC3) 29.636 19.974 1.48 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 16UC3) 36.266 19.563 1.85 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 32FC3) 30.124 19.434 1.55 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 8UC4) 34.290 21.998 1.56 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 16UC4) 41.765 21.705 1.92 WarpPerspective::TestWarpPerspective::(1920x1080, INTER_LINEAR, BORDER_REPLICATE, 32FC4) 27.767 22.838 1.22 ``` ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
1555 lines
51 KiB
C++
1555 lines
51 KiB
C++
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
//
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
// If you do not agree to this license, do not download, install,
|
|
// copy or use the software.
|
|
//
|
|
//
|
|
// Intel License Agreement
|
|
// For Open Source Computer Vision Library
|
|
//
|
|
// Copyright (C) 2000, Intel Corporation, all rights reserved.
|
|
// Third party copyrights are property of their respective owners.
|
|
//
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
// are permitted provided that the following conditions are met:
|
|
//
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
// this list of conditions and the following disclaimer.
|
|
//
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
// and/or other materials provided with the distribution.
|
|
//
|
|
// * The name of Intel Corporation may not be used to endorse or promote products
|
|
// derived from this software without specific prior written permission.
|
|
//
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
//
|
|
//M*/
|
|
|
|
#include "test_precomp.hpp"
|
|
|
|
namespace opencv_test { namespace {
|
|
|
|
void __wrap_printf_func(const char* fmt, ...)
|
|
{
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
char buffer[256];
|
|
vsnprintf (buffer, sizeof(buffer), fmt, args);
|
|
cvtest::TS::ptr()->printf(cvtest::TS::SUMMARY, buffer);
|
|
va_end(args);
|
|
}
|
|
|
|
#define PRINT_TO_LOG __wrap_printf_func
|
|
|
|
#define SHOW_IMAGE
|
|
#undef SHOW_IMAGE
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
// ImageWarpBaseTest
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
class CV_ImageWarpBaseTest :
|
|
public cvtest::BaseTest
|
|
{
|
|
public:
|
|
enum { cell_size = 10 };
|
|
|
|
CV_ImageWarpBaseTest();
|
|
virtual ~CV_ImageWarpBaseTest();
|
|
|
|
virtual void run(int);
|
|
protected:
|
|
virtual void generate_test_data();
|
|
|
|
virtual void run_func() = 0;
|
|
virtual void run_reference_func() = 0;
|
|
virtual float get_success_error_level(int _interpolation, int _depth) const;
|
|
virtual void validate_results() const;
|
|
virtual void prepare_test_data_for_reference_func();
|
|
|
|
Size randSize(RNG& rng) const;
|
|
|
|
String interpolation_to_string(int inter_type) const;
|
|
|
|
int interpolation;
|
|
Mat src;
|
|
Mat dst;
|
|
Mat reference_dst;
|
|
};
|
|
|
|
CV_ImageWarpBaseTest::CV_ImageWarpBaseTest() :
|
|
BaseTest(), interpolation(-1),
|
|
src(), dst(), reference_dst()
|
|
{
|
|
test_case_count = 40;
|
|
ts->set_failed_test_info(cvtest::TS::OK);
|
|
}
|
|
|
|
CV_ImageWarpBaseTest::~CV_ImageWarpBaseTest()
|
|
{
|
|
}
|
|
|
|
String CV_ImageWarpBaseTest::interpolation_to_string(int inter) const
|
|
{
|
|
bool inverse = (inter & WARP_INVERSE_MAP) != 0;
|
|
inter &= ~WARP_INVERSE_MAP;
|
|
String str;
|
|
|
|
if (inter == INTER_NEAREST)
|
|
str = "INTER_NEAREST";
|
|
else if (inter == INTER_LINEAR)
|
|
str = "INTER_LINEAR";
|
|
else if (inter == INTER_LINEAR_EXACT)
|
|
str = "INTER_LINEAR_EXACT";
|
|
else if (inter == INTER_AREA)
|
|
str = "INTER_AREA";
|
|
else if (inter == INTER_CUBIC)
|
|
str = "INTER_CUBIC";
|
|
else if (inter == INTER_LANCZOS4)
|
|
str = "INTER_LANCZOS4";
|
|
else if (inter == INTER_LANCZOS4 + 1)
|
|
str = "INTER_AREA_FAST";
|
|
|
|
if (inverse)
|
|
str += " | WARP_INVERSE_MAP";
|
|
|
|
return str.empty() ? "Unsupported/Unknown interpolation type" : str;
|
|
}
|
|
|
|
Size CV_ImageWarpBaseTest::randSize(RNG& rng) const
|
|
{
|
|
Size size;
|
|
size.width = static_cast<int>(std::exp(rng.uniform(1.0f, 7.0f)));
|
|
size.height = static_cast<int>(std::exp(rng.uniform(1.0f, 7.0f)));
|
|
|
|
return size;
|
|
}
|
|
|
|
void CV_ImageWarpBaseTest::generate_test_data()
|
|
{
|
|
RNG& rng = ts->get_rng();
|
|
|
|
// generating the src matrix structure
|
|
Size ssize = randSize(rng), dsize;
|
|
|
|
int depth = rng.uniform(0, CV_64F);
|
|
while (depth == CV_8S || depth == CV_32S)
|
|
depth = rng.uniform(0, CV_64F);
|
|
|
|
int cn = rng.uniform(1, 5);
|
|
|
|
src.create(ssize, CV_MAKE_TYPE(depth, cn));
|
|
|
|
// generating the src matrix
|
|
int x, y;
|
|
if (cvtest::randInt(rng) % 2)
|
|
{
|
|
for (y = 0; y < ssize.height; y += cell_size)
|
|
for (x = 0; x < ssize.width; x += cell_size)
|
|
rectangle(src, Point(x, y), Point(x + std::min<int>(cell_size, ssize.width - x), y +
|
|
std::min<int>(cell_size, ssize.height - y)), Scalar::all((x + y) % 2 ? 255: 0), cv::FILLED);
|
|
}
|
|
else
|
|
{
|
|
src = Scalar::all(255);
|
|
for (y = cell_size; y < src.rows; y += cell_size)
|
|
line(src, Point2i(0, y), Point2i(src.cols, y), Scalar::all(0), 1);
|
|
for (x = cell_size; x < src.cols; x += cell_size)
|
|
line(src, Point2i(x, 0), Point2i(x, src.rows), Scalar::all(0), 1);
|
|
}
|
|
|
|
// generating an interpolation type
|
|
interpolation = rng.uniform(0, cv::INTER_LANCZOS4 + 1);
|
|
|
|
// generating the dst matrix structure
|
|
double scale_x, scale_y;
|
|
if (interpolation == INTER_AREA)
|
|
{
|
|
bool area_fast = rng.uniform(0., 1.) > 0.5;
|
|
if (area_fast)
|
|
{
|
|
scale_x = rng.uniform(2, 5);
|
|
scale_y = rng.uniform(2, 5);
|
|
}
|
|
else
|
|
{
|
|
scale_x = rng.uniform(1.0, 3.0);
|
|
scale_y = rng.uniform(1.0, 3.0);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
scale_x = rng.uniform(0.4, 4.0);
|
|
scale_y = rng.uniform(0.4, 4.0);
|
|
}
|
|
CV_Assert(scale_x > 0.0f && scale_y > 0.0f);
|
|
|
|
dsize.width = saturate_cast<int>((ssize.width + scale_x - 1) / scale_x);
|
|
dsize.height = saturate_cast<int>((ssize.height + scale_y - 1) / scale_y);
|
|
|
|
dst = Mat::zeros(dsize, src.type());
|
|
reference_dst = Mat::zeros(dst.size(), CV_MAKE_TYPE(CV_32F, dst.channels()));
|
|
|
|
scale_x = src.cols / static_cast<double>(dst.cols);
|
|
scale_y = src.rows / static_cast<double>(dst.rows);
|
|
|
|
if (interpolation == INTER_AREA && (scale_x < 1.0 || scale_y < 1.0))
|
|
interpolation = INTER_LINEAR;
|
|
}
|
|
|
|
void CV_ImageWarpBaseTest::run(int)
|
|
{
|
|
for (int i = 0; i < test_case_count; ++i)
|
|
{
|
|
generate_test_data();
|
|
run_func();
|
|
run_reference_func();
|
|
if (ts->get_err_code() < 0)
|
|
break;
|
|
validate_results();
|
|
if (ts->get_err_code() < 0)
|
|
break;
|
|
ts->update_context(this, i, true);
|
|
}
|
|
ts->set_gtest_status();
|
|
}
|
|
|
|
float CV_ImageWarpBaseTest::get_success_error_level(int _interpolation, int) const
|
|
{
|
|
if (_interpolation == INTER_CUBIC)
|
|
return 1.0f;
|
|
else if (_interpolation == INTER_LANCZOS4)
|
|
return 1.0f;
|
|
else if (_interpolation == INTER_NEAREST)
|
|
return 255.0f; // FIXIT: check is not reliable for Black/White (0/255) images
|
|
else if (_interpolation == INTER_AREA)
|
|
return 2.0f;
|
|
else
|
|
return 1.0f;
|
|
}
|
|
|
|
void CV_ImageWarpBaseTest::validate_results() const
|
|
{
|
|
Mat _dst;
|
|
dst.convertTo(_dst, reference_dst.depth());
|
|
|
|
Size dsize = dst.size(), ssize = src.size();
|
|
int cn = _dst.channels();
|
|
dsize.width *= cn;
|
|
float t = get_success_error_level(interpolation & INTER_MAX, dst.depth());
|
|
|
|
for (int dy = 0; dy < dsize.height; ++dy)
|
|
{
|
|
const float* rD = reference_dst.ptr<float>(dy);
|
|
const float* D = _dst.ptr<float>(dy);
|
|
|
|
for (int dx = 0; dx < dsize.width; ++dx)
|
|
if (fabs(rD[dx] - D[dx]) > t &&
|
|
// fabs(rD[dx] - D[dx]) < 250.0f &&
|
|
rD[dx] <= 255.0f && D[dx] <= 255.0f && rD[dx] >= 0.0f && D[dx] >= 0.0f)
|
|
{
|
|
PRINT_TO_LOG("\nNorm of the difference: %lf\n", cvtest::norm(reference_dst, _dst, NORM_INF));
|
|
PRINT_TO_LOG("Error in (dx, dy): (%d, %d)\n", dx / cn + 1, dy + 1);
|
|
PRINT_TO_LOG("Tuple (rD, D): (%f, %f)\n", rD[dx], D[dx]);
|
|
PRINT_TO_LOG("Dsize: (%d, %d)\n", dsize.width / cn, dsize.height);
|
|
PRINT_TO_LOG("Ssize: (%d, %d)\n", src.cols, src.rows);
|
|
|
|
double scale_x = static_cast<double>(ssize.width) / dsize.width;
|
|
double scale_y = static_cast<double>(ssize.height) / dsize.height;
|
|
bool area_fast = interpolation == INTER_AREA &&
|
|
fabs(scale_x - cvRound(scale_x)) < FLT_EPSILON &&
|
|
fabs(scale_y - cvRound(scale_y)) < FLT_EPSILON;
|
|
if (area_fast)
|
|
{
|
|
scale_y = cvRound(scale_y);
|
|
scale_x = cvRound(scale_x);
|
|
}
|
|
|
|
PRINT_TO_LOG("Interpolation: %s\n", interpolation_to_string(area_fast ? INTER_LANCZOS4 + 1 : interpolation).c_str());
|
|
PRINT_TO_LOG("Scale (x, y): (%lf, %lf)\n", scale_x, scale_y);
|
|
PRINT_TO_LOG("Elemsize: %d\n", src.elemSize1());
|
|
PRINT_TO_LOG("Channels: %d\n", cn);
|
|
|
|
#ifdef SHOW_IMAGE
|
|
const std::string w1("OpenCV impl (run func)"), w2("Reference func"), w3("Src image"), w4("Diff");
|
|
namedWindow(w1, cv::WINDOW_KEEPRATIO);
|
|
namedWindow(w2, cv::WINDOW_KEEPRATIO);
|
|
namedWindow(w3, cv::WINDOW_KEEPRATIO);
|
|
namedWindow(w4, cv::WINDOW_KEEPRATIO);
|
|
|
|
Mat diff;
|
|
absdiff(reference_dst, _dst, diff);
|
|
|
|
imshow(w1, dst);
|
|
imshow(w2, reference_dst);
|
|
imshow(w3, src);
|
|
imshow(w4, diff);
|
|
|
|
waitKey();
|
|
#endif
|
|
|
|
const int radius = 3;
|
|
int rmin = MAX(dy - radius, 0), rmax = MIN(dy + radius, dsize.height);
|
|
int cmin = MAX(dx / cn - radius, 0), cmax = MIN(dx / cn + radius, dsize.width);
|
|
|
|
std::cout << "opencv result:\n" << dst(Range(rmin, rmax), Range(cmin, cmax)) << std::endl;
|
|
std::cout << "reference result:\n" << reference_dst(Range(rmin, rmax), Range(cmin, cmax)) << std::endl;
|
|
|
|
ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void CV_ImageWarpBaseTest::prepare_test_data_for_reference_func()
|
|
{
|
|
if (src.depth() != CV_32F)
|
|
{
|
|
Mat tmp;
|
|
src.convertTo(tmp, CV_32F);
|
|
src = tmp;
|
|
}
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
// Resize
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
class CV_Resize_Test :
|
|
public CV_ImageWarpBaseTest
|
|
{
|
|
public:
|
|
CV_Resize_Test();
|
|
virtual ~CV_Resize_Test();
|
|
|
|
protected:
|
|
virtual void generate_test_data();
|
|
|
|
virtual void run_func();
|
|
virtual void run_reference_func();
|
|
|
|
private:
|
|
double scale_x;
|
|
double scale_y;
|
|
bool area_fast;
|
|
|
|
void resize_generic();
|
|
void resize_area();
|
|
double getWeight(double a, double b, int x);
|
|
|
|
typedef std::vector<std::pair<int, double> > dim;
|
|
void generate_buffer(double scale, dim& _dim);
|
|
void resize_1d(const Mat& _src, Mat& _dst, int dy, const dim& _dim);
|
|
};
|
|
|
|
CV_Resize_Test::CV_Resize_Test() :
|
|
CV_ImageWarpBaseTest(), scale_x(),
|
|
scale_y(), area_fast(false)
|
|
{
|
|
}
|
|
|
|
CV_Resize_Test::~CV_Resize_Test()
|
|
{
|
|
}
|
|
|
|
namespace
|
|
{
|
|
void interpolateLinear(float x, float* coeffs)
|
|
{
|
|
coeffs[0] = 1.f - x;
|
|
coeffs[1] = x;
|
|
}
|
|
|
|
void interpolateCubic(float x, float* coeffs)
|
|
{
|
|
const float A = -0.75f;
|
|
|
|
coeffs[0] = ((A*(x + 1) - 5*A)*(x + 1) + 8*A)*(x + 1) - 4*A;
|
|
coeffs[1] = ((A + 2)*x - (A + 3))*x*x + 1;
|
|
coeffs[2] = ((A + 2)*(1 - x) - (A + 3))*(1 - x)*(1 - x) + 1;
|
|
coeffs[3] = 1.f - coeffs[0] - coeffs[1] - coeffs[2];
|
|
}
|
|
|
|
void interpolateLanczos4(float x, float* coeffs)
|
|
{
|
|
static const double s45 = 0.70710678118654752440084436210485;
|
|
static const double cs[][2]=
|
|
{{1, 0}, {-s45, -s45}, {0, 1}, {s45, -s45}, {-1, 0}, {s45, s45}, {0, -1}, {-s45, s45}};
|
|
|
|
if( x < FLT_EPSILON )
|
|
{
|
|
for( int i = 0; i < 8; i++ )
|
|
coeffs[i] = 0;
|
|
coeffs[3] = 1;
|
|
return;
|
|
}
|
|
|
|
float sum = 0;
|
|
double y0=-(x+3)*CV_PI*0.25, s0 = sin(y0), c0=cos(y0);
|
|
for(int i = 0; i < 8; i++ )
|
|
{
|
|
double y = -(x+3-i)*CV_PI*0.25;
|
|
coeffs[i] = (float)((cs[i][0]*s0 + cs[i][1]*c0)/(y*y));
|
|
sum += coeffs[i];
|
|
}
|
|
|
|
sum = 1.f/sum;
|
|
for(int i = 0; i < 8; i++ )
|
|
coeffs[i] *= sum;
|
|
}
|
|
|
|
typedef void (*interpolate_method)(float x, float* coeffs);
|
|
interpolate_method inter_array[] = { &interpolateLinear, &interpolateCubic, &interpolateLanczos4 };
|
|
}
|
|
|
|
void CV_Resize_Test::generate_test_data()
|
|
{
|
|
RNG& rng = ts->get_rng();
|
|
|
|
// generating the src matrix structure
|
|
Size ssize = randSize(rng), dsize;
|
|
|
|
int depth = rng.uniform(0, CV_64F);
|
|
while (depth == CV_8S || depth == CV_32S)
|
|
depth = rng.uniform(0, CV_64F);
|
|
|
|
int cn = rng.uniform(1, 4);
|
|
|
|
src.create(ssize, CV_MAKE_TYPE(depth, cn));
|
|
|
|
// generating the src matrix
|
|
int x, y;
|
|
if (cvtest::randInt(rng) % 2)
|
|
{
|
|
for (y = 0; y < ssize.height; y += cell_size)
|
|
for (x = 0; x < ssize.width; x += cell_size)
|
|
rectangle(src, Point(x, y), Point(x + std::min<int>(cell_size, ssize.width - x), y +
|
|
std::min<int>(cell_size, ssize.height - y)), Scalar::all((x + y) % 2 ? 255: 0), cv::FILLED);
|
|
}
|
|
else
|
|
{
|
|
src = Scalar::all(255);
|
|
for (y = cell_size; y < src.rows; y += cell_size)
|
|
line(src, Point2i(0, y), Point2i(src.cols, y), Scalar::all(0), 1);
|
|
for (x = cell_size; x < src.cols; x += cell_size)
|
|
line(src, Point2i(x, 0), Point2i(x, src.rows), Scalar::all(0), 1);
|
|
}
|
|
|
|
// generating an interpolation type
|
|
interpolation = rng.uniform(0, cv::INTER_MAX - 1);
|
|
|
|
// generating the dst matrix structure
|
|
if (interpolation == INTER_AREA)
|
|
{
|
|
area_fast = rng.uniform(0., 1.) > 0.5;
|
|
if (area_fast)
|
|
{
|
|
scale_x = rng.uniform(2, 5);
|
|
scale_y = rng.uniform(2, 5);
|
|
}
|
|
else
|
|
{
|
|
scale_x = rng.uniform(1.0, 3.0);
|
|
scale_y = rng.uniform(1.0, 3.0);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
scale_x = rng.uniform(0.4, 4.0);
|
|
scale_y = rng.uniform(0.4, 4.0);
|
|
}
|
|
CV_Assert(scale_x > 0.0f && scale_y > 0.0f);
|
|
|
|
dsize.width = saturate_cast<int>((ssize.width + scale_x - 1) / scale_x);
|
|
dsize.height = saturate_cast<int>((ssize.height + scale_y - 1) / scale_y);
|
|
|
|
dst = Mat::zeros(dsize, src.type());
|
|
reference_dst = Mat::zeros(dst.size(), CV_MAKE_TYPE(CV_32F, dst.channels()));
|
|
|
|
scale_x = src.cols / static_cast<double>(dst.cols);
|
|
scale_y = src.rows / static_cast<double>(dst.rows);
|
|
|
|
if (interpolation == INTER_AREA && (scale_x < 1.0 || scale_y < 1.0))
|
|
interpolation = INTER_LINEAR_EXACT;
|
|
if (interpolation == INTER_LINEAR_EXACT && (depth == CV_32F || depth == CV_64F))
|
|
interpolation = INTER_LINEAR;
|
|
|
|
area_fast = interpolation == INTER_AREA &&
|
|
fabs(scale_x - cvRound(scale_x)) < FLT_EPSILON &&
|
|
fabs(scale_y - cvRound(scale_y)) < FLT_EPSILON;
|
|
if (area_fast)
|
|
{
|
|
scale_x = cvRound(scale_x);
|
|
scale_y = cvRound(scale_y);
|
|
}
|
|
}
|
|
|
|
void CV_Resize_Test::run_func()
|
|
{
|
|
cv::resize(src, dst, dst.size(), 0, 0, interpolation);
|
|
}
|
|
|
|
void CV_Resize_Test::run_reference_func()
|
|
{
|
|
CV_ImageWarpBaseTest::prepare_test_data_for_reference_func();
|
|
|
|
if (interpolation == INTER_AREA)
|
|
resize_area();
|
|
else
|
|
resize_generic();
|
|
}
|
|
|
|
double CV_Resize_Test::getWeight(double a, double b, int x)
|
|
{
|
|
double w = std::min(static_cast<double>(x + 1), b) - std::max(static_cast<double>(x), a);
|
|
CV_Assert(w >= 0);
|
|
return w;
|
|
}
|
|
|
|
void CV_Resize_Test::resize_area()
|
|
{
|
|
Size ssize = src.size(), dsize = reference_dst.size();
|
|
CV_Assert(!ssize.empty() && !dsize.empty());
|
|
int cn = src.channels();
|
|
|
|
CV_Assert(scale_x >= 1.0 && scale_y >= 1.0);
|
|
|
|
double fsy0 = 0, fsy1 = scale_y;
|
|
for (int dy = 0; dy < dsize.height; ++dy)
|
|
{
|
|
float* yD = reference_dst.ptr<float>(dy);
|
|
int isy0 = cvFloor(fsy0), isy1 = std::min(cvFloor(fsy1), ssize.height - 1);
|
|
CV_Assert(isy1 <= ssize.height && isy0 < ssize.height);
|
|
|
|
double fsx0 = 0, fsx1 = scale_x;
|
|
|
|
for (int dx = 0; dx < dsize.width; ++dx)
|
|
{
|
|
float* xyD = yD + cn * dx;
|
|
int isx0 = cvFloor(fsx0), isx1 = std::min(ssize.width - 1, cvFloor(fsx1));
|
|
|
|
CV_Assert(isx1 <= ssize.width);
|
|
CV_Assert(isx0 < ssize.width);
|
|
|
|
// for each pixel of dst
|
|
for (int r = 0; r < cn; ++r)
|
|
{
|
|
xyD[r] = 0.0f;
|
|
double area = 0.0;
|
|
for (int sy = isy0; sy <= isy1; ++sy)
|
|
{
|
|
const float* yS = src.ptr<float>(sy);
|
|
for (int sx = isx0; sx <= isx1; ++sx)
|
|
{
|
|
double wy = getWeight(fsy0, fsy1, sy);
|
|
double wx = getWeight(fsx0, fsx1, sx);
|
|
double w = wx * wy;
|
|
xyD[r] += static_cast<float>(yS[sx * cn + r] * w);
|
|
area += w;
|
|
}
|
|
}
|
|
|
|
CV_Assert(area != 0);
|
|
// norming pixel
|
|
xyD[r] = static_cast<float>(xyD[r] / area);
|
|
}
|
|
fsx1 = std::min((fsx0 = fsx1) + scale_x, static_cast<double>(ssize.width));
|
|
}
|
|
fsy1 = std::min((fsy0 = fsy1) + scale_y, static_cast<double>(ssize.height));
|
|
}
|
|
}
|
|
|
|
// for interpolation type : INTER_LINEAR, INTER_LINEAR_EXACT, INTER_CUBIC, INTER_LANCZOS4
|
|
void CV_Resize_Test::resize_1d(const Mat& _src, Mat& _dst, int dy, const dim& _dim)
|
|
{
|
|
Size dsize = _dst.size();
|
|
int cn = _dst.channels();
|
|
float* yD = _dst.ptr<float>(dy);
|
|
|
|
if (interpolation == INTER_NEAREST)
|
|
{
|
|
const float* yS = _src.ptr<float>(dy);
|
|
for (int dx = 0; dx < dsize.width; ++dx)
|
|
{
|
|
int isx = _dim[dx].first;
|
|
const float* xyS = yS + isx * cn;
|
|
float* xyD = yD + dx * cn;
|
|
|
|
for (int r = 0; r < cn; ++r)
|
|
xyD[r] = xyS[r];
|
|
}
|
|
}
|
|
else if (interpolation == INTER_LINEAR || interpolation == INTER_LINEAR_EXACT || interpolation == INTER_CUBIC || interpolation == INTER_LANCZOS4)
|
|
{
|
|
interpolate_method inter_func = inter_array[interpolation - (interpolation == INTER_LANCZOS4 ? 2 : interpolation == INTER_LINEAR_EXACT ? 5 : 1)];
|
|
size_t elemsize = _src.elemSize();
|
|
|
|
int ofs = 0, ksize = 2;
|
|
if (interpolation == INTER_CUBIC)
|
|
ofs = 1, ksize = 4;
|
|
else if (interpolation == INTER_LANCZOS4)
|
|
ofs = 3, ksize = 8;
|
|
|
|
Mat _extended_src_row(1, _src.cols + ksize * 2, _src.type());
|
|
const uchar* srow = _src.ptr(dy);
|
|
memcpy(_extended_src_row.ptr() + elemsize * ksize, srow, _src.step);
|
|
for (int k = 0; k < ksize; ++k)
|
|
{
|
|
memcpy(_extended_src_row.ptr() + k * elemsize, srow, elemsize);
|
|
memcpy(_extended_src_row.ptr() + (ksize + k) * elemsize + _src.step, srow + _src.step - elemsize, elemsize);
|
|
}
|
|
|
|
for (int dx = 0; dx < dsize.width; ++dx)
|
|
{
|
|
int isx = _dim[dx].first;
|
|
double fsx = _dim[dx].second;
|
|
|
|
float *xyD = yD + dx * cn;
|
|
const float* xyS = _extended_src_row.ptr<float>(0) + (isx + ksize - ofs) * cn;
|
|
|
|
float w[8];
|
|
inter_func(static_cast<float>(fsx), w);
|
|
|
|
for (int r = 0; r < cn; ++r)
|
|
{
|
|
xyD[r] = 0;
|
|
for (int k = 0; k < ksize; ++k)
|
|
xyD[r] += w[k] * xyS[k * cn + r];
|
|
}
|
|
}
|
|
}
|
|
else
|
|
CV_Assert(0);
|
|
}
|
|
|
|
void CV_Resize_Test::generate_buffer(double scale, dim& _dim)
|
|
{
|
|
size_t length = _dim.size();
|
|
for (size_t dx = 0; dx < length; ++dx)
|
|
{
|
|
double fsx = scale * (dx + 0.5) - 0.5;
|
|
int isx = cvFloor(fsx);
|
|
_dim[dx] = std::make_pair(isx, fsx - isx);
|
|
}
|
|
}
|
|
|
|
void CV_Resize_Test::resize_generic()
|
|
{
|
|
Size dsize = reference_dst.size(), ssize = src.size();
|
|
CV_Assert(!dsize.empty() && !ssize.empty());
|
|
|
|
dim dims[] = { dim(dsize.width), dim(dsize.height) };
|
|
if (interpolation == INTER_NEAREST)
|
|
{
|
|
for (int dx = 0; dx < dsize.width; ++dx)
|
|
dims[0][dx].first = std::min(cvFloor(dx * scale_x), ssize.width - 1);
|
|
for (int dy = 0; dy < dsize.height; ++dy)
|
|
dims[1][dy].first = std::min(cvFloor(dy * scale_y), ssize.height - 1);
|
|
}
|
|
else
|
|
{
|
|
generate_buffer(scale_x, dims[0]);
|
|
generate_buffer(scale_y, dims[1]);
|
|
}
|
|
|
|
Mat tmp(ssize.height, dsize.width, reference_dst.type());
|
|
for (int dy = 0; dy < tmp.rows; ++dy)
|
|
resize_1d(src, tmp, dy, dims[0]);
|
|
|
|
cv::Mat tmp_t(tmp.cols, tmp.rows, tmp.type());
|
|
cvtest::transpose(tmp, tmp_t);
|
|
cv::Mat reference_dst_t(reference_dst.cols, reference_dst.rows, reference_dst.type());
|
|
cvtest::transpose(reference_dst, reference_dst_t);
|
|
|
|
for (int dy = 0; dy < tmp_t.rows; ++dy)
|
|
resize_1d(tmp_t, reference_dst_t, dy, dims[1]);
|
|
|
|
cvtest::transpose(reference_dst_t, reference_dst);
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
// remap
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
class CV_Remap_Test :
|
|
public CV_ImageWarpBaseTest
|
|
{
|
|
public:
|
|
CV_Remap_Test();
|
|
|
|
virtual ~CV_Remap_Test();
|
|
|
|
private:
|
|
typedef void (CV_Remap_Test::*remap_func)(const Mat&, Mat&);
|
|
|
|
protected:
|
|
virtual void generate_test_data();
|
|
virtual void prepare_test_data_for_reference_func();
|
|
|
|
virtual void run_func();
|
|
virtual void run_reference_func();
|
|
|
|
Mat mapx, mapy;
|
|
int borderType;
|
|
Scalar borderValue;
|
|
|
|
remap_func funcs[2];
|
|
|
|
private:
|
|
void remap_nearest(const Mat&, Mat&);
|
|
void remap_generic(const Mat&, Mat&);
|
|
|
|
void convert_maps();
|
|
const char* borderType_to_string() const;
|
|
virtual void validate_results() const;
|
|
};
|
|
|
|
CV_Remap_Test::CV_Remap_Test() :
|
|
CV_ImageWarpBaseTest(), borderType(-1)
|
|
{
|
|
funcs[0] = &CV_Remap_Test::remap_nearest;
|
|
funcs[1] = &CV_Remap_Test::remap_generic;
|
|
}
|
|
|
|
CV_Remap_Test::~CV_Remap_Test()
|
|
{
|
|
}
|
|
|
|
void CV_Remap_Test::generate_test_data()
|
|
{
|
|
CV_ImageWarpBaseTest::generate_test_data();
|
|
|
|
RNG& rng = ts->get_rng();
|
|
borderType = rng.uniform(1, BORDER_WRAP);
|
|
borderValue = Scalar::all(rng.uniform(0, 255));
|
|
|
|
// generating the mapx, mapy matrices
|
|
static const int mapx_types[] = { CV_16SC2, CV_32FC1, CV_32FC2 };
|
|
mapx.create(dst.size(), mapx_types[rng.uniform(0, sizeof(mapx_types) / sizeof(int))]);
|
|
mapy.release();
|
|
|
|
const int n = std::min(std::min(src.cols, src.rows) / 10 + 1, 2);
|
|
float _n = 0; //static_cast<float>(-n);
|
|
|
|
switch (mapx.type())
|
|
{
|
|
case CV_16SC2:
|
|
{
|
|
MatIterator_<Vec2s> begin_x = mapx.begin<Vec2s>(), end_x = mapx.end<Vec2s>();
|
|
for ( ; begin_x != end_x; ++begin_x)
|
|
{
|
|
(*begin_x)[0] = static_cast<short>(rng.uniform(static_cast<int>(_n), std::max(src.cols + n - 1, 0)));
|
|
(*begin_x)[1] = static_cast<short>(rng.uniform(static_cast<int>(_n), std::max(src.rows + n - 1, 0)));
|
|
}
|
|
|
|
if (interpolation != INTER_NEAREST)
|
|
{
|
|
static const int mapy_types[] = { CV_16UC1, CV_16SC1 };
|
|
mapy.create(dst.size(), mapy_types[rng.uniform(0, sizeof(mapy_types) / sizeof(int))]);
|
|
|
|
switch (mapy.type())
|
|
{
|
|
case CV_16UC1:
|
|
{
|
|
MatIterator_<ushort> begin_y = mapy.begin<ushort>(), end_y = mapy.end<ushort>();
|
|
for ( ; begin_y != end_y; ++begin_y)
|
|
*begin_y = static_cast<ushort>(rng.uniform(0, 1024));
|
|
}
|
|
break;
|
|
|
|
case CV_16SC1:
|
|
{
|
|
MatIterator_<short> begin_y = mapy.begin<short>(), end_y = mapy.end<short>();
|
|
for ( ; begin_y != end_y; ++begin_y)
|
|
*begin_y = static_cast<short>(rng.uniform(0, 1024));
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
|
|
case CV_32FC1:
|
|
{
|
|
mapy.create(dst.size(), CV_32FC1);
|
|
float fscols = static_cast<float>(std::max(src.cols - 1 + n, 0)),
|
|
fsrows = static_cast<float>(std::max(src.rows - 1 + n, 0));
|
|
MatIterator_<float> begin_x = mapx.begin<float>(), end_x = mapx.end<float>();
|
|
MatIterator_<float> begin_y = mapy.begin<float>();
|
|
for ( ; begin_x != end_x; ++begin_x, ++begin_y)
|
|
{
|
|
*begin_x = rng.uniform(_n, fscols);
|
|
*begin_y = rng.uniform(_n, fsrows);
|
|
}
|
|
}
|
|
break;
|
|
|
|
case CV_32FC2:
|
|
{
|
|
float fscols = static_cast<float>(std::max(src.cols - 1 + n, 0)),
|
|
fsrows = static_cast<float>(std::max(src.rows - 1 + n, 0));
|
|
int width = mapx.cols << 1;
|
|
|
|
for (int y = 0; y < mapx.rows; ++y)
|
|
{
|
|
float * ptr = mapx.ptr<float>(y);
|
|
|
|
for (int x = 0; x < width; x += 2)
|
|
{
|
|
ptr[x] = rng.uniform(_n, fscols);
|
|
ptr[x + 1] = rng.uniform(_n, fsrows);
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
|
|
default:
|
|
CV_Assert(0);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void CV_Remap_Test::run_func()
|
|
{
|
|
remap(src, dst, mapx, mapy, interpolation, borderType, borderValue);
|
|
}
|
|
|
|
void CV_Remap_Test::convert_maps()
|
|
{
|
|
if (mapx.type() != CV_16SC2)
|
|
convertMaps(mapx.clone(), mapy.clone(), mapx, mapy, CV_16SC2, interpolation == INTER_NEAREST);
|
|
else if (interpolation != INTER_NEAREST)
|
|
if (mapy.type() != CV_16UC1)
|
|
mapy.clone().convertTo(mapy, CV_16UC1);
|
|
|
|
if (interpolation == INTER_NEAREST)
|
|
mapy = Mat();
|
|
CV_Assert(((interpolation == INTER_NEAREST && mapy.empty()) || mapy.type() == CV_16UC1 ||
|
|
mapy.type() == CV_16SC1) && mapx.type() == CV_16SC2);
|
|
}
|
|
|
|
const char* CV_Remap_Test::borderType_to_string() const
|
|
{
|
|
if (borderType == BORDER_CONSTANT)
|
|
return "BORDER_CONSTANT";
|
|
if (borderType == BORDER_REPLICATE)
|
|
return "BORDER_REPLICATE";
|
|
if (borderType == BORDER_REFLECT)
|
|
return "BORDER_REFLECT";
|
|
if (borderType == BORDER_WRAP)
|
|
return "BORDER_WRAP";
|
|
if (borderType == BORDER_REFLECT_101)
|
|
return "BORDER_REFLECT_101";
|
|
return "Unsupported/Unknown border type";
|
|
}
|
|
|
|
void CV_Remap_Test::prepare_test_data_for_reference_func()
|
|
{
|
|
CV_ImageWarpBaseTest::prepare_test_data_for_reference_func();
|
|
convert_maps();
|
|
}
|
|
|
|
void CV_Remap_Test::run_reference_func()
|
|
{
|
|
prepare_test_data_for_reference_func();
|
|
|
|
if (interpolation == INTER_AREA)
|
|
interpolation = INTER_LINEAR;
|
|
|
|
int index = interpolation == INTER_NEAREST ? 0 : 1;
|
|
(this->*funcs[index])(src, reference_dst);
|
|
}
|
|
|
|
void CV_Remap_Test::remap_nearest(const Mat& _src, Mat& _dst)
|
|
{
|
|
CV_Assert(_src.depth() == CV_32F && _dst.type() == _src.type());
|
|
CV_Assert(mapx.type() == CV_16SC2 && mapy.empty());
|
|
|
|
Size ssize = _src.size(), dsize = _dst.size();
|
|
CV_Assert(!ssize.empty() && !dsize.empty());
|
|
int cn = _src.channels();
|
|
|
|
for (int dy = 0; dy < dsize.height; ++dy)
|
|
{
|
|
const short* yM = mapx.ptr<short>(dy);
|
|
float* yD = _dst.ptr<float>(dy);
|
|
|
|
for (int dx = 0; dx < dsize.width; ++dx)
|
|
{
|
|
float* xyD = yD + cn * dx;
|
|
int sx = yM[dx * 2], sy = yM[dx * 2 + 1];
|
|
|
|
if (sx >= 0 && sx < ssize.width && sy >= 0 && sy < ssize.height)
|
|
{
|
|
const float *xyS = _src.ptr<float>(sy) + sx * cn;
|
|
|
|
for (int r = 0; r < cn; ++r)
|
|
xyD[r] = xyS[r];
|
|
}
|
|
else if (borderType != BORDER_TRANSPARENT)
|
|
{
|
|
if (borderType == BORDER_CONSTANT)
|
|
for (int r = 0; r < cn; ++r)
|
|
xyD[r] = saturate_cast<float>(borderValue[r]);
|
|
else
|
|
{
|
|
sx = borderInterpolate(sx, ssize.width, borderType);
|
|
sy = borderInterpolate(sy, ssize.height, borderType);
|
|
CV_Assert(sx >= 0 && sy >= 0 && sx < ssize.width && sy < ssize.height);
|
|
|
|
const float *xyS = _src.ptr<float>(sy) + sx * cn;
|
|
|
|
for (int r = 0; r < cn; ++r)
|
|
xyD[r] = xyS[r];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void CV_Remap_Test::remap_generic(const Mat& _src, Mat& _dst)
|
|
{
|
|
CV_Assert(mapx.type() == CV_16SC2 && mapy.type() == CV_16UC1);
|
|
|
|
int ksize = 2;
|
|
if (interpolation == INTER_CUBIC)
|
|
ksize = 4;
|
|
else if (interpolation == INTER_LANCZOS4)
|
|
ksize = 8;
|
|
else if (interpolation != INTER_LINEAR)
|
|
CV_Assert(0);
|
|
int ofs = (ksize / 2) - 1;
|
|
|
|
CV_Assert(_src.depth() == CV_32F && _dst.type() == _src.type());
|
|
Size ssize = _src.size(), dsize = _dst.size();
|
|
int cn = _src.channels(), width1 = std::max(ssize.width - ksize + 1, 0),
|
|
height1 = std::max(ssize.height - ksize + 1, 0);
|
|
|
|
float ix[8], w[16];
|
|
interpolate_method inter_func = inter_array[interpolation - (interpolation == INTER_LANCZOS4 ? 2 : 1)];
|
|
|
|
for (int dy = 0; dy < dsize.height; ++dy)
|
|
{
|
|
const short* yMx = mapx.ptr<short>(dy);
|
|
const ushort* yMy = mapy.ptr<ushort>(dy);
|
|
|
|
float* yD = _dst.ptr<float>(dy);
|
|
|
|
for (int dx = 0; dx < dsize.width; ++dx)
|
|
{
|
|
float* xyD = yD + dx * cn;
|
|
float sx = yMx[dx * 2], sy = yMx[dx * 2 + 1];
|
|
int isx = cvFloor(sx), isy = cvFloor(sy);
|
|
|
|
inter_func((yMy[dx] & (INTER_TAB_SIZE - 1)) / static_cast<float>(INTER_TAB_SIZE), w);
|
|
inter_func(((yMy[dx] >> INTER_BITS) & (INTER_TAB_SIZE - 1)) / static_cast<float>(INTER_TAB_SIZE), w + ksize);
|
|
|
|
isx -= ofs;
|
|
isy -= ofs;
|
|
|
|
if (isx >= 0 && isx < width1 && isy >= 0 && isy < height1)
|
|
{
|
|
for (int r = 0; r < cn; ++r)
|
|
{
|
|
for (int y = 0; y < ksize; ++y)
|
|
{
|
|
const float* xyS = _src.ptr<float>(isy + y) + isx * cn;
|
|
|
|
ix[y] = 0;
|
|
for (int i = 0; i < ksize; ++i)
|
|
ix[y] += w[i] * xyS[i * cn + r];
|
|
}
|
|
xyD[r] = 0;
|
|
for (int i = 0; i < ksize; ++i)
|
|
xyD[r] += w[ksize + i] * ix[i];
|
|
}
|
|
}
|
|
else if (borderType != BORDER_TRANSPARENT)
|
|
{
|
|
int ar_x[8], ar_y[8];
|
|
|
|
for (int k = 0; k < ksize; k++)
|
|
{
|
|
ar_x[k] = borderInterpolate(isx + k, ssize.width, borderType) * cn;
|
|
ar_y[k] = borderInterpolate(isy + k, ssize.height, borderType);
|
|
}
|
|
|
|
for (int r = 0; r < cn; r++)
|
|
{
|
|
xyD[r] = 0;
|
|
for (int i = 0; i < ksize; ++i)
|
|
{
|
|
ix[i] = 0;
|
|
if (ar_y[i] >= 0)
|
|
{
|
|
const float* yS = _src.ptr<float>(ar_y[i]);
|
|
for (int j = 0; j < ksize; ++j)
|
|
ix[i] += saturate_cast<float>((ar_x[j] >= 0 ? yS[ar_x[j] + r] : borderValue[r]) * w[j]);
|
|
}
|
|
else
|
|
for (int j = 0; j < ksize; ++j)
|
|
ix[i] += saturate_cast<float>(borderValue[r] * w[j]);
|
|
}
|
|
for (int i = 0; i < ksize; ++i)
|
|
xyD[r] += saturate_cast<float>(w[ksize + i] * ix[i]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void CV_Remap_Test::validate_results() const
|
|
{
|
|
CV_ImageWarpBaseTest::validate_results();
|
|
if (cvtest::TS::ptr()->get_err_code() == cvtest::TS::FAIL_BAD_ACCURACY)
|
|
{
|
|
PRINT_TO_LOG("BorderType: %s\n", borderType_to_string());
|
|
PRINT_TO_LOG("BorderValue: (%f, %f, %f, %f)\n",
|
|
borderValue[0], borderValue[1], borderValue[2], borderValue[3]);
|
|
}
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
// warpAffine
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
class CV_WarpAffine_Test :
|
|
public CV_Remap_Test
|
|
{
|
|
public:
|
|
CV_WarpAffine_Test();
|
|
|
|
virtual ~CV_WarpAffine_Test();
|
|
|
|
protected:
|
|
virtual void generate_test_data();
|
|
virtual float get_success_error_level(int _interpolation, int _depth) const;
|
|
|
|
virtual void run_func();
|
|
virtual void run_reference_func();
|
|
|
|
template<int channels, typename T>
|
|
void newLinear(int x, float sx, float sy, const T *srcptr_, T *dstptr, int srccols, int srcrows, size_t srcstep,
|
|
const T *bval, int borderType_x, int borderType_y);
|
|
|
|
Mat M;
|
|
private:
|
|
void warpAffine(const Mat&, Mat&);
|
|
|
|
template<typename T>
|
|
void newWarpAffine(const Mat&, Mat&, const Mat&);
|
|
};
|
|
|
|
CV_WarpAffine_Test::CV_WarpAffine_Test() :
|
|
CV_Remap_Test()
|
|
{
|
|
}
|
|
|
|
CV_WarpAffine_Test::~CV_WarpAffine_Test()
|
|
{
|
|
}
|
|
|
|
void CV_WarpAffine_Test::generate_test_data()
|
|
{
|
|
CV_Remap_Test::generate_test_data();
|
|
|
|
RNG& rng = ts->get_rng();
|
|
|
|
// generating the M 2x3 matrix
|
|
static const int depths[] = { CV_32FC1, CV_64FC1 };
|
|
|
|
// generating 2d matrix
|
|
M = getRotationMatrix2D(Point2f(src.cols / 2.f, src.rows / 2.f),
|
|
rng.uniform(-180.f, 180.f), rng.uniform(0.4f, 2.0f));
|
|
int depth = depths[rng.uniform(0, sizeof(depths) / sizeof(depths[0]))];
|
|
if (M.depth() != depth)
|
|
{
|
|
Mat tmp;
|
|
M.convertTo(tmp, depth);
|
|
M = tmp;
|
|
}
|
|
|
|
// warp_matrix is inverse
|
|
if (rng.uniform(0., 1.) > 0)
|
|
interpolation |= cv::WARP_INVERSE_MAP;
|
|
}
|
|
|
|
void CV_WarpAffine_Test::run_func()
|
|
{
|
|
cv::warpAffine(src, dst, M, dst.size(), interpolation, borderType, borderValue);
|
|
}
|
|
|
|
float CV_WarpAffine_Test::get_success_error_level(int _interpolation, int _depth) const
|
|
{
|
|
return _depth == CV_8U ? 0.f : CV_ImageWarpBaseTest::get_success_error_level(_interpolation, _depth);
|
|
}
|
|
|
|
void CV_WarpAffine_Test::run_reference_func()
|
|
{
|
|
Mat tmp = Mat::zeros(dst.size(), dst.type());
|
|
warpAffine(src, tmp);
|
|
tmp.convertTo(reference_dst, reference_dst.depth());
|
|
}
|
|
|
|
#define FETCH_PIXEL_SCALAR(cn, dy, dx) \
|
|
if ((((unsigned)(ix + dx) < (unsigned)srccols) & ((unsigned)(iy + dy) < (unsigned)srcrows)) != 0) { \
|
|
size_t ofs = dy*srcstep + dx*cn; \
|
|
for (int ci = 0; ci < cn; ci++) { pxy[2*dy*cn+dx*cn+ci] = srcptr[ofs+ci];} \
|
|
} else if (borderType == BORDER_CONSTANT) { \
|
|
for (int ci = 0; ci < cn; ci++) { pxy[2*dy*cn+dx*cn+ci] = bval[ci];} \
|
|
} else if (borderType == BORDER_TRANSPARENT) { \
|
|
for (int ci = 0; ci < cn; ci++) { pxy[2*dy*cn+dx*cn+ci] = dstptr[x*cn+ci];} \
|
|
} else { \
|
|
int ix_ = borderInterpolate(ix + dx, srccols, borderType_x); \
|
|
int iy_ = borderInterpolate(iy + dy, srcrows, borderType_y); \
|
|
size_t glob_ofs = iy_*srcstep + ix_*cn; \
|
|
for (int ci = 0; ci < cn; ci++) { pxy[2*dy*cn+dx*cn+ci] = srcptr_[glob_ofs+ci];} \
|
|
}
|
|
|
|
#define WARPAFFINE_SHUFFLE(cn) \
|
|
if ((((unsigned)ix < (unsigned)(srccols-1)) & \
|
|
((unsigned)iy < (unsigned)(srcrows-1))) != 0) { \
|
|
for (int ci = 0; ci < cn; ci++) { \
|
|
pxy[ci] = srcptr[ci]; \
|
|
pxy[ci+cn] = srcptr[ci+cn]; \
|
|
pxy[ci+cn*2] = srcptr[srcstep+ci]; \
|
|
pxy[ci+cn*3] = srcptr[srcstep+ci+cn]; \
|
|
} \
|
|
} else { \
|
|
if ((borderType == BORDER_CONSTANT || borderType == BORDER_TRANSPARENT) && \
|
|
(((unsigned)(ix+1) >= (unsigned)(srccols+1))| \
|
|
((unsigned)(iy+1) >= (unsigned)(srcrows+1))) != 0) { \
|
|
if (borderType == BORDER_CONSTANT) { \
|
|
for (int ci = 0; ci < cn; ci++) { dstptr[x*cn+ci] = bval[ci]; } \
|
|
} \
|
|
return; \
|
|
} \
|
|
FETCH_PIXEL_SCALAR(cn, 0, 0); \
|
|
FETCH_PIXEL_SCALAR(cn, 0, 1); \
|
|
FETCH_PIXEL_SCALAR(cn, 1, 0); \
|
|
FETCH_PIXEL_SCALAR(cn, 1, 1); \
|
|
}
|
|
|
|
template<typename T>
|
|
static inline void warpaffine_linear_calc(int cn, const T *pxy, T *dst, float sx, float sy)
|
|
{
|
|
for (int ci = 0; ci < cn; ci++) {
|
|
float p00 = pxy[ci];
|
|
float p01 = pxy[ci+cn];
|
|
float p10 = pxy[ci+cn*2];
|
|
float p11 = pxy[ci+cn*3];
|
|
float v0 = p00 + sx*(p01 - p00);
|
|
float v1 = p10 + sx*(p11 - p10);
|
|
v0 += sy*(v1 - v0);
|
|
dst[ci] = saturate_cast<T>(v0);
|
|
}
|
|
}
|
|
template<>
|
|
inline void warpaffine_linear_calc<float>(int cn, const float *pxy, float *dst, float sx, float sy)
|
|
{
|
|
for (int ci = 0; ci < cn; ci++) {
|
|
float p00 = pxy[ci];
|
|
float p01 = pxy[ci+cn];
|
|
float p10 = pxy[ci+cn*2];
|
|
float p11 = pxy[ci+cn*3];
|
|
float v0 = p00 + sx*(p01 - p00);
|
|
float v1 = p10 + sx*(p11 - p10);
|
|
v0 += sy*(v1 - v0);
|
|
dst[ci] = v0;
|
|
}
|
|
}
|
|
|
|
template<int channels, typename T>
|
|
void CV_WarpAffine_Test::newLinear(int x, float sx, float sy, const T *srcptr_, T *dstptr,
|
|
int srccols, int srcrows, size_t srcstep,
|
|
const T *bval, int borderType_x, int borderType_y)
|
|
{
|
|
int ix = (int)floorf(sx), iy = (int)floorf(sy);
|
|
sx -= ix; sy -= iy;
|
|
|
|
T pxy[channels*4];
|
|
const T *srcptr = srcptr_ + srcstep*iy + ix*channels;
|
|
|
|
WARPAFFINE_SHUFFLE(channels);
|
|
|
|
warpaffine_linear_calc(channels, pxy, dstptr+x*channels, sx, sy);
|
|
}
|
|
template<>
|
|
void CV_WarpAffine_Test::newLinear<3, float>(int x, float sx, float sy, const float *srcptr_, float *dstptr,
|
|
int srccols, int srcrows, size_t srcstep,
|
|
const float *bval, int borderType_x, int borderType_y)
|
|
{
|
|
int ix = (int)floorf(sx), iy = (int)floorf(sy);
|
|
sx -= ix; sy -= iy;
|
|
|
|
float pxy[12];
|
|
const float *srcptr = srcptr_ + srcstep*iy + ix*3;
|
|
|
|
WARPAFFINE_SHUFFLE(3);
|
|
|
|
warpaffine_linear_calc(3, pxy, dstptr+x*3, sx, sy);
|
|
}
|
|
|
|
template<typename T>
|
|
void CV_WarpAffine_Test::newWarpAffine(const Mat &_src, Mat &_dst, const Mat &tM)
|
|
{
|
|
int num_channels = _dst.channels();
|
|
CV_CheckTrue(num_channels == 1 || num_channels == 3 || num_channels == 4, "");
|
|
|
|
auto *srcptr_ = _src.ptr<const T>();
|
|
auto *dstptr_ = _dst.ptr<T>();
|
|
size_t srcstep = _src.step/sizeof(T), dststep = _dst.step/sizeof(T);
|
|
int srccols = _src.cols, srcrows = _src.rows;
|
|
int dstcols = _dst.cols, dstrows = _dst.rows;
|
|
|
|
Mat ttM;
|
|
tM.convertTo(ttM, CV_32F);
|
|
auto *_M = ttM.ptr<const float>();
|
|
|
|
T bval[] = {
|
|
saturate_cast<T>(borderValue[0]),
|
|
saturate_cast<T>(borderValue[1]),
|
|
saturate_cast<T>(borderValue[2]),
|
|
saturate_cast<T>(borderValue[3]),
|
|
};
|
|
|
|
int borderType_x = borderType != BORDER_CONSTANT &&
|
|
borderType != BORDER_TRANSPARENT &&
|
|
srccols <= 1 ? BORDER_REPLICATE : borderType;
|
|
int borderType_y = borderType != BORDER_CONSTANT &&
|
|
borderType != BORDER_TRANSPARENT &&
|
|
srcrows <= 1 ? BORDER_REPLICATE : borderType;
|
|
|
|
for (int y = 0; y < dstrows; y++) {
|
|
T* dstptr = dstptr_ + y*dststep;
|
|
for (int x = 0; x < dstcols; x++) {
|
|
float sx = x*_M[0] + y*_M[1] + _M[2];
|
|
float sy = x*_M[3] + y*_M[4] + _M[5];
|
|
|
|
if (num_channels == 3) {
|
|
newLinear<3>(x, sx, sy, srcptr_, dstptr, srccols, srcrows, srcstep, bval, borderType_x, borderType_y);
|
|
} else if (num_channels == 4) {
|
|
newLinear<4>(x, sx, sy, srcptr_, dstptr, srccols, srcrows, srcstep, bval, borderType_x, borderType_y);
|
|
} else {
|
|
newLinear<1>(x, sx, sy, srcptr_, dstptr, srccols, srcrows, srcstep, bval, borderType_x, borderType_y);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void CV_WarpAffine_Test::warpAffine(const Mat& _src, Mat& _dst)
|
|
{
|
|
Size dsize = _dst.size();
|
|
|
|
CV_Assert(!_src.empty());
|
|
CV_Assert(!dsize.empty());
|
|
CV_Assert(_src.type() == _dst.type());
|
|
|
|
Mat tM;
|
|
M.convertTo(tM, CV_64F);
|
|
|
|
int inter = interpolation & INTER_MAX;
|
|
if (inter == INTER_AREA)
|
|
inter = INTER_LINEAR;
|
|
|
|
mapx.create(dsize, CV_16SC2);
|
|
if (inter != INTER_NEAREST)
|
|
mapy.create(dsize, CV_16SC1);
|
|
else
|
|
mapy = Mat();
|
|
|
|
if (!(interpolation & cv::WARP_INVERSE_MAP))
|
|
invertAffineTransform(tM.clone(), tM);
|
|
|
|
if (inter == INTER_LINEAR) {
|
|
int dst_depth = _dst.depth(), dst_channels = _dst.channels();
|
|
if (dst_depth == CV_8U && (dst_channels == 1 || dst_channels == 3 || dst_channels == 4)) {
|
|
return newWarpAffine<uint8_t>(_src, _dst, tM);
|
|
} else if (dst_depth == CV_16U && (dst_channels == 1 || dst_channels == 3 || dst_channels == 4)) {
|
|
return newWarpAffine<uint16_t>(_src, _dst, tM);
|
|
} else if (dst_depth == CV_32F && (dst_channels == 1 || dst_channels == 3 || dst_channels == 4)) {
|
|
return newWarpAffine<float>(_src, _dst, tM);
|
|
}
|
|
}
|
|
|
|
const int AB_BITS = MAX(10, (int)INTER_BITS);
|
|
const int AB_SCALE = 1 << AB_BITS;
|
|
int round_delta = (inter == INTER_NEAREST) ? AB_SCALE / 2 : (AB_SCALE / INTER_TAB_SIZE / 2);
|
|
|
|
const softdouble* data_tM = tM.ptr<softdouble>(0);
|
|
for (int dy = 0; dy < dsize.height; ++dy)
|
|
{
|
|
short* yM = mapx.ptr<short>(dy);
|
|
for (int dx = 0; dx < dsize.width; ++dx, yM += 2)
|
|
{
|
|
int v1 = saturate_cast<int>(saturate_cast<int>(data_tM[0] * dx * AB_SCALE) +
|
|
saturate_cast<int>((data_tM[1] * dy + data_tM[2]) * AB_SCALE) + round_delta),
|
|
v2 = saturate_cast<int>(saturate_cast<int>(data_tM[3] * dx * AB_SCALE) +
|
|
saturate_cast<int>((data_tM[4] * dy + data_tM[5]) * AB_SCALE) + round_delta);
|
|
v1 >>= AB_BITS - INTER_BITS;
|
|
v2 >>= AB_BITS - INTER_BITS;
|
|
|
|
yM[0] = saturate_cast<short>(v1 >> INTER_BITS);
|
|
yM[1] = saturate_cast<short>(v2 >> INTER_BITS);
|
|
|
|
if (inter != INTER_NEAREST)
|
|
mapy.ptr<short>(dy)[dx] = ((v2 & (INTER_TAB_SIZE - 1)) * INTER_TAB_SIZE + (v1 & (INTER_TAB_SIZE - 1)));
|
|
}
|
|
}
|
|
|
|
CV_Assert(mapx.type() == CV_16SC2 && ((inter == INTER_NEAREST && mapy.empty()) || mapy.type() == CV_16SC1));
|
|
cv::remap(_src, _dst, mapx, mapy, inter, borderType, borderValue);
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
// warpPerspective
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
class CV_WarpPerspective_Test :
|
|
public CV_WarpAffine_Test
|
|
{
|
|
public:
|
|
CV_WarpPerspective_Test();
|
|
|
|
virtual ~CV_WarpPerspective_Test();
|
|
|
|
protected:
|
|
virtual void generate_test_data();
|
|
virtual float get_success_error_level(int _interpolation, int _depth) const;
|
|
|
|
virtual void run_func();
|
|
virtual void run_reference_func();
|
|
|
|
private:
|
|
void warpPerspective(const Mat&, Mat&);
|
|
|
|
template<typename T>
|
|
void newWarpPerspective(const Mat&, Mat&, const Mat&);
|
|
};
|
|
|
|
CV_WarpPerspective_Test::CV_WarpPerspective_Test() :
|
|
CV_WarpAffine_Test()
|
|
{
|
|
}
|
|
|
|
CV_WarpPerspective_Test::~CV_WarpPerspective_Test()
|
|
{
|
|
}
|
|
|
|
void CV_WarpPerspective_Test::generate_test_data()
|
|
{
|
|
CV_Remap_Test::generate_test_data();
|
|
|
|
// generating the M 3x3 matrix
|
|
RNG& rng = ts->get_rng();
|
|
|
|
float cols = static_cast<float>(src.cols), rows = static_cast<float>(src.rows);
|
|
Point2f sp[] = { Point2f(0.0f, 0.0f), Point2f(cols, 0.0f), Point2f(0.0f, rows), Point2f(cols, rows) };
|
|
Point2f dp[] = { Point2f(rng.uniform(0.0f, cols), rng.uniform(0.0f, rows)),
|
|
Point2f(rng.uniform(0.0f, cols), rng.uniform(0.0f, rows)),
|
|
Point2f(rng.uniform(0.0f, cols), rng.uniform(0.0f, rows)),
|
|
Point2f(rng.uniform(0.0f, cols), rng.uniform(0.0f, rows)) };
|
|
M = getPerspectiveTransform(sp, dp);
|
|
|
|
static const int depths[] = { CV_32F, CV_64F };
|
|
int depth = depths[rng.uniform(0, 2)];
|
|
M.clone().convertTo(M, depth);
|
|
}
|
|
|
|
void CV_WarpPerspective_Test::run_func()
|
|
{
|
|
cv::warpPerspective(src, dst, M, dst.size(), interpolation, borderType, borderValue, cv::ALGO_HINT_APPROX);
|
|
// cv::warpPerspective(src, dst, M, dst.size(), interpolation, borderType, borderValue);
|
|
}
|
|
|
|
float CV_WarpPerspective_Test::get_success_error_level(int _interpolation, int _depth) const
|
|
{
|
|
return CV_ImageWarpBaseTest::get_success_error_level(_interpolation, _depth);
|
|
}
|
|
|
|
void CV_WarpPerspective_Test::run_reference_func()
|
|
{
|
|
Mat tmp = Mat::zeros(dst.size(), dst.type());
|
|
warpPerspective(src, tmp);
|
|
tmp.convertTo(reference_dst, reference_dst.depth());
|
|
}
|
|
|
|
template<typename T>
|
|
void CV_WarpPerspective_Test::newWarpPerspective(const Mat &_src, Mat &_dst, const Mat &tM)
|
|
{
|
|
int num_channels = _dst.channels();
|
|
CV_CheckTrue(num_channels == 1 || num_channels == 3 || num_channels == 4, "");
|
|
|
|
auto *srcptr_ = _src.ptr<const T>();
|
|
auto *dstptr_ = _dst.ptr<T>();
|
|
size_t srcstep = _src.step/sizeof(T), dststep = _dst.step/sizeof(T);
|
|
int srccols = _src.cols, srcrows = _src.rows;
|
|
int dstcols = _dst.cols, dstrows = _dst.rows;
|
|
|
|
Mat tmp;
|
|
tM.convertTo(tmp, CV_32F);
|
|
auto *_M = tmp.ptr<const float>();
|
|
|
|
T bval[] = {
|
|
saturate_cast<T>(borderValue[0]),
|
|
saturate_cast<T>(borderValue[1]),
|
|
saturate_cast<T>(borderValue[2]),
|
|
saturate_cast<T>(borderValue[3]),
|
|
};
|
|
|
|
int borderType_x = borderType != BORDER_CONSTANT &&
|
|
borderType != BORDER_TRANSPARENT &&
|
|
srccols <= 1 ? BORDER_REPLICATE : borderType;
|
|
int borderType_y = borderType != BORDER_CONSTANT &&
|
|
borderType != BORDER_TRANSPARENT &&
|
|
srcrows <= 1 ? BORDER_REPLICATE : borderType;
|
|
|
|
for (int y = 0; y < dstrows; y++) {
|
|
T* dstptr = dstptr_ + y*dststep;
|
|
for (int x = 0; x < dstcols; x++) {
|
|
float w = x*_M[6] + y*_M[7] + _M[8];
|
|
float sx = (x*_M[0] + y*_M[1] + _M[2]) / w;
|
|
float sy = (x*_M[3] + y*_M[4] + _M[5]) / w;
|
|
|
|
if (num_channels == 3) {
|
|
newLinear<3>(x, sx, sy, srcptr_, dstptr, srccols, srcrows, srcstep, bval, borderType_x, borderType_y);
|
|
} else if (num_channels == 4) {
|
|
newLinear<4>(x, sx, sy, srcptr_, dstptr, srccols, srcrows, srcstep, bval, borderType_x, borderType_y);
|
|
} else {
|
|
newLinear<1>(x, sx, sy, srcptr_, dstptr, srccols, srcrows, srcstep, bval, borderType_x, borderType_y);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void CV_WarpPerspective_Test::warpPerspective(const Mat& _src, Mat& _dst)
|
|
{
|
|
Size ssize = _src.size(), dsize = _dst.size();
|
|
|
|
CV_Assert(!ssize.empty());
|
|
CV_Assert(!dsize.empty());
|
|
CV_Assert(_src.type() == _dst.type());
|
|
|
|
if (M.depth() != CV_64F)
|
|
{
|
|
Mat tmp;
|
|
M.convertTo(tmp, CV_64F);
|
|
M = tmp;
|
|
}
|
|
|
|
if (!(interpolation & cv::WARP_INVERSE_MAP))
|
|
{
|
|
Mat tmp;
|
|
invert(M, tmp);
|
|
M = tmp;
|
|
}
|
|
|
|
int inter = interpolation & INTER_MAX;
|
|
if (inter == INTER_AREA)
|
|
inter = INTER_LINEAR;
|
|
|
|
if (inter == INTER_LINEAR) {
|
|
int dst_depth = _dst.depth(), dst_channels = _dst.channels();
|
|
if (dst_depth == CV_8U && (dst_channels == 1 || dst_channels == 3 || dst_channels == 4)) {
|
|
return newWarpPerspective<uint8_t>(_src, _dst, M);
|
|
} else if (dst_depth == CV_16U && (dst_channels == 1 || dst_channels == 3 || dst_channels == 4)) {
|
|
return newWarpPerspective<uint16_t>(_src, _dst, M);
|
|
} else if (dst_depth == CV_32F && (dst_channels == 1 || dst_channels == 3 || dst_channels == 4)) {
|
|
return newWarpPerspective<float>(_src, _dst, M);
|
|
}
|
|
}
|
|
|
|
mapx.create(dsize, CV_16SC2);
|
|
if (inter != INTER_NEAREST)
|
|
mapy.create(dsize, CV_16SC1);
|
|
else
|
|
mapy = Mat();
|
|
|
|
double* tM = M.ptr<double>(0);
|
|
for (int dy = 0; dy < dsize.height; ++dy)
|
|
{
|
|
short* yMx = mapx.ptr<short>(dy);
|
|
|
|
for (int dx = 0; dx < dsize.width; ++dx, yMx += 2)
|
|
{
|
|
double den = tM[6] * dx + tM[7] * dy + tM[8];
|
|
den = den ? 1.0 / den : 0.0;
|
|
|
|
if (inter == INTER_NEAREST)
|
|
{
|
|
yMx[0] = saturate_cast<short>((tM[0] * dx + tM[1] * dy + tM[2]) * den);
|
|
yMx[1] = saturate_cast<short>((tM[3] * dx + tM[4] * dy + tM[5]) * den);
|
|
continue;
|
|
}
|
|
|
|
den *= INTER_TAB_SIZE;
|
|
int v0 = saturate_cast<int>((tM[0] * dx + tM[1] * dy + tM[2]) * den);
|
|
int v1 = saturate_cast<int>((tM[3] * dx + tM[4] * dy + tM[5]) * den);
|
|
|
|
yMx[0] = saturate_cast<short>(v0 >> INTER_BITS);
|
|
yMx[1] = saturate_cast<short>(v1 >> INTER_BITS);
|
|
mapy.ptr<short>(dy)[dx] = saturate_cast<short>((v1 & (INTER_TAB_SIZE - 1)) *
|
|
INTER_TAB_SIZE + (v0 & (INTER_TAB_SIZE - 1)));
|
|
}
|
|
}
|
|
|
|
CV_Assert(mapx.type() == CV_16SC2 && ((inter == INTER_NEAREST && mapy.empty()) || mapy.type() == CV_16SC1));
|
|
cv::remap(_src, _dst, mapx, mapy, inter, borderType, borderValue);
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
// Tests
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
TEST(Imgproc_Resize_Test, accuracy) { CV_Resize_Test test; test.safe_run(); }
|
|
TEST(Imgproc_Remap_Test, accuracy) { CV_Remap_Test test; test.safe_run(); }
|
|
TEST(Imgproc_WarpAffine_Test, accuracy) { CV_WarpAffine_Test test; test.safe_run(); }
|
|
TEST(Imgproc_WarpPerspective_Test, accuracy) { CV_WarpPerspective_Test test; test.safe_run(); }
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#ifdef OPENCV_TEST_BIGDATA
|
|
|
|
CV_ENUM(Interpolation, INTER_NEAREST, INTER_LINEAR, INTER_LINEAR_EXACT, INTER_CUBIC, INTER_AREA)
|
|
|
|
class Imgproc_Resize :
|
|
public ::testing::TestWithParam<Interpolation>
|
|
{
|
|
public:
|
|
virtual void SetUp()
|
|
{
|
|
inter = GetParam();
|
|
}
|
|
|
|
protected:
|
|
int inter;
|
|
};
|
|
|
|
TEST_P(Imgproc_Resize, BigSize)
|
|
{
|
|
cv::Mat src(46342, 46342, CV_8UC3, cv::Scalar::all(10)), dst;
|
|
ASSERT_FALSE(src.empty());
|
|
|
|
ASSERT_NO_THROW(cv::resize(src, dst, cv::Size(), 0.5, 0.5, inter));
|
|
}
|
|
|
|
INSTANTIATE_TEST_CASE_P(Imgproc, Imgproc_Resize, Interpolation::all());
|
|
|
|
#endif
|
|
|
|
}} // namespace
|