void ccv_sample_up(ccv_dense_matrix_t* a, ccv_dense_matrix_t** b, int type, int src_x, int src_y) { assert(src_x >= 0 && src_y >= 0); ccv_declare_matrix_signature(sig, a->sig != 0, ccv_sign_with_format(64, "ccv_sample_up(%d,%d)", src_x, src_y), a->sig, 0); type = (type == 0) ? CCV_GET_DATA_TYPE(a->type) | CCV_GET_CHANNEL(a->type) : CCV_GET_DATA_TYPE(type) | CCV_GET_CHANNEL(a->type); ccv_dense_matrix_t* db = *b = ccv_dense_matrix_renew(*b, a->rows * 2, a->cols * 2, CCV_ALL_DATA_TYPE | CCV_GET_CHANNEL(a->type), type, sig); ccv_matrix_return_if_cached(, db); int ch = CCV_GET_CHANNEL(a->type); int cols0 = a->cols - 1 - src_x; int y, x, sy = -1 + src_y, sx = src_x * ch, k; int* tab = (int*)alloca((a->cols + src_x + 2) * ch * sizeof(int)); for (x = 0; x < a->cols + src_x + 2; x++) for (k = 0; k < ch; k++) tab[x * ch + k] = ((x >= a->cols) ? a->cols * 2 - 1 - x : x) * ch + k; unsigned char* buf = (unsigned char*)alloca(3 * db->cols * ch * ccv_max(CCV_GET_DATA_TYPE_SIZE(db->type), sizeof(int))); int bufstep = db->cols * ch * ccv_max(CCV_GET_DATA_TYPE_SIZE(db->type), sizeof(int)); unsigned char* b_ptr = db->data.u8; /* why src_y * 2: the same argument as in ccv_sample_down */ #define for_block(_for_get_a, _for_set, _for_get, _for_set_b) \ for (y = 0; y < a->rows; y++) \ { \ for (; sy <= y + 1 + src_y; sy++) \ { \ unsigned char* row = buf + ((sy + src_y * 2 + 1) % 3) * bufstep; \ int _sy = (sy < 0) ? -1 - sy : (sy >= a->rows) ? a->rows * 2 - 1 - sy : sy; \ unsigned char* a_ptr = a->data.u8 + a->step * _sy; \ if (a->cols == 1) \ { \ for (k = 0; k < ch; k++) \ { \ _for_set(row, k, _for_get_a(a_ptr, k, 0) * (G025 + G075 + G125), 0); \ _for_set(row, k + ch, _for_get_a(a_ptr, k, 0) * (G025 + G075 + G125), 0); \ } \ continue; \ } \ if (sx == 0) \ { \ for (k = 0; k < ch; k++) \ { \ _for_set(row, k, _for_get_a(a_ptr, k + sx, 0) * (G025 + G075) + _for_get_a(a_ptr, k + sx + ch, 0) * G125, 0); \ _for_set(row, k + ch, _for_get_a(a_ptr, k + sx, 0) * (G125 + G025) + _for_get_a(a_ptr, k + sx + ch, 0) * G075, 0); \ } \ } \ /* some serious flaw in computing Gaussian weighting in previous version * specially, we are doing perfect upsampling (2x) so, it concerns a grid like: * XXYY * XXYY * in this case, to upsampling, the weight should be from distance 0.25 and 1.25, and 0.25 and 0.75 * previously, it was mistakingly be 0.0 1.0, 0.5 0.5 (imperfect upsampling (2x - 1)) */ \ for (x = (sx == 0) ? ch : 0; x < cols0 * ch; x += ch) \ { \ for (k = 0; k < ch; k++) \ { \ _for_set(row, x * 2 + k, _for_get_a(a_ptr, x + sx - ch + k, 0) * G075 + _for_get_a(a_ptr, x + sx + k, 0) * G025 + _for_get_a(a_ptr, x + sx + ch + k, 0) * G125, 0); \ _for_set(row, x * 2 + ch + k, _for_get_a(a_ptr, x + sx - ch + k, 0) * G125 + _for_get_a(a_ptr, x + sx + k, 0) * G025 + _for_get_a(a_ptr, x + sx + ch + k, 0) * G075, 0); \ } \ } \ x_block(_for_get_a, _for_set, _for_get, _for_set_b); \ } \
static void _ccv_init_cubic_coeffs(int si, int sz, float s, ccv_cubic_coeffs_t* coeff) { const float A = -0.75f; coeff->si[0] = ccv_max(si - 1, 0); coeff->si[1] = si; coeff->si[2] = ccv_min(si + 1, sz - 1); coeff->si[3] = ccv_min(si + 2, sz - 1); float x = s - si; coeff->coeffs[0] = ((A * (x + 1) - 5 * A) * (x + 1) + 8 * A) * (x + 1) - 4 * A; coeff->coeffs[1] = ((A + 2) * x - (A + 3)) * x * x + 1; coeff->coeffs[2] = ((A + 2) * (1 - x) - (A + 3)) * (1 - x) * (1 - x) + 1; coeff->coeffs[3] = 1.f - coeff->coeffs[0] - coeff->coeffs[1] - coeff->coeffs[2]; }
static void _ccv_init_cubic_integer_coeffs(int si, int sz, float s, ccv_cubic_integer_coeffs_t* coeff) { const float A = -0.75f; coeff->si[0] = ccv_max(si - 1, 0); coeff->si[1] = si; coeff->si[2] = ccv_min(si + 1, sz - 1); coeff->si[3] = ccv_min(si + 2, sz - 1); float x = s - si; const int W_BITS = 1 << 6; coeff->coeffs[0] = (int)((((A * (x + 1) - 5 * A) * (x + 1) + 8 * A) * (x + 1) - 4 * A) * W_BITS + 0.5); coeff->coeffs[1] = (int)((((A + 2) * x - (A + 3)) * x * x + 1) * W_BITS + 0.5); coeff->coeffs[2] = (int)((((A + 2) * (1 - x) - (A + 3)) * (1 - x) * (1 - x) + 1) * W_BITS + 0.5); coeff->coeffs[3] = W_BITS - coeff->coeffs[0] - coeff->coeffs[1] - coeff->coeffs[2]; }
static int _ccv_nnc_gemm_back_sse2(const ccv_nnc_tensor_view_t* const g, const ccv_nnc_tensor_view_t* const a, const ccv_nnc_tensor_view_t* const w, ccv_nnc_tensor_view_t* const dw, ccv_nnc_tensor_view_t* const bias, ccv_nnc_tensor_view_t* const h, const int flags) { const int* dwinc = CCV_IS_TENSOR_VIEW(dw) ? dw->inc : dw->info.dim; if (!(flags & CCV_NNC_ACCUMULATE_OUTPUT)) // reset the gradients to 0 { memset(dw->data.u8, 0, sizeof(float) * dwinc[1] * dw->info.dim[0]); memset(bias->data.u8, 0, sizeof(float) * bias->info.dim[0]); } const int a_nd = ccv_nnc_tensor_nd(a->info.dim); const int* adim = (a_nd == 1) ? a->info.dim : a->info.dim + 1; const int g_nd = ccv_nnc_tensor_nd(g->info.dim); const int* gdim = (g_nd == 1) ? g->info.dim : g->info.dim + 1; const int batch_size = a_nd == 1 ? 1 : ccv_max(1, a->info.dim[0]); int i, j; float* gp = g->data.f32; float* bp = bias->data.f32; assert(bias->info.dim[0] == gdim[0]); const int* ginc = CCV_IS_TENSOR_VIEW(g) ? ((g_nd == 1) ? g->inc : g->inc + 1) : gdim; for (i = 0; i < batch_size; i++) { for (j = 0; j < gdim[0] - 3; j += 4) { __m128 g4 = _mm_load_ps(gp + j); __m128 b4 = _mm_load_ps(bp + j); _mm_stream_ps(bp + j, _mm_add_ps(b4, g4)); } gp += ginc[0]; } assert(gdim[0] == dw->info.dim[0]); assert(adim[0] == dw->info.dim[1]); const int* ainc = CCV_IS_TENSOR_VIEW(a) ? ((a_nd == 1) ? a->inc : a->inc + 1) : adim; for (i = 0; i < batch_size; i++) { const float* const gp = g->data.f32 + i * ginc[0]; const float* const ap = a->data.f32 + i * ainc[0]; parallel_for(j, gdim[0]) { float* const dwp = dw->data.f32 + j * dwinc[1]; __m128 g4 = _mm_set1_ps(gp[j]); int k; for (k = 0; k < adim[0] - 3; k+= 4) { __m128 a4 = _mm_load_ps(ap + k); __m128 dw4 = _mm_load_ps(dwp + k); _mm_stream_ps(dwp + k, _mm_add_ps(dw4, _mm_mul_ps(a4, g4))); } } parallel_endfor }
static int _ccv_nnc_gemm_forw_sse2(const ccv_nnc_tensor_view_t* const a, const ccv_nnc_tensor_view_t* const w, const ccv_nnc_tensor_view_t* const bias, ccv_nnc_tensor_view_t* const b) { const int a_nd = ccv_nnc_tensor_nd(a->info.dim); const int* adim = (a_nd == 1) ? a->info.dim : a->info.dim + 1; const int b_nd = ccv_nnc_tensor_nd(b->info.dim); const int* bdim = (b_nd == 1) ? b->info.dim : b->info.dim + 1; assert(bdim[0] == bias->info.dim[0]); assert(bdim[0] == w->info.dim[0]); assert(adim[0] == w->info.dim[1]); const int* ainc = CCV_IS_TENSOR_VIEW(a) ? (a_nd == 1 ? a->inc : a->inc + 1) : adim; const int* binc = CCV_IS_TENSOR_VIEW(b) ? (b_nd == 1 ? b->inc : b->inc + 1) : bdim; const int* winc = CCV_IS_TENSOR_VIEW(w) ? w->inc : w->info.dim; const int batch_size = a_nd == 1 ? 1 : ccv_max(1, a->info.dim[0]); int i; for (i = 0; i < batch_size; i++) { const float* const ap = a->data.f32 + i * ainc[0]; float* const bp = b->data.f32 + i * binc[0]; parallel_for(j, bdim[0]) { const float* const wp = w->data.f32 + j * winc[1]; int k; __m128 v40 = _mm_set_ss(bias->data.f32[j]); __m128 v41 = _mm_setzero_ps(); for (k = 0; k < adim[0] - 7; k += 8) { __m128 ap40 = _mm_load_ps(ap + k); __m128 ap41 = _mm_load_ps(ap + k + 4); __m128 w40 = _mm_load_ps(wp + k); __m128 w41 = _mm_load_ps(wp + k + 4); v40 =_mm_add_ps(_mm_mul_ps(w40, ap40), v40); v41 =_mm_add_ps(_mm_mul_ps(w41, ap41), v41); } v40 = _mm_add_ps(v40, v41); v41 = _mm_add_ps(v40, _mm_movehl_ps(v40, v40)); v40 = _mm_add_ss(v41, _mm_shuffle_ps(v41, v41, 1)); _mm_store_ss(bp + j, v40); } parallel_endfor } return CCV_NNC_EXEC_SUCCESS; }
ccv_array_t* ccv_array_new(int rsize, int rnum, uint64_t sig) { ccv_array_t* array; if (ccv_cache_opt && sig != 0) { uint8_t type; array = (ccv_array_t*)ccv_cache_out(&ccv_cache, sig, &type); if (array) { assert(type == 1); array->type |= CCV_GARBAGE; array->refcount = 1; return array; } } array = (ccv_array_t*)ccmalloc(sizeof(ccv_array_t)); array->sig = sig; array->type = CCV_REUSABLE & ~CCV_GARBAGE; array->rnum = 0; array->rsize = rsize; array->size = ccv_max(rnum, 2 /* allocate memory for at least 2 items */); array->data = ccmalloc((size_t)array->size * (size_t)rsize); return array; }
static void _ccv_set_union_mser(ccv_dense_matrix_t* a, ccv_dense_matrix_t* h, ccv_dense_matrix_t* b, ccv_array_t* seq, ccv_mser_param_t params) { assert(params.direction == CCV_BRIGHT_TO_DARK || params.direction == CCV_DARK_TO_BRIGHT); int v, i, j; ccv_mser_node_t* node = (ccv_mser_node_t*)ccmalloc(sizeof(ccv_mser_node_t) * a->rows * a->cols); ccv_mser_node_t** rnode = (ccv_mser_node_t**)ccmalloc(sizeof(ccv_mser_node_t*) * a->rows * a->cols); if (params.range <= 0) params.range = 255; // put it in a block so that the memory allocated can be released in the end int* buck = (int*)alloca(sizeof(int) * (params.range + 2)); memset(buck, 0, sizeof(int) * (params.range + 2)); ccv_mser_node_t* pnode = node; // this for_block is the only computation that can be shared between dark to bright and bright to dark // two MSER alternatives, and it only occupies 10% of overall time, we won't share this computation // at all (also, we need to reinitialize node for the two passes anyway). if (h != 0) { unsigned char* aptr = a->data.u8; unsigned char* hptr = h->data.u8; #define for_block(_for_get_a, _for_get_h) \ for (i = 0; i < a->rows; i++) \ { \ for (j = 0; j < a->cols; j++) \ if (!_for_get_h(hptr, j, 0)) \ ++buck[_for_get_a(aptr, j, 0)]; \ aptr += a->step; \ hptr += h->step; \ } \ for (i = 1; i <= params.range; i++) \ buck[i] += buck[i - 1]; \ buck[params.range + 1] = buck[params.range]; \ aptr = a->data.u8; \ hptr = h->data.u8; \ for (i = 0; i < a->rows; i++) \ { \ for (j = 0; j < a->cols; j++) \ { \ _ccv_mser_init_node(pnode, j, i); \ if (!_for_get_h(hptr, j, 0)) \ rnode[--buck[_for_get_a(aptr, j, 0)]] = pnode; \ else \ pnode->shortcut = 0; /* this means the pnode is not available */ \ ++pnode; \ } \ aptr += a->step; \ hptr += h->step; \ } ccv_matrix_getter_integer_only_a(a->type, ccv_matrix_getter_integer_only, h->type, for_block); #undef for_block } else { unsigned char* aptr = a->data.u8; #define for_block(_, _for_get) \ for (i = 0; i < a->rows; i++) \ { \ for (j = 0; j < a->cols; j++) \ ++buck[_for_get(aptr, j, 0)]; \ aptr += a->step; \ } \ for (i = 1; i <= params.range; i++) \ buck[i] += buck[i - 1]; \ buck[params.range + 1] = buck[params.range]; \ aptr = a->data.u8; \ for (i = 0; i < a->rows; i++) \ { \ for (j = 0; j < a->cols; j++) \ { \ _ccv_mser_init_node(pnode, j, i); \ rnode[--buck[_for_get(aptr, j, 0)]] = pnode; \ ++pnode; \ } \ aptr += a->step; \ } ccv_matrix_getter_integer_only(a->type, for_block); #undef for_block } ccv_array_t* history_list = ccv_array_new(sizeof(ccv_mser_history_t), 64, 0); for (v = 0; v <= params.range; v++) { int range_segment = buck[params.direction == CCV_DARK_TO_BRIGHT ? v : params.range - v]; int range_segment_cap = buck[params.direction == CCV_DARK_TO_BRIGHT ? v + 1 : params.range - v + 1]; for (i = range_segment; i < range_segment_cap; i++) { pnode = rnode[i]; // try to merge pnode with its neighbors static int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1}; static int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1}; ccv_mser_node_t* node0 = _ccv_mser_find_root(pnode); for (j = 0; j < 8; j++) { int x = dx[j] + pnode->point.x; int y = dy[j] + pnode->point.y; if (x >= 0 && x < a->cols && y >= 0 && y < a->rows) { ccv_mser_node_t* nnode = pnode + dx[j] + dy[j] * a->cols; if (nnode->shortcut == 0) // this is a void node, skip continue; ccv_mser_node_t* node1 = _ccv_mser_find_root(nnode); if (node0 != node1) { // grep the extended root information ccv_mser_history_t* root0 = (node0->root >= 0) ? (ccv_mser_history_t*)ccv_array_get(history_list, node0->root) : 0; ccv_mser_history_t* root1 = (node1->root >= 0) ? (ccv_mser_history_t*)ccv_array_get(history_list, node1->root) : 0; // swap the node if root1 has higher rank, or larger in size, or root0 is non-existent if ((root0 && root1 && (root1->value > root0->value || (root1->value == root0->value && root1->rank > root0->rank) || (root1->value == root0->value && root1->rank == root0->rank && root1->size > root0->size))) || (root1 && !root0)) { ccv_mser_node_t* exnode = node0; node0 = node1; node1 = exnode; ccv_mser_history_t* root = root0; root0 = root1; root1 = root; } if (!root0) { ccv_mser_history_t root = { .rank = 0, .size = 1, .value = v, .shortcut = history_list->rnum, .parent = history_list->rnum, .head = node0, .tail = node1 }; node0->root = history_list->rnum; ccv_array_push(history_list, &root); root0 = (ccv_mser_history_t*)ccv_array_get(history_list, history_list->rnum - 1); assert(node1->root == -1); } else if (root0->value < v) { // conceal the old root as history (er), making a new one and pointing to it root0->shortcut = root0->parent = history_list->rnum; ccv_mser_history_t root = *root0; root.value = v; node0->root = history_list->rnum; ccv_array_push(history_list, &root); root0 = (ccv_mser_history_t*)ccv_array_get(history_list, history_list->rnum - 1); root1 = (node1->root >= 0) ? (ccv_mser_history_t*)ccv_array_get(history_list, node1->root) : 0; // the memory may be reallocated root0->rank = ccv_max(root0->rank, (root1 ? root1->rank : 0)) + 1; } if (root1) { if (root1->value < root0->value) // in this case, root1 is sealed as well root1->parent = node0->root; // thus, if root1->parent == itself && root1->shortcut != itself // it is voided, and not sealed root1->shortcut = node0->root; } // merge the two node1->shortcut = node0; root0->size += root1 ? root1->size : 1; /* insert one endless double link list to another, see illustration: * 0->1->2->3->4->5->0 * a->b->c->d->a * set 5.next (0.prev.next) point to a * set 0.prev point to d * set d.next (a.prev.next) point to 0 * set a.prev point to 5 * the result endless double link list will be: * 0->1->2->3->4->5->a->b->c->d->0 */ node0->prev->next = node1; ccv_mser_node_t* prev = node0->prev; node0->prev = node1->prev; node1->prev->next = node0; // consider self-referencing node1->prev = prev; root0->head = node0; root0->tail = node0->prev; } } }
/* the following code is adopted from OpenCV cvPyrDown */ void ccv_sample_down(ccv_dense_matrix_t* a, ccv_dense_matrix_t** b, int type, int src_x, int src_y) { assert(src_x >= 0 && src_y >= 0); ccv_declare_derived_signature(sig, a->sig != 0, ccv_sign_with_format(64, "ccv_sample_down(%d,%d)", src_x, src_y), a->sig, CCV_EOF_SIGN); type = (type == 0) ? CCV_GET_DATA_TYPE(a->type) | CCV_GET_CHANNEL(a->type) : CCV_GET_DATA_TYPE(type) | CCV_GET_CHANNEL(a->type); ccv_dense_matrix_t* db = *b = ccv_dense_matrix_renew(*b, a->rows / 2, a->cols / 2, CCV_ALL_DATA_TYPE | CCV_GET_CHANNEL(a->type), type, sig); ccv_object_return_if_cached(, db); int ch = CCV_GET_CHANNEL(a->type); int cols0 = db->cols - 1 - src_x; int dy, sy = -2 + src_y, sx = src_x * ch, dx, k; int* tab = (int*)alloca((a->cols + src_x + 2) * ch * sizeof(int)); for (dx = 0; dx < a->cols + src_x + 2; dx++) for (k = 0; k < ch; k++) tab[dx * ch + k] = ((dx >= a->cols) ? a->cols * 2 - 1 - dx : dx) * ch + k; unsigned char* buf = (unsigned char*)alloca(5 * db->cols * ch * ccv_max(CCV_GET_DATA_TYPE_SIZE(db->type), sizeof(int))); int bufstep = db->cols * ch * ccv_max(CCV_GET_DATA_TYPE_SIZE(db->type), sizeof(int)); #ifdef __clang_analyzer__ memset(buf, 0, 5 * bufstep); #endif unsigned char* b_ptr = db->data.u8; /* why is src_y * 4 in computing the offset of row? * Essentially, it means sy - src_y but in a manner that doesn't result negative number. * notice that we added src_y before when computing sy in the first place, however, * it is not desirable to have that offset when we try to wrap it into our 5-row buffer ( * because in later rearrangement, we have no src_y to backup the arrangement). In * such micro scope, we managed to stripe 5 addition into one shift and addition. */ #define for_block(_for_get_a, _for_set, _for_get, _for_set_b) \ for (dy = 0; dy < db->rows; dy++) \ { \ for(; sy <= dy * 2 + 2 + src_y; sy++) \ { \ unsigned char* row = buf + ((sy + src_y * 4 + 2) % 5) * bufstep; \ int _sy = (sy < 0) ? -1 - sy : (sy >= a->rows) ? a->rows * 2 - 1 - sy : sy; \ unsigned char* a_ptr = a->data.u8 + a->step * _sy; \ for (k = 0; k < ch; k++) \ _for_set(row, k, _for_get_a(a_ptr, sx + k, 0) * 10 + _for_get_a(a_ptr, ch + sx + k, 0) * 5 + _for_get_a(a_ptr, 2 * ch + sx + k, 0), 0); \ for(dx = ch; dx < cols0 * ch; dx += ch) \ for (k = 0; k < ch; k++) \ _for_set(row, dx + k, _for_get_a(a_ptr, dx * 2 + sx + k, 0) * 6 + (_for_get_a(a_ptr, dx * 2 + sx + k - ch, 0) + _for_get_a(a_ptr, dx * 2 + sx + k + ch, 0)) * 4 + _for_get_a(a_ptr, dx * 2 + sx + k - ch * 2, 0) + _for_get_a(a_ptr, dx * 2 + sx + k + ch * 2, 0), 0); \ x_block(_for_get_a, _for_set, _for_get, _for_set_b); \ } \ unsigned char* rows[5]; \ for(k = 0; k < 5; k++) \ rows[k] = buf + ((dy * 2 + k) % 5) * bufstep; \ for(dx = 0; dx < db->cols * ch; dx++) \ _for_set_b(b_ptr, dx, (_for_get(rows[2], dx, 0) * 6 + (_for_get(rows[1], dx, 0) + _for_get(rows[3], dx, 0)) * 4 + _for_get(rows[0], dx, 0) + _for_get(rows[4], dx, 0)) / 256, 0); \ b_ptr += db->step; \ } int no_8u_type = (a->type & CCV_8U) ? CCV_32S : a->type; if (src_x > 0) { #define x_block(_for_get_a, _for_set, _for_get, _for_set_b) \ for (dx = cols0 * ch; dx < db->cols * ch; dx += ch) \ for (k = 0; k < ch; k++) \ _for_set(row, dx + k, _for_get_a(a_ptr, tab[dx * 2 + sx + k], 0) * 6 + (_for_get_a(a_ptr, tab[dx * 2 + sx + k - ch], 0) + _for_get_a(a_ptr, tab[dx * 2 + sx + k + ch], 0)) * 4 + _for_get_a(a_ptr, tab[dx * 2 + sx + k - ch * 2], 0) + _for_get_a(a_ptr, tab[dx * 2 + sx + k + ch * 2], 0), 0); ccv_matrix_getter_a(a->type, ccv_matrix_setter_getter, no_8u_type, ccv_matrix_setter_b, db->type, for_block); #undef x_block } else { #define x_block(_for_get_a, _for_set, _for_get, _for_set_b) \ for (k = 0; k < ch; k++) \ _for_set(row, (db->cols - 1) * ch + k, _for_get_a(a_ptr, a->cols * ch + sx - ch + k, 0) * 10 + _for_get_a(a_ptr, (a->cols - 2) * ch + sx + k, 0) * 5 + _for_get_a(a_ptr, (a->cols - 3) * ch + sx + k, 0), 0); ccv_matrix_getter_a(a->type, ccv_matrix_setter_getter, no_8u_type, ccv_matrix_setter_b, db->type, for_block); #undef x_block } #undef for_block }
static void _ccv_resample_area_8u(ccv_dense_matrix_t* a, ccv_dense_matrix_t* b) { assert(a->cols > 0 && b->cols > 0); ccv_int_alpha* xofs = (ccv_int_alpha*)alloca(sizeof(ccv_int_alpha) * a->cols * 2); int ch = ccv_clamp(CCV_GET_CHANNEL(a->type), 1, 4); double scale_x = (double)a->cols / b->cols; double scale_y = (double)a->rows / b->rows; // double scale = 1.f / (scale_x * scale_y); unsigned int inv_scale_256 = (int)(scale_x * scale_y * 0x10000); int dx, dy, sx, sy, i, k; for (dx = 0, k = 0; dx < b->cols; dx++) { double fsx1 = dx * scale_x, fsx2 = fsx1 + scale_x; int sx1 = (int)(fsx1 + 1.0 - 1e-6), sx2 = (int)(fsx2); sx1 = ccv_min(sx1, a->cols - 1); sx2 = ccv_min(sx2, a->cols - 1); if (sx1 > fsx1) { xofs[k].di = dx * ch; xofs[k].si = (sx1 - 1) * ch; xofs[k++].alpha = (unsigned int)((sx1 - fsx1) * 0x100); } for (sx = sx1; sx < sx2; sx++) { xofs[k].di = dx * ch; xofs[k].si = sx * ch; xofs[k++].alpha = 256; } if (fsx2 - sx2 > 1e-3) { xofs[k].di = dx * ch; xofs[k].si = sx2 * ch; xofs[k++].alpha = (unsigned int)((fsx2 - sx2) * 256); } } int xofs_count = k; unsigned int* buf = (unsigned int*)alloca(b->cols * ch * sizeof(unsigned int)); unsigned int* sum = (unsigned int*)alloca(b->cols * ch * sizeof(unsigned int)); for (dx = 0; dx < b->cols * ch; dx++) buf[dx] = sum[dx] = 0; dy = 0; for (sy = 0; sy < a->rows; sy++) { unsigned char* a_ptr = a->data.u8 + a->step * sy; for (k = 0; k < xofs_count; k++) { int dxn = xofs[k].di; unsigned int alpha = xofs[k].alpha; for (i = 0; i < ch; i++) buf[dxn + i] += a_ptr[xofs[k].si + i] * alpha; } if ((dy + 1) * scale_y <= sy + 1 || sy == a->rows - 1) { unsigned int beta = (int)(ccv_max(sy + 1 - (dy + 1) * scale_y, 0.f) * 256); unsigned int beta1 = 256 - beta; unsigned char* b_ptr = b->data.u8 + b->step * dy; if (beta <= 0) { for (dx = 0; dx < b->cols * ch; dx++) { b_ptr[dx] = ccv_clamp((sum[dx] + buf[dx] * 256) / inv_scale_256, 0, 255); sum[dx] = buf[dx] = 0; } } else { for (dx = 0; dx < b->cols * ch; dx++) { b_ptr[dx] = ccv_clamp((sum[dx] + buf[dx] * beta1) / inv_scale_256, 0, 255); sum[dx] = buf[dx] * beta; buf[dx] = 0; } } dy++; } else { for(dx = 0; dx < b->cols * ch; dx++) { sum[dx] += buf[dx] * 256; buf[dx] = 0; } } } }
ccv_array_t* ccv_bbf_detect_objects(ccv_dense_matrix_t* a, ccv_bbf_classifier_cascade_t** _cascade, int count, ccv_bbf_param_t params) { int hr = a->rows / ENDORSE(params.size.height); int wr = a->cols / ENDORSE(params.size.width); double scale = pow(2., 1. / (params.interval + 1.)); APPROX int next = params.interval + 1; int scale_upto = (int)(log((double)ccv_min(hr, wr)) / log(scale)); ccv_dense_matrix_t** pyr = (ccv_dense_matrix_t**)alloca(ENDORSE(scale_upto + next * 2) * 4 * sizeof(ccv_dense_matrix_t*)); memset(pyr, 0, (scale_upto + next * 2) * 4 * sizeof(ccv_dense_matrix_t*)); if (ENDORSE(params.size.height != _cascade[0]->size.height || params.size.width != _cascade[0]->size.width)) ccv_resample(a, &pyr[0], 0, a->rows * ENDORSE(_cascade[0]->size.height / params.size.height), a->cols * ENDORSE(_cascade[0]->size.width / params.size.width), CCV_INTER_AREA); else pyr[0] = a; APPROX int i; int j, k, t, x, y, q; for (i = 1; ENDORSE(i < ccv_min(params.interval + 1, scale_upto + next * 2)); i++) ccv_resample(pyr[0], &pyr[i * 4], 0, (int)(pyr[0]->rows / pow(scale, i)), (int)(pyr[0]->cols / pow(scale, i)), CCV_INTER_AREA); for (i = next; ENDORSE(i < scale_upto + next * 2); i++) ccv_sample_down(pyr[i * 4 - next * 4], &pyr[i * 4], 0, 0, 0); if (params.accurate) for (i = next * 2; ENDORSE(i < scale_upto + next * 2); i++) { ccv_sample_down(pyr[i * 4 - next * 4], &pyr[i * 4 + 1], 0, 1, 0); ccv_sample_down(pyr[i * 4 - next * 4], &pyr[i * 4 + 2], 0, 0, 1); ccv_sample_down(pyr[i * 4 - next * 4], &pyr[i * 4 + 3], 0, 1, 1); } ccv_array_t* idx_seq; ccv_array_t* seq = ccv_array_new(sizeof(ccv_comp_t), 64, 0); ccv_array_t* seq2 = ccv_array_new(sizeof(ccv_comp_t), 64, 0); ccv_array_t* result_seq = ccv_array_new(sizeof(ccv_comp_t), 64, 0); /* detect in multi scale */ for (t = 0; t < count; t++) { ccv_bbf_classifier_cascade_t* cascade = _cascade[t]; APPROX float scale_x = (float) params.size.width / (float) cascade->size.width; APPROX float scale_y = (float) params.size.height / (float) cascade->size.height; ccv_array_clear(seq); for (i = 0; ENDORSE(i < scale_upto); i++) { APPROX int dx[] = {0, 1, 0, 1}; APPROX int dy[] = {0, 0, 1, 1}; APPROX int i_rows = pyr[i * 4 + next * 8]->rows - ENDORSE(cascade->size.height >> 2); APPROX int steps[] = { pyr[i * 4]->step, pyr[i * 4 + next * 4]->step, pyr[i * 4 + next * 8]->step }; APPROX int i_cols = pyr[i * 4 + next * 8]->cols - ENDORSE(cascade->size.width >> 2); int paddings[] = { pyr[i * 4]->step * 4 - i_cols * 4, pyr[i * 4 + next * 4]->step * 2 - i_cols * 2, pyr[i * 4 + next * 8]->step - i_cols }; for (q = 0; q < (params.accurate ? 4 : 1); q++) { APPROX unsigned char* u8[] = { pyr[i * 4]->data.u8 + dx[q] * 2 + dy[q] * pyr[i * 4]->step * 2, pyr[i * 4 + next * 4]->data.u8 + dx[q] + dy[q] * pyr[i * 4 + next * 4]->step, pyr[i * 4 + next * 8 + q]->data.u8 }; for (y = 0; ENDORSE(y < i_rows); y++) { for (x = 0; ENDORSE(x < i_cols); x++) { APPROX float sum; APPROX int flag = 1; ccv_bbf_stage_classifier_t* classifier = cascade->stage_classifier; for (j = 0; j < ENDORSE(cascade->count); ++j, ++classifier) { sum = 0; APPROX float* alpha = classifier->alpha; ccv_bbf_feature_t* feature = classifier->feature; for (k = 0; k < ENDORSE(classifier->count); ++k, alpha += 2, ++feature) sum += alpha[_ccv_run_bbf_feature(feature, ENDORSE(steps), u8)]; if (ENDORSE(sum) < ENDORSE(classifier->threshold)) { flag = 0; break; } } if (ENDORSE(flag)) { ccv_comp_t comp; comp.rect = ccv_rect((int)((x * 4 + dx[q] * 2) * scale_x + 0.5), (int)((y * 4 + dy[q] * 2) * scale_y + 0.5), (int)(cascade->size.width * scale_x + 0.5), (int)(cascade->size.height * scale_y + 0.5)); comp.neighbors = 1; comp.classification.id = t; comp.classification.confidence = sum; ccv_array_push(seq, &comp); } u8[0] += 4; u8[1] += 2; u8[2] += 1; } u8[0] += paddings[0]; u8[1] += paddings[1]; u8[2] += paddings[2]; } } scale_x *= scale; scale_y *= scale; } /* the following code from OpenCV's haar feature implementation */ if(params.min_neighbors == 0) { for (i = 0; ENDORSE(i < seq->rnum); i++) { ccv_comp_t* comp = (ccv_comp_t*)ENDORSE(ccv_array_get(seq, i)); ccv_array_push(result_seq, comp); } } else { idx_seq = 0; ccv_array_clear(seq2); // group retrieved rectangles in order to filter out noise int ncomp = ccv_array_group(seq, &idx_seq, _ccv_is_equal_same_class, 0); ccv_comp_t* comps = (ccv_comp_t*)ccmalloc((ncomp + 1) * sizeof(ccv_comp_t)); memset(comps, 0, (ncomp + 1) * sizeof(ccv_comp_t)); // count number of neighbors for(i = 0; ENDORSE(i < seq->rnum); i++) { ccv_comp_t r1 = *(ccv_comp_t*)ENDORSE(ccv_array_get(seq, i)); int idx = *(int*)ENDORSE(ccv_array_get(idx_seq, i)); if (ENDORSE(comps[idx].neighbors) == 0) comps[idx].classification.confidence = r1.classification.confidence; ++comps[idx].neighbors; comps[idx].rect.x += r1.rect.x; comps[idx].rect.y += r1.rect.y; comps[idx].rect.width += r1.rect.width; comps[idx].rect.height += r1.rect.height; comps[idx].classification.id = r1.classification.id; comps[idx].classification.confidence = ccv_max(comps[idx].classification.confidence, r1.classification.confidence); } // calculate average bounding box for(i = 0; ENDORSE(i < ncomp); i++) { int n = ENDORSE(comps[i].neighbors); if(n >= params.min_neighbors) { ccv_comp_t comp; comp.rect.x = (comps[i].rect.x * 2 + n) / (2 * n); comp.rect.y = (comps[i].rect.y * 2 + n) / (2 * n); comp.rect.width = (comps[i].rect.width * 2 + n) / (2 * n); comp.rect.height = (comps[i].rect.height * 2 + n) / (2 * n); comp.neighbors = comps[i].neighbors; comp.classification.id = comps[i].classification.id; comp.classification.confidence = comps[i].classification.confidence; ccv_array_push(seq2, &comp); } } // filter out small face rectangles inside large face rectangles for(i = 0; ENDORSE(i < seq2->rnum); i++) { ccv_comp_t r1 = *(ccv_comp_t*)ENDORSE(ccv_array_get(seq2, i)); APPROX int flag = 1; for(j = 0; ENDORSE(j < seq2->rnum); j++) { ccv_comp_t r2 = *(ccv_comp_t*)ENDORSE(ccv_array_get(seq2, j)); APPROX int distance = (int)(r2.rect.width * 0.25 + 0.5); if(ENDORSE(i != j && r1.classification.id == r2.classification.id && r1.rect.x >= r2.rect.x - distance && r1.rect.y >= r2.rect.y - distance && r1.rect.x + r1.rect.width <= r2.rect.x + r2.rect.width + distance && r1.rect.y + r1.rect.height <= r2.rect.y + r2.rect.height + distance && (r2.neighbors > ccv_max(3, r1.neighbors) || r1.neighbors < 3))) { flag = 0; break; } } if(ENDORSE(flag)) ccv_array_push(result_seq, &r1); } ccv_array_free(idx_seq); ccfree(comps); } } ccv_array_free(seq); ccv_array_free(seq2); ccv_array_t* result_seq2; /* the following code from OpenCV's haar feature implementation */ if (params.flags & CCV_BBF_NO_NESTED) { result_seq2 = ccv_array_new(sizeof(ccv_comp_t), 64, 0); idx_seq = 0; // group retrieved rectangles in order to filter out noise int ncomp = ccv_array_group(result_seq, &idx_seq, _ccv_is_equal, 0); ccv_comp_t* comps = (ccv_comp_t*)ccmalloc((ncomp + 1) * sizeof(ccv_comp_t)); memset(comps, 0, (ncomp + 1) * sizeof(ccv_comp_t)); // count number of neighbors for(i = 0; ENDORSE(i < result_seq->rnum); i++) { ccv_comp_t r1 = *(ccv_comp_t*)ENDORSE(ccv_array_get(result_seq, i)); int idx = *(int*)ENDORSE(ccv_array_get(idx_seq, i)); if (ENDORSE(comps[idx].neighbors == 0 || comps[idx].classification.confidence < r1.classification.confidence)) { comps[idx].classification.confidence = r1.classification.confidence; comps[idx].neighbors = 1; comps[idx].rect = r1.rect; comps[idx].classification.id = r1.classification.id; } } // calculate average bounding box for(i = 0; ENDORSE(i < ncomp); i++) if(ENDORSE(comps[i].neighbors)) ccv_array_push(result_seq2, &comps[i]); ccv_array_free(result_seq); ccfree(comps); } else { result_seq2 = result_seq; } for (i = 1; ENDORSE(i < scale_upto + next * 2); i++) ccv_matrix_free(pyr[i * 4]); if (params.accurate) for (i = next * 2; ENDORSE(i < scale_upto + next * 2); i++) { ccv_matrix_free(pyr[i * 4 + 1]); ccv_matrix_free(pyr[i * 4 + 2]); ccv_matrix_free(pyr[i * 4 + 3]); } if (ENDORSE(params.size.height != _cascade[0]->size.height || params.size.width != _cascade[0]->size.width)) ccv_matrix_free(pyr[0]); return result_seq2; }
/* sobel filter is fundamental to many other high-level algorithms, * here includes 2 special case impl (for 1x3/3x1, 3x3) and one general impl */ void ccv_sobel(ccv_dense_matrix_t* a, ccv_dense_matrix_t** b, int type, int dx, int dy) { ccv_declare_derived_signature(sig, a->sig != 0, ccv_sign_with_format(64, "ccv_sobel(%d,%d)", dx, dy), a->sig, CCV_EOF_SIGN); type = (type == 0) ? CCV_32S | CCV_GET_CHANNEL(a->type) : CCV_GET_DATA_TYPE(type) | CCV_GET_CHANNEL(a->type); ccv_dense_matrix_t* db = *b = ccv_dense_matrix_renew(*b, a->rows, a->cols, CCV_GET_CHANNEL(a->type) | CCV_ALL_DATA_TYPE, type, sig); ccv_object_return_if_cached(, db); int i, j, k, c, ch = CCV_GET_CHANNEL(a->type); unsigned char* a_ptr = a->data.u8; unsigned char* b_ptr = db->data.u8; if (dx == 1 || dy == 1) { /* special case 1: 1x3 or 3x1 window */ if (dx > dy) { #define for_block(_for_get, _for_set) \ for (i = 0; i < a->rows; i++) \ { \ for (k = 0; k < ch; k++) \ _for_set(b_ptr, k, _for_get(a_ptr, ch + k, 0) - _for_get(a_ptr, k, 0), 0); \ for (j = 1; j < a->cols - 1; j++) \ for (k = 0; k < ch; k++) \ _for_set(b_ptr, j * ch + k, 2 * (_for_get(a_ptr, (j + 1) * ch + k, 0) - _for_get(a_ptr, (j - 1) * ch + k, 0)), 0); \ for (k = 0; k < ch; k++) \ _for_set(b_ptr, (a->cols - 1) * ch + k, _for_get(a_ptr, (a->cols - 1) * ch + k, 0) - _for_get(a_ptr, (a->cols - 2) * ch + k, 0), 0); \ b_ptr += db->step; \ a_ptr += a->step; \ } ccv_matrix_getter(a->type, ccv_matrix_setter, db->type, for_block); #undef for_block } else { #define for_block(_for_get, _for_set) \ for (j = 0; j < a->cols; j++) \ for (k = 0; k < ch; k++) \ _for_set(b_ptr, j * ch + k, _for_get(a_ptr + a->step, j * ch + k, 0) - _for_get(a_ptr, j * ch + k, 0), 0); \ a_ptr += a->step; \ b_ptr += db->step; \ for (i = 1; i < a->rows - 1; i++) \ { \ for (j = 0; j < a->cols; j++) \ for (k = 0; k < ch; k++) \ _for_set(b_ptr, j * ch + k, 2 * (_for_get(a_ptr + a->step, j * ch + k, 0) - _for_get(a_ptr - a->step, j * ch + k, 0)), 0); \ a_ptr += a->step; \ b_ptr += db->step; \ } \ for (j = 0; j < a->cols; j++) \ for (k = 0; k < ch; k++) \ _for_set(b_ptr, j * ch + k, _for_get(a_ptr, j * ch + k, 0) - _for_get(a_ptr - a->step, j * ch + k, 0), 0); ccv_matrix_getter(a->type, ccv_matrix_setter, db->type, for_block); #undef for_block } } else if (dx > 3 || dy > 3) { /* general case: in this case, I will generate a separable filter, and do the convolution */ int fsz = ccv_max(dx, dy); assert(fsz % 2 == 1); int hfz = fsz / 2; unsigned char* df = (unsigned char*)alloca(sizeof(double) * fsz); unsigned char* gf = (unsigned char*)alloca(sizeof(double) * fsz); /* the sigma calculation is linear derviation of 3x3 - 0.85, 5x5 - 1.32 */ double sigma = ((fsz - 1) / 2) * 0.47 + 0.38; double sigma2 = (2.0 * sigma * sigma); /* 2.5 is the factor to make the kernel "visible" in integer setting */ double psigma3 = 2.5 / sqrt(sqrt(2 * CCV_PI) * sigma * sigma * sigma); for (i = 0; i < fsz; i++) { ((double*)df)[i] = (i - hfz) * exp(-((i - hfz) * (i - hfz)) / sigma2) * psigma3; ((double*)gf)[i] = exp(-((i - hfz) * (i - hfz)) / sigma2) * psigma3; } if (db->type & CCV_32S) { for (i = 0; i < fsz; i++) { // df could be negative, thus, (int)(x + 0.5) shortcut will not work ((int*)df)[i] = (int)round(((double*)df)[i] * 256.0); ((int*)gf)[i] = (int)(((double*)gf)[i] * 256.0 + 0.5); } } else { for (i = 0; i < fsz; i++) { ccv_set_value(db->type, df, i, ((double*)df)[i], 0); ccv_set_value(db->type, gf, i, ((double*)gf)[i], 0); } } if (dx < dy) { unsigned char* tf = df; df = gf; gf = tf; } unsigned char* buf = (unsigned char*)alloca(sizeof(double) * ch * (fsz + ccv_max(a->rows, a->cols))); #define for_block(_for_get, _for_type_b, _for_set_b, _for_get_b) \ for (i = 0; i < a->rows; i++) \ { \ for (j = 0; j < hfz; j++) \ for (k = 0; k < ch; k++) \ _for_set_b(buf, j * ch + k, _for_get(a_ptr, k, 0), 0); \ for (j = 0; j < a->cols; j++) \ for (k = 0; k < ch; k++) \ _for_set_b(buf, (j + hfz) * ch + k, _for_get(a_ptr, j * ch + k, 0), 0); \ for (j = a->cols; j < a->cols + hfz; j++) \ for (k = 0; k < ch; k++) \ _for_set_b(buf, (j + hfz) * ch + k, _for_get(a_ptr, (a->cols - 1) * ch + k, 0), 0); \ for (j = 0; j < a->cols; j++) \ { \ for (c = 0; c < ch; c++) \ { \ _for_type_b sum = 0; \ for (k = 0; k < fsz; k++) \ sum += _for_get_b(buf, (j + k) * ch + c, 0) * _for_get_b(df, k, 0); \ _for_set_b(b_ptr, j * ch + c, sum, 8); \ } \ } \ a_ptr += a->step; \ b_ptr += db->step; \ } \ b_ptr = db->data.u8; \ for (i = 0; i < a->cols; i++) \ { \ for (j = 0; j < hfz; j++) \ for (k = 0; k < ch; k++) \ _for_set_b(buf, j * ch + k, _for_get_b(b_ptr, i * ch + k, 0), 0); \ for (j = 0; j < a->rows; j++) \ for (k = 0; k < ch; k++) \ _for_set_b(buf, (j + hfz) * ch + k, _for_get_b(b_ptr + j * db->step, i * ch + k, 0), 0); \ for (j = a->rows; j < a->rows + hfz; j++) \ for (k = 0; k < ch; k++) \ _for_set_b(buf, (j + hfz) * ch + k, _for_get_b(b_ptr + (a->rows - 1) * db->step, i * ch + k, 0), 0); \ for (j = 0; j < a->rows; j++) \ { \ for (c = 0; c < ch; c++) \ { \ _for_type_b sum = 0; \ for (k = 0; k < fsz; k++) \ sum += _for_get_b(buf, (j + k) * ch + c, 0) * _for_get_b(gf, k, 0); \ _for_set_b(b_ptr + j * db->step, i * ch + c, sum, 8); \ } \ } \ } ccv_matrix_getter(a->type, ccv_matrix_typeof_setter_getter, db->type, for_block); #undef for_block } else { /* special case 2: 3x3 window, corresponding sigma = 0.85 */ unsigned char* buf = (unsigned char*)alloca(db->step); if (dx > dy) { #define for_block(_for_get, _for_set_b, _for_get_b) \ for (j = 0; j < a->cols; j++) \ for (k = 0; k < ch; k++) \ _for_set_b(b_ptr, j * ch + k, _for_get(a_ptr + a->step, j * ch + k, 0) + 3 * _for_get(a_ptr, j * ch + k, 0), 0); \ a_ptr += a->step; \ b_ptr += db->step; \ for (i = 1; i < a->rows - 1; i++) \ { \ for (j = 0; j < a->cols; j++) \ for (k = 0; k < ch; k++) \ _for_set_b(b_ptr, j * ch + k, _for_get(a_ptr + a->step, j * ch + k, 0) + 2 * _for_get(a_ptr, j * ch + k, 0) + _for_get(a_ptr - a->step, j * ch + k, 0), 0); \ a_ptr += a->step; \ b_ptr += db->step; \ } \ for (j = 0; j < a->cols; j++) \ for (k = 0; k < ch; k++) \ _for_set_b(b_ptr, j * ch + k, 3 * _for_get(a_ptr, j * ch + k, 0) + _for_get(a_ptr - a->step, j * ch + k, 0), 0); \ b_ptr = db->data.u8; \ for (i = 0; i < a->rows; i++) \ { \ for (k = 0; k < ch; k++) \ _for_set_b(buf, k, _for_get_b(b_ptr, ch + k, 0) - _for_get_b(b_ptr, k, 0), 0); \ for (j = 1; j < a->cols - 1; j++) \ for (k = 0; k < ch; k++) \ _for_set_b(buf, j * ch + k, _for_get_b(b_ptr, (j + 1) * ch + k, 0) - _for_get_b(b_ptr, (j - 1) * ch + k, 0), 0); \ for (k = 0; k < ch; k++) \ _for_set_b(buf, (a->cols - 1) * ch + k, _for_get_b(b_ptr, (a->cols - 1) * ch + k, 0) - _for_get_b(b_ptr, (a->cols - 2) * ch + k, 0), 0); \ memcpy(b_ptr, buf, db->step); \ b_ptr += db->step; \ } ccv_matrix_getter(a->type, ccv_matrix_setter_getter, db->type, for_block); #undef for_block } else { #define for_block(_for_get, _for_set_b, _for_get_b) \ for (j = 0; j < a->cols; j++) \ for (k = 0; k < ch; k++) \ _for_set_b(b_ptr, j * ch + k, _for_get(a_ptr + a->step, j * ch + k, 0) - _for_get(a_ptr, j * ch + k, 0), 0); \ a_ptr += a->step; \ b_ptr += db->step; \ for (i = 1; i < a->rows - 1; i++) \ { \ for (j = 0; j < a->cols; j++) \ for (k = 0; k < ch; k++) \ _for_set_b(b_ptr, j * ch + k, _for_get(a_ptr + a->step, j * ch + k, 0) - _for_get(a_ptr - a->step, j * ch + k, 0), 0); \ a_ptr += a->step; \ b_ptr += db->step; \ } \ for (j = 0; j < a->cols; j++) \ for (k = 0; k < ch; k++) \ _for_set_b(b_ptr, j * ch + k, _for_get(a_ptr, j * ch + k, 0) - _for_get(a_ptr - a->step, j * ch + k, 0), 0); \ b_ptr = db->data.u8; \ for (i = 0; i < a->rows; i++) \ { \ for (k = 0; k < ch; k++) \ _for_set_b(buf, k, _for_get_b(b_ptr, ch + k, 0) + 3 * _for_get_b(b_ptr, k, 0), 0); \ for (j = 1; j < a->cols - 1; j++) \ for (k = 0; k < ch; k++) \ _for_set_b(buf, j * ch + k, _for_get_b(b_ptr, (j + 1) * ch + k, 0) + 2 * _for_get_b(b_ptr, j * ch + k, 0) + _for_get_b(b_ptr, (j - 1) * ch + k, 0), 0); \ for (k = 0; k < ch; k++) \ _for_set_b(buf, (a->cols - 1) * ch + k, _for_get_b(b_ptr, (a->cols - 2) * ch + k, 0) + 3 * _for_get_b(b_ptr, (a->cols - 1) * ch + k, 0), 0); \ memcpy(b_ptr, buf, db->step); \ b_ptr += db->step; \ } ccv_matrix_getter(a->type, ccv_matrix_setter_getter, db->type, for_block); #undef for_block } } }
void ccv_blur(ccv_dense_matrix_t* a, ccv_dense_matrix_t** b, int type, double sigma) { ccv_declare_derived_signature(sig, a->sig != 0, ccv_sign_with_format(64, "ccv_blur(%la)", sigma), a->sig, CCV_EOF_SIGN); type = (type == 0) ? CCV_GET_DATA_TYPE(a->type) | CCV_GET_CHANNEL(a->type) : CCV_GET_DATA_TYPE(type) | CCV_GET_CHANNEL(a->type); ccv_dense_matrix_t* db = *b = ccv_dense_matrix_renew(*b, a->rows, a->cols, CCV_ALL_DATA_TYPE | CCV_GET_CHANNEL(a->type), type, sig); ccv_object_return_if_cached(, db); int fsz = ccv_max(1, (int)(4.0 * sigma + 1.0 - 1e-8)) * 2 + 1; int hfz = fsz / 2; unsigned char* buf = (unsigned char*)alloca(sizeof(double) * ccv_max(fsz + a->rows, (fsz + a->cols) * CCV_GET_CHANNEL(a->type))); unsigned char* filter = (unsigned char*)alloca(sizeof(double) * fsz); double tw = 0; int i, j, k, ch = CCV_GET_CHANNEL(a->type); for (i = 0; i < fsz; i++) tw += ((double*)filter)[i] = exp(-((i - hfz) * (i - hfz)) / (2.0 * sigma * sigma)); int no_8u_type = (db->type & CCV_8U) ? CCV_32S : db->type; if (no_8u_type & CCV_32S) { tw = 256.0 / tw; for (i = 0; i < fsz; i++) ((int*)filter)[i] = (int)(((double*)filter)[i] * tw + 0.5); } else { tw = 1.0 / tw; for (i = 0; i < fsz; i++) ccv_set_value(db->type, filter, i, ((double*)filter)[i] * tw, 0); } /* horizontal */ unsigned char* a_ptr = a->data.u8; unsigned char* b_ptr = db->data.u8; #define for_block(_for_type, _for_set_b, _for_get_b, _for_set_a, _for_get_a) \ for (i = 0; i < a->rows; i++) \ { \ for (j = 0; j < hfz; j++) \ for (k = 0; k < ch; k++) \ _for_set_b(buf, j * ch + k, _for_get_a(a_ptr, k, 0), 0); \ for (j = 0; j < a->cols * ch; j++) \ _for_set_b(buf, j + hfz * ch, _for_get_a(a_ptr, j, 0), 0); \ for (j = a->cols; j < hfz + a->cols; j++) \ for (k = 0; k < ch; k++) \ _for_set_b(buf, j * ch + hfz * ch + k, _for_get_a(a_ptr, (a->cols - 1) * ch + k, 0), 0); \ for (j = 0; j < a->cols * ch; j++) \ { \ _for_type sum = 0; \ for (k = 0; k < fsz; k++) \ sum += _for_get_b(buf, k * ch + j, 0) * _for_get_b(filter, k, 0); \ _for_set_b(buf, j, sum, 8); \ } \ for (j = 0; j < a->cols * ch; j++) \ _for_set_a(b_ptr, j, _for_get_b(buf, j, 0), 0); \ a_ptr += a->step; \ b_ptr += db->step; \ } ccv_matrix_typeof_setter_getter(no_8u_type, ccv_matrix_setter, db->type, ccv_matrix_getter, a->type, for_block); #undef for_block /* vertical */ b_ptr = db->data.u8; #define for_block(_for_type, _for_set_b, _for_get_b, _for_set_a, _for_get_a) \ for (i = 0; i < a->cols * ch; i++) \ { \ for (j = 0; j < hfz; j++) \ _for_set_b(buf, j, _for_get_a(b_ptr, i, 0), 0); \ for (j = 0; j < a->rows; j++) \ _for_set_b(buf, j + hfz, _for_get_a(b_ptr + j * db->step, i, 0), 0); \ for (j = a->rows; j < hfz + a->rows; j++) \ _for_set_b(buf, j + hfz, _for_get_a(b_ptr + (a->rows - 1) * db->step, i, 0), 0); \ for (j = 0; j < a->rows; j++) \ { \ _for_type sum = 0; \ for (k = 0; k < fsz; k++) \ sum += _for_get_b(buf, k + j, 0) * _for_get_b(filter, k, 0); \ _for_set_b(buf, j, sum, 8); \ } \ for (j = 0; j < a->rows; j++) \ _for_set_a(b_ptr + j * db->step, i, _for_get_b(buf, j, 0), 0); \ } ccv_matrix_typeof_setter_getter(no_8u_type, ccv_matrix_setter_getter, db->type, for_block); #undef for_block }
ccv_decimal_point_t ccv_perspective_transform_apply(ccv_decimal_point_t point, ccv_size_t size, float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22) { m00 *= 1.0 / ccv_max(size.width, size.height); m01 *= 1.0 / ccv_max(size.width, size.height); m02 *= 1.0 / ccv_max(size.width, size.height); m10 *= 1.0 / ccv_max(size.width, size.height); m11 *= 1.0 / ccv_max(size.width, size.height); m12 *= 1.0 / ccv_max(size.width, size.height); m20 *= 1.0 / (ccv_max(size.width, size.height) * ccv_max(size.width, size.height)); m21 *= 1.0 / (ccv_max(size.width, size.height) * ccv_max(size.width, size.height)); m22 *= 1.0 / ccv_max(size.width, size.height); point.x -= size.width * 0.5; point.y -= size.height * 0.5; float wz = 1.0 / (point.x * m20 + point.y * m21 + m22); float wx = size.width * 0.5 + (point.x * m00 + point.y * m01 + m02) * wz; float wy = size.height * 0.5 + (point.x * m10 + point.y * m11 + m12) * wz; return ccv_decimal_point(wx, wy); }
// this method is a merely baseline implementation and has no optimization effort ever put into it, if at all void ccv_perspective_transform(ccv_dense_matrix_t* a, ccv_dense_matrix_t** b, int type, float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22) { ccv_declare_derived_signature(sig, a->sig != 0, ccv_sign_with_format(64, "ccv_perspective_transform(%a,%a,%a,%a,%a,%a,%a,%a,%a)", m00, m01, m02, m10, m11, m12, m20, m21, m22), a->sig, CCV_EOF_SIGN); type = (type == 0) ? CCV_GET_DATA_TYPE(a->type) | CCV_GET_CHANNEL(a->type) : CCV_GET_DATA_TYPE(type) | CCV_GET_CHANNEL(a->type); ccv_dense_matrix_t* db = *b = ccv_dense_matrix_renew(*b, a->rows, a->cols, CCV_ALL_DATA_TYPE | CCV_GET_CHANNEL(a->type), type, sig); ccv_object_return_if_cached(, db); // with default of bilinear interpolation int i, j, k, ch = CCV_GET_CHANNEL(a->type); unsigned char* a_ptr = a->data.u8; unsigned char* b_ptr = db->data.u8; // assume field of view is 60, modify the matrix value to reflect that // (basically, apply x / ccv_max(a->rows, a->cols), y / ccv_max(a->rows, a->cols) before hand m00 *= 1.0 / ccv_max(a->rows, a->cols); m01 *= 1.0 / ccv_max(a->rows, a->cols); m02 *= 1.0 / ccv_max(a->rows, a->cols); m10 *= 1.0 / ccv_max(a->rows, a->cols); m11 *= 1.0 / ccv_max(a->rows, a->cols); m12 *= 1.0 / ccv_max(a->rows, a->cols); m20 *= 1.0 / (ccv_max(a->rows, a->cols) * ccv_max(a->rows, a->cols)); m21 *= 1.0 / (ccv_max(a->rows, a->cols) * ccv_max(a->rows, a->cols)); m22 *= 1.0 / ccv_max(a->rows, a->cols); #define for_block(_for_set, _for_get) \ for (i = 0; i < db->rows; i++) \ { \ float cy = i - db->rows * 0.5; \ float crx = cy * m01 + m02; \ float cry = cy * m11 + m12; \ float crz = cy * m21 + m22; \ for (j = 0; j < db->cols; j++) \ { \ float cx = j - db->cols * 0.5; \ float wz = 1.0 / (cx * m20 + crz); \ float wx = a->cols * 0.5 + (cx * m00 + crx) * wz; \ float wy = a->rows * 0.5 + (cx * m10 + cry) * wz; \ int iwx = (int)wx; \ int iwy = (int)wy; \ wx = wx - iwx; \ wy = wy - iwy; \ if (iwx >= 0 && iwx < a->cols - 1 && iwy >= 0 && iwy < a->rows - 1) \ for (k = 0; k < ch; k++) \ _for_set(b_ptr, j * ch + k, _for_get(a_ptr + iwy * a->step, iwx * ch + k, 0) * (1 - wx) * (1 - wy) + \ _for_get(a_ptr + iwy * a->step, iwx * ch + ch + k, 0) * wx * (1 - wy) + \ _for_get(a_ptr + iwy * a->step + a->step, iwx * ch + k, 0) * (1 - wx) * wy + \ _for_get(a_ptr + iwy * a->step + a->step, iwx * ch + ch + k, 0) * wx * wy, 0); \ else \ for (k = 0; k < ch; k++) \ _for_set(b_ptr, j * ch + k, 0, 0); \ } \ b_ptr += db->step; \ } ccv_matrix_setter(db->type, ccv_matrix_getter, a->type, for_block); #undef for_block }
/* this code is a rewrite from OpenCV's legendary Lucas-Kanade optical flow implementation */ void ccv_optical_flow_lucas_kanade(ccv_dense_matrix_t* a, ccv_dense_matrix_t* b, ccv_array_t* point_a, ccv_array_t** point_b, ccv_size_t win_size, int level, double min_eigen) { assert(a && b && a->rows == b->rows && a->cols == b->cols); assert(CCV_GET_CHANNEL(a->type) == CCV_GET_CHANNEL(b->type) && CCV_GET_DATA_TYPE(a->type) == CCV_GET_DATA_TYPE(b->type)); assert(CCV_GET_CHANNEL(a->type) == 1); assert(CCV_GET_DATA_TYPE(a->type) == CCV_8U); assert(point_a->rnum > 0); level = ccv_clamp(level + 1, 1, (int)(log((double)ccv_min(a->rows, a->cols) / ccv_max(win_size.width * 2, win_size.height * 2)) / log(2.0) + 0.5)); ccv_declare_derived_signature(sig, a->sig != 0 && b->sig != 0 && point_a->sig != 0, ccv_sign_with_format(128, "ccv_optical_flow_lucas_kanade(%d,%d,%d,%la)", win_size.width, win_size.height, level, min_eigen), a->sig, b->sig, point_a->sig, CCV_EOF_SIGN); ccv_array_t* seq = *point_b = ccv_array_new(sizeof(ccv_decimal_point_with_status_t), point_a->rnum, sig); ccv_object_return_if_cached(, seq); seq->rnum = point_a->rnum; ccv_dense_matrix_t** pyr_a = (ccv_dense_matrix_t**)malloc(sizeof(ccv_dense_matrix_t*) * level); ccv_dense_matrix_t** pyr_a_dx = (ccv_dense_matrix_t**)malloc(sizeof(ccv_dense_matrix_t*) * level); ccv_dense_matrix_t** pyr_a_dy = (ccv_dense_matrix_t**)malloc(sizeof(ccv_dense_matrix_t*) * level); ccv_dense_matrix_t** pyr_b = (ccv_dense_matrix_t**)malloc(sizeof(ccv_dense_matrix_t*) * level); int i, j, t, x, y; /* generating image pyramid */ pyr_a[0] = a; pyr_a_dx[0] = pyr_a_dy[0] = 0; ccv_sobel(pyr_a[0], &pyr_a_dx[0], 0, 3, 0); ccv_sobel(pyr_a[0], &pyr_a_dy[0], 0, 0, 3); pyr_b[0] = b; for (i = 1; i < level; i++) { pyr_a[i] = pyr_a_dx[i] = pyr_a_dy[i] = pyr_b[i] = 0; ccv_sample_down(pyr_a[i - 1], &pyr_a[i], 0, 0, 0); ccv_sobel(pyr_a[i], &pyr_a_dx[i], 0, 3, 0); ccv_sobel(pyr_a[i], &pyr_a_dy[i], 0, 0, 3); ccv_sample_down(pyr_b[i - 1], &pyr_b[i], 0, 0, 0); } int* wi = (int*)malloc(sizeof(int) * win_size.width * win_size.height); int* widx = (int*)malloc(sizeof(int) * win_size.width * win_size.height); int* widy = (int*)malloc(sizeof(int) * win_size.width * win_size.height); ccv_decimal_point_t half_win = ccv_decimal_point((win_size.width - 1) * 0.5f, (win_size.height - 1) * 0.5f); const int W_BITS14 = 14, W_BITS7 = 7, W_BITS9 = 9; const float FLT_SCALE = 1.0f / (1 << 25); // clean up status to 1 for (i = 0; i < point_a->rnum; i++) { ccv_decimal_point_with_status_t* point_with_status = (ccv_decimal_point_with_status_t*)ccv_array_get(seq, i); point_with_status->status = 1; } int prev_rows, prev_cols; for (t = level - 1; t >= 0; t--) { ccv_dense_matrix_t* a = pyr_a[t]; ccv_dense_matrix_t* adx = pyr_a_dx[t]; ccv_dense_matrix_t* ady = pyr_a_dy[t]; assert(CCV_GET_DATA_TYPE(adx->type) == CCV_32S); assert(CCV_GET_DATA_TYPE(ady->type) == CCV_32S); ccv_dense_matrix_t* b = pyr_b[t]; for (i = 0; i < point_a->rnum; i++) { ccv_decimal_point_t prev_point = *(ccv_decimal_point_t*)ccv_array_get(point_a, i); ccv_decimal_point_with_status_t* point_with_status = (ccv_decimal_point_with_status_t*)ccv_array_get(seq, i); prev_point.x = prev_point.x / (float)(1 << t); prev_point.y = prev_point.y / (float)(1 << t); ccv_decimal_point_t next_point; if (t == level - 1) next_point = prev_point; else { next_point.x = point_with_status->point.x * 2 + (a->cols - prev_cols * 2) * 0.5; next_point.y = point_with_status->point.y * 2 + (a->rows - prev_rows * 2) * 0.5; } point_with_status->point = next_point; prev_point.x -= half_win.x; prev_point.y -= half_win.y; ccv_point_t iprev_point = ccv_point((int)prev_point.x, (int)prev_point.y); if (iprev_point.x < 0 || iprev_point.x >= a->cols - win_size.width - 1 || iprev_point.y < 0 || iprev_point.y >= a->rows - win_size.height - 1) { if (t == 0) point_with_status->status = 0; continue; } float xd = prev_point.x - iprev_point.x; float yd = prev_point.y - iprev_point.y; int iw00 = (int)((1 - xd) * (1 - yd) * (1 << W_BITS14) + 0.5); int iw01 = (int)(xd * (1 - yd) * (1 << W_BITS14) + 0.5); int iw10 = (int)((1 - xd) * yd * (1 << W_BITS14) + 0.5); int iw11 = (1 << W_BITS14) - iw00 - iw01 - iw10; float a11 = 0, a12 = 0, a22 = 0; unsigned char* a_ptr = (unsigned char*)ccv_get_dense_matrix_cell_by(CCV_C1 | CCV_8U, a, iprev_point.y, iprev_point.x, 0); int* adx_ptr = (int*)ccv_get_dense_matrix_cell_by(CCV_C1 | CCV_32S, adx, iprev_point.y, iprev_point.x, 0); int* ady_ptr = (int*)ccv_get_dense_matrix_cell_by(CCV_C1 | CCV_32S, ady, iprev_point.y, iprev_point.x, 0); int* wi_ptr = wi; int* widx_ptr = widx; int* widy_ptr = widy; for (y = 0; y < win_size.height; y++) { for (x = 0; x < win_size.width; x++) { wi_ptr[x] = ccv_descale(a_ptr[x] * iw00 + a_ptr[x + 1] * iw01 + a_ptr[x + a->step] * iw10 + a_ptr[x + a->step + 1] * iw11, W_BITS7); // because we use 3x3 sobel, which scaled derivative up by 4 widx_ptr[x] = ccv_descale(adx_ptr[x] * iw00 + adx_ptr[x + 1] * iw01 + adx_ptr[x + adx->cols] * iw10 + adx_ptr[x + adx->cols + 1] * iw11, W_BITS9); widy_ptr[x] = ccv_descale(ady_ptr[x] * iw00 + ady_ptr[x + 1] * iw01 + ady_ptr[x + ady->cols] + iw10 + ady_ptr[x + ady->cols + 1] * iw11, W_BITS9); a11 += (float)(widx_ptr[x] * widx_ptr[x]); a12 += (float)(widx_ptr[x] * widy_ptr[x]); a22 += (float)(widy_ptr[x] * widy_ptr[x]); } a_ptr += a->step; adx_ptr += adx->cols; ady_ptr += ady->cols; wi_ptr += win_size.width; widx_ptr += win_size.width; widy_ptr += win_size.width; } a11 *= FLT_SCALE; a12 *= FLT_SCALE; a22 *= FLT_SCALE; float D = a11 * a22 - a12 * a12; float eigen = (a22 + a11 - sqrtf((a11 - a22) * (a11 - a22) + 4.0f * a12 * a12)) / (2 * win_size.width * win_size.height); if (eigen < min_eigen || D < FLT_EPSILON) { if (t == 0) point_with_status->status = 0; continue; } D = 1.0f / D; next_point.x -= half_win.x; next_point.y -= half_win.y; ccv_decimal_point_t prev_delta; for (j = 0; j < LK_MAX_ITER; j++) { ccv_point_t inext_point = ccv_point((int)next_point.x, (int)next_point.y); if (inext_point.x < 0 || inext_point.x >= a->cols - win_size.width - 1 || inext_point.y < 0 || inext_point.y >= a->rows - win_size.height - 1) break; float xd = next_point.x - inext_point.x; float yd = next_point.y - inext_point.y; int iw00 = (int)((1 - xd) * (1 - yd) * (1 << W_BITS14) + 0.5); int iw01 = (int)(xd * (1 - yd) * (1 << W_BITS14) + 0.5); int iw10 = (int)((1 - xd) * yd * (1 << W_BITS14) + 0.5); int iw11 = (1 << W_BITS14) - iw00 - iw01 - iw10; float b1 = 0, b2 = 0; unsigned char* b_ptr = (unsigned char*)ccv_get_dense_matrix_cell_by(CCV_C1 | CCV_8U, b, inext_point.y, inext_point.x, 0); int* wi_ptr = wi; int* widx_ptr = widx; int* widy_ptr = widy; for (y = 0; y < win_size.height; y++) { for (x = 0; x < win_size.width; x++) { int diff = ccv_descale(b_ptr[x] * iw00 + b_ptr[x + 1] * iw01 + b_ptr[x + b->step] * iw10 + b_ptr[x + b->step + 1] * iw11, W_BITS7) - wi_ptr[x]; b1 += (float)(diff * widx_ptr[x]); b2 += (float)(diff * widy_ptr[x]); } b_ptr += b->step; wi_ptr += win_size.width; widx_ptr += win_size.width; widy_ptr += win_size.width; } b1 *= FLT_SCALE; b2 *= FLT_SCALE; ccv_decimal_point_t delta = ccv_decimal_point((a12 * b2 - a22 * b1) * D, (a12 * b1 - a11 * b2) * D); next_point.x += delta.x; next_point.y += delta.y; if (delta.x * delta.x + delta.y * delta.y < LK_EPSILON) break; if (j > 0 && fabs(prev_delta.x - delta.x) < 0.01 && fabs(prev_delta.y - delta.y) < 0.01) { next_point.x -= delta.x * 0.5; next_point.y -= delta.y * 0.5; break; } prev_delta = delta; } ccv_point_t inext_point = ccv_point((int)next_point.x, (int)next_point.y); if (inext_point.x < 0 || inext_point.x >= a->cols - win_size.width - 1 || inext_point.y < 0 || inext_point.y >= a->rows - win_size.height - 1) point_with_status->status = 0; else { point_with_status->point.x = next_point.x + half_win.x; point_with_status->point.y = next_point.y + half_win.y; } } prev_rows = a->rows; prev_cols = a->cols; ccv_matrix_free(adx); ccv_matrix_free(ady); if (t > 0) { ccv_matrix_free(a); ccv_matrix_free(b); } } free(widy); free(widx); free(wi); free(pyr_b); free(pyr_a_dy); free(pyr_a_dx); free(pyr_a); }
// compute harmonic mean of precision / recall of swt static void _ccv_evaluate_wolf(ccv_array_t* words, ccv_array_t* truth, ccv_swt_param_t params, double* precision, double* recall) { if (words->rnum == 0 || truth->rnum == 0) return; int j, k; double total_recall = 0, total_precision = 0; int* cG = (int*)ccmalloc(sizeof(int) * truth->rnum); int* cD = (int*)ccmalloc(sizeof(int) * words->rnum); memset(cG, 0, sizeof(int) * truth->rnum); memset(cD, 0, sizeof(int) * words->rnum); double* mG = (double*)ccmalloc(sizeof(double) * truth->rnum * words->rnum); double* mD = (double*)ccmalloc(sizeof(double) * truth->rnum * words->rnum); memset(mG, 0, sizeof(double) * truth->rnum * words->rnum); memset(mD, 0, sizeof(double) * truth->rnum * words->rnum); for (j = 0; j < truth->rnum; j++) { ccv_rect_t* rect = (ccv_rect_t*)ccv_array_get(truth, j); for (k = 0; k < words->rnum; k++) { ccv_rect_t* target = (ccv_rect_t*)ccv_array_get(words, k); int match = ccv_max(ccv_min(target->x + target->width, rect->x + rect->width) - ccv_max(target->x, rect->x), 0) * ccv_max(ccv_min(target->y + target->height, rect->y + rect->height) - ccv_max(target->y, rect->y), 0); if (match > 0) { mG[j * words->rnum + k] = (double)match / (double)(rect->width * rect->height); mD[k * truth->rnum + j] = (double)match / (double)(target->width * target->height); ++cG[j]; ++cD[k]; } } } unsigned char* tG = (unsigned char*)ccmalloc(truth->rnum); unsigned char* tD = (unsigned char*)ccmalloc(words->rnum); memset(tG, 0, truth->rnum); memset(tD, 0, words->rnum); // one to one match for (j = 0; j < truth->rnum; j++) { if (cG[j] != 1) continue; ccv_rect_t* rect = (ccv_rect_t*)ccv_array_get(truth, j); for (k = 0; k < words->rnum; k++) { if (cD[k] != 1) continue; ccv_rect_t* target = (ccv_rect_t*)ccv_array_get(words, k); if (mG[j * words->rnum + k] >= one_g && mD[k * truth->rnum + j] >= one_d) { double dx = (target->x + target->width * 0.5) - (rect->x + rect->width * 0.5); double dy = (target->y + target->height * 0.5) - (rect->y + rect->height * 0.5); double d = sqrt(dx * dx + dy * dy) * 2.0 / (sqrt(target->width * target->width + target->height * target->height) + sqrt(rect->width * rect->width + rect->height * rect->height)); if (d < center_diff_thr) { total_recall += 1.0; total_precision += 1.0; assert(tG[j] == 0); assert(tD[k] == 0); tG[j] = tD[k] = 1; } } } } int* many = (int*)ccmalloc(sizeof(int) * ccv_max(words->rnum, truth->rnum)); // one to many match, starts with ground truth for (j = 0; j < truth->rnum; j++) { if (tG[j] || cG[j] <= 1) continue; double one_sum = 0; int no_many = 0; for (k = 0; k < words->rnum; k++) { if (tD[k]) continue; double many_single = mD[k * truth->rnum + j]; if (many_single >= one_d) { one_sum += mG[j * words->rnum + k]; many[no_many] = k; ++no_many; } } if (no_many == 1) { // degrade to one to one match if (mG[j * words->rnum + many[0]] >= one_g && mD[many[0] * truth->rnum + j] >= one_d) { total_recall += 1.0; total_precision += 1.0; tG[j] = tD[many[0]] = 1; } } else if (one_sum >= one_g) { for (k = 0; k < no_many; k++) tD[many[k]] = 1; total_recall += om_one; total_precision += om_one / (1 + log(no_many)); } } // one to many match, with estimate for (k = 0; k < words->rnum; k++) { if (tD[k] || cD[k] <= 1) continue; double one_sum = 0; int no_many = 0; for (j = 0; j < truth->rnum; j++) { if (tG[j]) continue; double many_single = mG[j * words->rnum + k]; if (many_single >= one_g) { one_sum += mD[k * truth->rnum + j]; many[no_many] = j; ++no_many; } } if (no_many == 1) { // degrade to one to one match if (mG[many[0] * words->rnum + k] >= one_g && mD[k * truth->rnum + many[0]] >= one_d) { total_recall += 1.0; total_precision += 1.0; tG[many[0]] = tD[k] = 1; } } else if (one_sum >= one_g) { for (j = 0; j < no_many; j++) tG[many[j]] = 1; total_recall += om_one / (1 + log(no_many)); total_precision += om_one; } } ccfree(many); ccfree(tG); ccfree(tD); ccfree(cG); ccfree(cD); ccfree(mG); ccfree(mD); assert(total_precision < words->rnum + 0.1); assert(total_recall < truth->rnum + 0.1); if (precision) *precision = total_precision; if (recall) *recall = total_recall; }