| 154 | |
| 155 | template<typename T> |
| 156 | int computeHomography(T* H_ptr, const float* rnd_ptr, const float* x_src_ptr, |
| 157 | const float* y_src_ptr, const float* x_dst_ptr, |
| 158 | const float* y_dst_ptr) { |
| 159 | if (static_cast<unsigned>(rnd_ptr[0]) == |
| 160 | static_cast<unsigned>(rnd_ptr[1]) || |
| 161 | static_cast<unsigned>(rnd_ptr[0]) == |
| 162 | static_cast<unsigned>(rnd_ptr[2]) || |
| 163 | static_cast<unsigned>(rnd_ptr[0]) == |
| 164 | static_cast<unsigned>(rnd_ptr[3]) || |
| 165 | static_cast<unsigned>(rnd_ptr[1]) == |
| 166 | static_cast<unsigned>(rnd_ptr[2]) || |
| 167 | static_cast<unsigned>(rnd_ptr[1]) == |
| 168 | static_cast<unsigned>(rnd_ptr[3]) || |
| 169 | static_cast<unsigned>(rnd_ptr[2]) == |
| 170 | static_cast<unsigned>(rnd_ptr[3])) { |
| 171 | return 1; |
| 172 | } |
| 173 | |
| 174 | float src_pt_x[4], src_pt_y[4], dst_pt_x[4], dst_pt_y[4]; |
| 175 | for (unsigned j = 0; j < 4; j++) { |
| 176 | src_pt_x[j] = x_src_ptr[static_cast<unsigned>(rnd_ptr[j])]; |
| 177 | src_pt_y[j] = y_src_ptr[static_cast<unsigned>(rnd_ptr[j])]; |
| 178 | dst_pt_x[j] = x_dst_ptr[static_cast<unsigned>(rnd_ptr[j])]; |
| 179 | dst_pt_y[j] = y_dst_ptr[static_cast<unsigned>(rnd_ptr[j])]; |
| 180 | } |
| 181 | |
| 182 | float x_src_mean = |
| 183 | (src_pt_x[0] + src_pt_x[1] + src_pt_x[2] + src_pt_x[3]) / 4.f; |
| 184 | float y_src_mean = |
| 185 | (src_pt_y[0] + src_pt_y[1] + src_pt_y[2] + src_pt_y[3]) / 4.f; |
| 186 | float x_dst_mean = |
| 187 | (dst_pt_x[0] + dst_pt_x[1] + dst_pt_x[2] + dst_pt_x[3]) / 4.f; |
| 188 | float y_dst_mean = |
| 189 | (dst_pt_y[0] + dst_pt_y[1] + dst_pt_y[2] + dst_pt_y[3]) / 4.f; |
| 190 | |
| 191 | float src_var = 0.0f, dst_var = 0.0f; |
| 192 | for (unsigned j = 0; j < 4; j++) { |
| 193 | src_var += sq(src_pt_x[j] - x_src_mean) + sq(src_pt_y[j] - y_src_mean); |
| 194 | dst_var += sq(dst_pt_x[j] - x_dst_mean) + sq(dst_pt_y[j] - y_dst_mean); |
| 195 | } |
| 196 | |
| 197 | src_var /= 4.f; |
| 198 | dst_var /= 4.f; |
| 199 | |
| 200 | float src_scale = sqrt(2.0f) / sqrt(src_var); |
| 201 | float dst_scale = sqrt(2.0f) / sqrt(dst_var); |
| 202 | |
| 203 | Array<T> A = createValueArray<T>(af::dim4(9, 9), static_cast<T>(0)); |
| 204 | af::dim4 Adims = A.dims(); |
| 205 | T* A_ptr = A.get(); |
| 206 | getQueue().sync(); |
| 207 | |
| 208 | for (unsigned j = 0; j < 4; j++) { |
| 209 | float srcx = (src_pt_x[j] - x_src_mean) * src_scale; |
| 210 | float srcy = (src_pt_y[j] - y_src_mean) * src_scale; |
| 211 | float dstx = (dst_pt_x[j] - x_dst_mean) * dst_scale; |
| 212 | float dsty = (dst_pt_y[j] - y_dst_mean) * dst_scale; |
| 213 | |