void operator ()(const BlockedRange &r) const { const int num_images = static_cast<int>(features.size()); for (int i = r.begin(); i < r.end(); ++i) { int from = near_pairs[i].first; int to = near_pairs[i].second; int pair_idx = from*num_images + to; matcher(features[from], features[to], pairwise_matches[pair_idx]); pairwise_matches[pair_idx].src_img_idx = from; pairwise_matches[pair_idx].dst_img_idx = to; size_t dual_pair_idx = to*num_images + from; pairwise_matches[dual_pair_idx] = pairwise_matches[pair_idx]; pairwise_matches[dual_pair_idx].src_img_idx = to; pairwise_matches[dual_pair_idx].dst_img_idx = from; if (!pairwise_matches[pair_idx].H.empty()) pairwise_matches[dual_pair_idx].H = pairwise_matches[pair_idx].H.inv(); for (size_t j = 0; j < pairwise_matches[dual_pair_idx].matches.size(); ++j) swap(pairwise_matches[dual_pair_idx].matches[j].queryIdx, pairwise_matches[dual_pair_idx].matches[j].trainIdx); LOG("."); } }
void GaborFilterBody<_Tp>::operator() (const BlockedRange& range ) const { const complex<_Tp> i = sqrt (complex<_Tp>(-1)); _Tp magnitude; _Tp commonPart; _Tp offsetXVal; _Tp offsetYVal; complex<_Tp> complexTempResult; complex<_Tp> complexResult; for( int index=range.begin(); index!=range.end( ); ++index ) { offsetXVal = (index/mFilterSizeY) - mOffsetX; offsetYVal = (index%mFilterSizeY) - mOffsetY; magnitude = (offsetXVal)*(offsetXVal) + (offsetYVal)*(offsetYVal); commonPart = mKS * exp(mKSHalf * (magnitude)); complexTempResult = (exp(1.0*i * ((mKReal * offsetYVal) + (mKImag * offsetXVal))) - exp(-0.5 * mSSquare)); complexResult = commonPart * complexTempResult; data[index] = complexResult; } }
void operator()( const BlockedRange& range ) const { int i, i1 = range.begin(), i2 = range.end(); int m = src->rows; size_t sstep = src->step, dstep = dst->step/sizeof(float); AutoBuffer<int> _d(m); int* d = _d; for( i = i1; i < i2; i++ ) { const uchar* sptr = src->data.ptr + i + (m-1)*sstep; float* dptr = dst->data.fl + i; int j, dist = m-1; for( j = m-1; j >= 0; j--, sptr -= sstep ) { dist = (dist + 1) & (sptr[0] == 0 ? 0 : -1); d[j] = dist; } dist = m-1; for( j = 0; j < m; j++, dptr += dstep ) { dist = dist + 1 - sat_tab[dist - d[j]]; d[j] = dist; dptr[0] = sqr_tab[dist]; } } }
void operator()( const BlockedRange& range ) const { const float inf = 1e15f; int i, i1 = range.begin(), i2 = range.end(); int n = dst->cols; AutoBuffer<uchar> _buf((n+2)*2*sizeof(float) + (n+2)*sizeof(int)); float* f = (float*)(uchar*)_buf; float* z = f + n; int* v = alignPtr((int*)(z + n + 1), sizeof(int)); for( i = i1; i < i2; i++ ) { float* d = (float*)(dst->data.ptr + i*dst->step); int p, q, k; v[0] = 0; z[0] = -inf; z[1] = inf; f[0] = d[0]; for( q = 1, k = 0; q < n; q++ ) { float fq = d[q]; f[q] = fq; for(;;k--) { p = v[k]; float s = (fq + sqr_tab[q] - d[p] - sqr_tab[p])*inv_tab[q - p]; if( s > z[k] ) { k++; v[k] = q; z[k] = s; z[k+1] = inf; break; } } } for( q = 0, k = 0; q < n; q++ ) { while( z[k+1] < q ) k++; p = v[k]; d[q] = std::sqrt(sqr_tab[std::abs(q - p)] + f[p]); } } }
void operator()( const BlockedRange& range ) const { for ( int fi = range.begin(); fi < range.end(); fi++) { for( int si = 0; si < sample_count; si++ ) { valCache->at<float>(fi,si) = (*feval)( fi, si ); if ( is_buf_16u ) *(udst + fi*sample_count + si) = (unsigned short)si; else *(idst + fi*sample_count + si) = si; } if ( is_buf_16u ) icvSortUShAux( udst + fi*sample_count, sample_count, valCache->ptr<float>(fi) ); else icvSortIntAux( idst + fi*sample_count, sample_count, valCache->ptr<float>(fi) ); } }
void operator()( const BlockedRange& r ) const { vector<char> pointsMask(objectPoints.cols, 0); memset(&pointsMask[0], 1, MIN_POINTS_COUNT ); for( int i=r.begin(); i!=r.end(); ++i ) { generateVar(pointsMask); pnpTask(pointsMask, objectPoints, imagePoints, parameters, inliers, rvec, tvec, initRvec, initTvec, syncMutex); if ((int)inliers.size() >= parameters.minInliersCount) { #ifdef HAVE_TBB tbb::task::self().cancel_group_execution(); #else break; #endif } } }
void operator()( const BlockedRange& range ) const { cv::AutoBuffer<float> valCache(sample_count); float* valCachePtr = (float*)valCache; for ( int fi = range.begin(); fi < range.end(); fi++) { for( int si = 0; si < sample_count; si++ ) { valCachePtr[si] = (*feval)( fi, si ); if ( is_buf_16u ) *(udst + fi*sample_count + si) = (unsigned short)si; else *(idst + fi*sample_count + si) = si; } if ( is_buf_16u ) icvSortUShAux( udst + fi*sample_count, sample_count, valCachePtr ); else icvSortIntAux( idst + fi*sample_count, sample_count, valCachePtr ); } }
void operator()( const BlockedRange& range ) { int temp_bua = bua, temp_bna = bna; for( int i = range.begin(); i != range.end(); i++ ) { float rx1 = minx + i*dr; float rx2 = rx1 - diff.x; for( float ry1 = (float)miny; ry1 <= (float)maxy; ry1 += dr ) { float ry2 = ry1 - diff.y; //compute the distance from the ellipse center float e1 = (float)(ellipse1[0]*rx1*rx1 + 2*ellipse1[1]*rx1*ry1 + ellipse1[2]*ry1*ry1); float e2 = (float)(ellipse2[0]*rx2*rx2 + 2*ellipse2[1]*rx2*ry2 + ellipse2[2]*ry2*ry2); //compute the area if( e1<1 && e2<1 ) temp_bna++; if( e1<1 || e2<1 ) temp_bua++; } } bua = temp_bua; bna = temp_bna; }
void ForestTreeBestSplitFinder::operator()(const BlockedRange& range) { int vi, vi1 = range.begin(), vi2 = range.end(); int n = node->sample_count; CvDTreeTrainData* data = tree->get_data(); AutoBuffer<uchar> inn_buf(2*n*(sizeof(int) + sizeof(float))); CvForestTree* ftree = (CvForestTree*)tree; const CvMat* active_var_mask = ftree->forest->get_active_var_mask(); for( vi = vi1; vi < vi2; vi++ ) { CvDTreeSplit *res; int ci = data->var_type->data.i[vi]; if( node->num_valid[vi] <= 1 || (active_var_mask && !active_var_mask->data.ptr[vi]) ) continue; if( data->is_classifier ) { if( ci >= 0 ) res = ftree->find_split_cat_class( node, vi, bestSplit->quality, split, (uchar*)inn_buf ); else res = ftree->find_split_ord_class( node, vi, bestSplit->quality, split, (uchar*)inn_buf ); } else { if( ci >= 0 ) res = ftree->find_split_cat_reg( node, vi, bestSplit->quality, split, (uchar*)inn_buf ); else res = ftree->find_split_ord_reg( node, vi, bestSplit->quality, split, (uchar*)inn_buf ); } if( res && bestSplit->quality < split->quality ) memcpy( (CvDTreeSplit*)bestSplit, (CvDTreeSplit*)split, splitSize ); } }
void FastNlMeansMultiDenoisingInvoker<T>::operator() (const BlockedRange& range) const { int row_from = range.begin(); int row_to = range.end() - 1; Array3d<int> dist_sums(temporal_window_size_, search_window_size_, search_window_size_); // for lazy calc optimization Array4d<int> col_dist_sums( template_window_size_, temporal_window_size_, search_window_size_, search_window_size_); int first_col_num = -1; Array4d<int> up_col_dist_sums( cols_, temporal_window_size_, search_window_size_, search_window_size_); for (int i = row_from; i <= row_to; i++) { for (int j = 0; j < cols_; j++) { int search_window_y = i - search_window_half_size_; int search_window_x = j - search_window_half_size_; // calc dist_sums if (j == 0) { calcDistSumsForFirstElementInRow(i, dist_sums, col_dist_sums, up_col_dist_sums); first_col_num = 0; } else { // calc cur dist_sums using previous dist_sums if (i == row_from) { calcDistSumsForElementInFirstRow(i, j, first_col_num, dist_sums, col_dist_sums, up_col_dist_sums); } else { int ay = border_size_ + i; int ax = border_size_ + j + template_window_half_size_; int start_by = border_size_ + i - search_window_half_size_; int start_bx = border_size_ + j - search_window_half_size_ + template_window_half_size_; T a_up = main_extended_src_.at<T>(ay - template_window_half_size_ - 1, ax); T a_down = main_extended_src_.at<T>(ay + template_window_half_size_, ax); // copy class member to local variable for optimization int search_window_size = search_window_size_; for (int d = 0; d < temporal_window_size_; d++) { Mat cur_extended_src = extended_srcs_[d]; Array2d<int> cur_dist_sums = dist_sums[d]; Array2d<int> cur_col_dist_sums = col_dist_sums[first_col_num][d]; Array2d<int> cur_up_col_dist_sums = up_col_dist_sums[j][d]; for (int y = 0; y < search_window_size; y++) { int* dist_sums_row = cur_dist_sums.row_ptr(y); int* col_dist_sums_row = cur_col_dist_sums.row_ptr(y); int* up_col_dist_sums_row = cur_up_col_dist_sums.row_ptr(y); const T* b_up_ptr = cur_extended_src.ptr<T>(start_by - template_window_half_size_ - 1 + y); const T* b_down_ptr = cur_extended_src.ptr<T>(start_by + template_window_half_size_ + y); for (int x = 0; x < search_window_size; x++) { dist_sums_row[x] -= col_dist_sums_row[x]; col_dist_sums_row[x] = up_col_dist_sums_row[x] + calcUpDownDist( a_up, a_down, b_up_ptr[start_bx + x], b_down_ptr[start_bx + x] ); dist_sums_row[x] += col_dist_sums_row[x]; up_col_dist_sums_row[x] = col_dist_sums_row[x]; } } } } first_col_num = (first_col_num + 1) % template_window_size_; } // calc weights int weights_sum = 0; int estimation[3]; for (int channel_num = 0; channel_num < channels_count_; channel_num++) { estimation[channel_num] = 0; } for (int d = 0; d < temporal_window_size_; d++) { const Mat& esrc_d = extended_srcs_[d]; for (int y = 0; y < search_window_size_; y++) { const T* cur_row_ptr = esrc_d.ptr<T>(border_size_ + search_window_y + y); int* dist_sums_row = dist_sums.row_ptr(d, y); for (int x = 0; x < search_window_size_; x++) { int almostAvgDist = dist_sums_row[x] >> almost_template_window_size_sq_bin_shift; int weight = almost_dist2weight[almostAvgDist]; weights_sum += weight; T p = cur_row_ptr[border_size_ + search_window_x + x]; incWithWeight(estimation, weight, p); } } } if (weights_sum > 0) { for (int channel_num = 0; channel_num < channels_count_; channel_num++) { estimation[channel_num] = cvRound(((double)estimation[channel_num]) / weights_sum); } dst_.at<T>(i,j) = saturateCastFromArray<T>(estimation); } else { // weights_sum == 0 const Mat& esrc = extended_srcs_[temporal_window_half_size_]; dst_.at<T>(i,j) = esrc.at<T>(i,j); } } } }
void operator()(const BlockedRange& range) const { int y0 = range.begin(), y1 = range.end(); int ncols = src->cols, nchannels = src->channels(); AutoBuffer<float> buf(src->cols*nchannels); float alpha1 = 1.f - alphaT; float dData[CV_CN_MAX]; for( int y = y0; y < y1; y++ ) { const float* data = buf; if( cvtfunc ) cvtfunc( src->ptr(y), src->step, 0, 0, (uchar*)data, 0, Size(ncols*nchannels, 1), 0); else data = src->ptr<float>(y); float* mean = mean0 + ncols*nmixtures*nchannels*y; GMM* gmm = gmm0 + ncols*nmixtures*y; uchar* modesUsed = modesUsed0 + ncols*y; uchar* mask = dst->ptr(y); for( int x = 0; x < ncols; x++, data += nchannels, gmm += nmixtures, mean += nmixtures*nchannels ) { //calculate distances to the modes (+ sort) //here we need to go in descending order!!! bool background = false;//return value -> true - the pixel classified as background //internal: bool fitsPDF = false;//if it remains zero a new GMM mode will be added int nmodes = modesUsed[x], nNewModes = nmodes;//current number of modes in GMM float totalWeight = 0.f; float* mean_m = mean; ////// //go through all modes for( int mode = 0; mode < nmodes; mode++, mean_m += nchannels ) { float weight = alpha1*gmm[mode].weight + prune;//need only weight if fit is found //// //fit not found yet if( !fitsPDF ) { //check if it belongs to some of the remaining modes float var = gmm[mode].variance; //calculate difference and distance float dist2; if( nchannels == 3 ) { dData[0] = mean_m[0] - data[0]; dData[1] = mean_m[1] - data[1]; dData[2] = mean_m[2] - data[2]; dist2 = dData[0]*dData[0] + dData[1]*dData[1] + dData[2]*dData[2]; } else { dist2 = 0.f; for( int c = 0; c < nchannels; c++ ) { dData[c] = mean_m[c] - data[c]; dist2 += dData[c]*dData[c]; } } //background? - Tb - usually larger than Tg if( totalWeight < TB && dist2 < Tb*var ) background = true; //check fit if( dist2 < Tg*var ) { ///// //belongs to the mode fitsPDF = true; //update distribution //update weight weight += alphaT; float k = alphaT/weight; //update mean for( int c = 0; c < nchannels; c++ ) mean_m[c] -= k*dData[c]; //update variance float varnew = var + k*(dist2-var); //limit the variance varnew = MAX(varnew, varMin); varnew = MIN(varnew, varMax); gmm[mode].variance = varnew; //sort //all other weights are at the same place and //only the matched (iModes) is higher -> just find the new place for it for( int i = mode; i > 0; i-- ) { //check one up if( weight < gmm[i-1].weight ) break; //swap one up std::swap(gmm[i], gmm[i-1]); for( int c = 0; c < nchannels; c++ ) std::swap(mean[i*nchannels + c], mean[(i-1)*nchannels + c]); } //belongs to the mode - bFitsPDF becomes 1 ///// } }//!bFitsPDF) //check prune if( weight < -prune ) { weight = 0.0; nmodes--; } gmm[mode].weight = weight;//update weight by the calculated value totalWeight += weight; } //go through all modes ////// //renormalize weights totalWeight = 1.f/totalWeight; for( int mode = 0; mode < nmodes; mode++ ) { gmm[mode].weight *= totalWeight; } nmodes = nNewModes; //make new mode if needed and exit if( !fitsPDF ) { // replace the weakest or add a new one int mode = nmodes == nmixtures ? nmixtures-1 : nmodes++; if (nmodes==1) gmm[mode].weight = 1.f; else { gmm[mode].weight = alphaT; // renormalize all other weights for( int i = 0; i < nmodes-1; i++ ) gmm[i].weight *= alpha1; } // init for( int c = 0; c < nchannels; c++ ) mean[mode*nchannels + c] = data[c]; gmm[mode].variance = varInit; //sort //find the new place for it for( int i = nmodes - 1; i > 0; i-- ) { // check one up if( alphaT < gmm[i-1].weight ) break; // swap one up std::swap(gmm[i], gmm[i-1]); for( int c = 0; c < nchannels; c++ ) std::swap(mean[i*nchannels + c], mean[(i-1)*nchannels + c]); } } //set the number of modes modesUsed[x] = uchar(nmodes); mask[x] = background ? 0 : detectShadows && detectShadowGMM(data, nchannels, nmodes, gmm, mean, Tb, TB, tau) ? shadowVal : 255; } } }
void operator()( const BlockedRange& range ) const { for ( int fi = range.begin(); fi < range.end(); fi++) for( int si = 0; si < sample_count; si++ ) valCache->at<float>(fi,si) = (*feval)( fi, si ); }