| 24 | |
| 25 | template<typename T> |
| 26 | int homography(Array<T> &bestH, const Array<float> &x_src, |
| 27 | const Array<float> &y_src, const Array<float> &x_dst, |
| 28 | const Array<float> &y_dst, const Array<float> &initial, |
| 29 | const af_homography_type htype, const float inlier_thr, |
| 30 | const unsigned iterations) { |
| 31 | // constexpr float RANSACConfidence = 0.99f; |
| 32 | constexpr float LMEDSConfidence = 0.99f; |
| 33 | constexpr float LMEDSOutlierRatio = 0.4f; |
| 34 | |
| 35 | const af::dim4 &idims = x_src.dims(); |
| 36 | const unsigned nsamples = idims[0]; |
| 37 | |
| 38 | unsigned iter = iterations; |
| 39 | Array<float> err = createEmptyArray<float>(af::dim4()); |
| 40 | if (htype == AF_HOMOGRAPHY_LMEDS) { |
| 41 | iter = |
| 42 | ::std::min(iter, static_cast<unsigned>( |
| 43 | log(1.f - LMEDSConfidence) / |
| 44 | log(1.f - pow(1.f - LMEDSOutlierRatio, 4.f)))); |
| 45 | err = createValueArray<float>(af::dim4(nsamples, iter), |
| 46 | numeric_limits<float>::max()); |
| 47 | } else { |
| 48 | // Avoid passing "null" cl_mem object to kernels |
| 49 | err = createEmptyArray<float>(af::dim4(1)); |
| 50 | } |
| 51 | |
| 52 | const size_t iter_sz = divup(iter, 256) * 256; |
| 53 | |
| 54 | af::dim4 rdims(4, iter_sz); |
| 55 | Array<float> fctr = |
| 56 | createValueArray<float>(rdims, static_cast<float>(nsamples)); |
| 57 | Array<float> rnd = arithOp<float, af_mul_t>(initial, fctr, rdims); |
| 58 | |
| 59 | Array<T> tmpH = |
| 60 | createValueArray<T>(af::dim4(9, iter_sz), static_cast<T>(0)); |
| 61 | |
| 62 | bestH = createValueArray<T>(af::dim4(3, 3), static_cast<T>(0)); |
| 63 | return kernel::computeH<T>(bestH, tmpH, err, x_src, y_src, x_dst, y_dst, |
| 64 | rnd, iter, nsamples, inlier_thr, htype); |
| 65 | } |
| 66 | |
| 67 | #define INSTANTIATE(T) \ |
| 68 | template int homography( \ |