dim_type* Get_Area_Part(dim_type bdim[],dim_type dim[],dim_type start[],dim_type end[],ndim_type ndim) { dim_type area_dim[TZ_MATRIX_MAX_DIM]; dim_type i,j; for(i=0; i<ndim; i++) { if (end == NULL) { area_dim[i] = dim[i]; } else { area_dim[i] = end[i] + 1; } if (start != NULL) { area_dim[i] -= start[i]; } } dim_type length = matrix_size(area_dim,ndim); dim_type *array = (dim_type *)malloc(sizeof(dim_type)*length); dim_type sub[TZ_MATRIX_MAX_DIM]; for(i=0; i<length; i++) { array[i] = 1; ind2sub(area_dim,ndim,i,sub); for(j=0; j<ndim; j++) { array[i] *= imin2(bdim[j]+dim[j]-2,sub[j]+start[j]+bdim[j]-1) -imax2(sub[j]+start[j],bdim[j]-2)+1; } } return array; }
cv::Scalar colorLevel(int index, hsv::Levels levels) { // TODO move these outside and pass as arguments? int numColors = levels[0] * levels[1] * levels[2]; int numGrays = levels[2] + 1; arma::ivec grays = arma::linspace<arma::ivec>(1, 256, numGrays); if (index > numColors - 1) { // grays int level = index - numColors; int value = grays(level) - 1; return cv::Scalar(value, value, value); } else { // colors hsv::Levels indices = ind2sub(levels, index); cv::Vec3f temp; for (int i = 0; i < 3; i++) { temp[i] = static_cast<float>(indices[i] + 1) / levels[i]; BOOST_ASSERT(temp[i] >= 0. and temp[i] <= 1.); } temp[0] *= 360.; cv::Mat bgr(1, 1, CV_32FC3); bgr.at<cv::Vec3f>(0) = temp; cv::cvtColor(bgr, bgr, CV_HSV2BGR); cv::Vec3f& val = bgr.at<cv::Vec3f>(0); val *= 255.; return cv::Scalar(val[0], val[1], val[2]); } }
typename Target::type operator()(Pos const& p, Size const&sz, Target const& ) const { typedef typename Target::type type; typedef typename meta::call<nt2::tag::ind2sub_(Size,Pos)>::type sub_t; sub_t const pos = ind2sub(sz,p); return nt2::splat<type>(h_*(pos[1]-1)+start_); }
typename Target::type operator()(Pos const& p, Size const&sz, Target const&) const { typedef typename Target::type type; typedef typename meta::as_index<type>::type i_t; return splat<type>( ind2sub(sz,enumerate<i_t>(p))[0] ); }
BOOST_FORCEINLINE typename Target::type operator()(Pos const& p, Size const& sz, Target const&) const { typedef typename Target::type type; typedef typename meta::call<nt2::tag::ind2sub_(Size,Pos)>::type sub_t; sub_t const pos = ind2sub(sz,p); return nt2::enumerate<type>(pos[0]); }
inline array<array<size_t, stack<3> > > ind2sub(const array_nd<T1, S1>& a, const array<T2, S2> s) { //CHECK (s <= a.length(), erange()); array<array<size_t, stack<3> > > b(s.length()); for (size_t n = 0; n < s.length(); n++) b[n] = ind2sub(a, s[n]); return b; }
inline array<array<size_t, stack<3> > > find( const array_nd<T, S>& a, const T& what) { array<array<size_t, stack<3> > > b(0); size_t n = 0; for (size_t i = 0; i < a.length(); i++) if (a[i] == what) b.push_back(ind2sub(a, i)); return b; }
double CGppe::expected_voi(const VectorXd & theta_x, const VectorXd& theta_t, const double& sigma, const MatrixXd& t, const MatrixXd & x, TypePair train_pairs, VectorXd& idx_global, VectorXd& ind_t, VectorXd& ind_x, MatrixXd test_pair, double fbest, double p_12) { int M = t.rows(); int N = x.rows(); CGppe gnew= CGppe(new CovSEard(),new CovSEard()); VectorXd idx_global_1, idx_global_2; MatrixXd tstar = t.row(M-1); double p_21, mei_12, mei_21; p_21 = 1. - p_12; train_pairs(M-1)=MatAdd(train_pairs(M-1),test_pair); compute_global_index(idx_global_1, idx_global_2, train_pairs, N); unique(idx_global, idx_global_1, idx_global_2); ind2sub(ind_x, ind_t, N, M, idx_global); gnew.Approx_CGppe_Laplace( theta_x, theta_t, sigma, t, x, train_pairs, idx_global, idx_global_1, idx_global_2, ind_t, ind_x, M, N); mei_12 = gnew.maximum_expected_improvement(theta_t, theta_x, sigma, t, x, idx_global, ind_t, ind_x, tstar, N, fbest); //dsp(mei_12,"mei12"); //recomputation fliplr(test_pair); train_pairs(M-1).bottomRows(1)=test_pair; compute_global_index(idx_global_1, idx_global_2, train_pairs, N); unique(idx_global, idx_global_1, idx_global_2); ind2sub(ind_x, ind_t, N, M, idx_global); gnew.Approx_CGppe_Laplace( theta_x, theta_t, sigma, t, x, train_pairs, idx_global, idx_global_1, idx_global_2, ind_t, ind_x, M, N); mei_21 = gnew.maximum_expected_improvement(theta_t, theta_x, sigma, t, x, idx_global, ind_t, ind_x, tstar, N, fbest); return (p_12*mei_12 + p_21*mei_21) ; }
void findEyeCenterByColorSegmentation(const Mat& image, Point2f & eyeCoord, float coordinateWeight, int kmeansIterations, int kmeansRepeats, int blurSize) { Mat img, gray_img; Mat colorpoints, kmeansPoints; img = equalizeImage(image); medianBlur(img, img, blurSize); cvtColor(image, gray_img, CV_BGR2GRAY); gray_img = imcomplement(gray_img); vector<Mat> layers(3); split(img, layers); for (int i = 0 ; i < layers.size(); i++) { layers[i] = layers[i].reshape(1,1).t(); } hconcat(layers, colorpoints); // add coordinates colorpoints.convertTo(colorpoints, CV_32FC1); Mat coordinates = matrixPointCoordinates(img.rows,img.cols,false) *coordinateWeight; hconcat(colorpoints, coordinates, kmeansPoints); Mat locIndex(img.size().area(),kmeansIterations,CV_32FC1,Scalar::all(-1)); linspace(0, img.size().area(), 1).copyTo(locIndex.col(0)); Mat index_img(img.rows,img.cols,CV_32FC1,Scalar::all(0)); Mat bestLabels, centers, clustered , colorsum , minColorPtIndex; for(int it = 1 ; it < kmeansIterations ; it++) { if (kmeansPoints.rows < 2) { break; } kmeans(kmeansPoints,2,bestLabels,TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, kmeansRepeats, 0.001),kmeansRepeats,KMEANS_PP_CENTERS,centers); reduce(centers.colRange(0, 3), colorsum, 1, CV_REDUCE_SUM); if (colorsum.at<float>(0) < colorsum.at<float>(1)) { findNonZero(bestLabels==0, minColorPtIndex); } else { findNonZero(bestLabels==1, minColorPtIndex); } minColorPtIndex = minColorPtIndex.reshape(1).col(1); for (int i = 0; i <minColorPtIndex.rows ; i ++) { locIndex.at<float>(i,it) = locIndex.at<float>(minColorPtIndex.at<int>(i),it-1); } Mat temp; for (int i = 0; i <minColorPtIndex.rows ; i ++) { temp.push_back(kmeansPoints.row(minColorPtIndex.at<int>(i))); } temp.copyTo(kmeansPoints); temp.release(); for (int i = 0 ; i < minColorPtIndex.rows ; i ++) { int r, c; ind2sub(locIndex.at<float>(i,it), index_img.cols, index_img.rows, r, c); index_img.at<float>(r,c) +=1; } } // imagesc("layered",mat2gray(index_img)); Mat layerweighted_img = index_img.mul(index_img); layerweighted_img = mat2gray(layerweighted_img); gray_img.convertTo(gray_img, CV_32FC1,1/255.0); Mat composed = gray_img.mul(layerweighted_img); float zoomRatio = 5.0f; Mat zoomed; imresize(composed, zoomRatio, zoomed); Mat score = calculateImageSymmetryScore(zoomed); // imagesc("score", score); Mat scoresum; reduce(score.rowRange(0, zoomed.cols/6), scoresum, 0, CV_REDUCE_SUM,CV_32FC1); // plotVectors("scoresum", scoresum.t()); double minVal , maxVal; Point minLoc, maxLoc; minMaxLoc(scoresum,&minVal,&maxVal,&minLoc,&maxLoc); float initialHC = (float)maxLoc.x/zoomRatio; line(zoomed, Point(maxLoc.x,0), Point(maxLoc.x,zoomed.rows-1), Scalar::all(255)); // imshow("zoomed", zoomed); int bestx = 0,bestlayer = 0; Mat bestIndex_img = index_img >=1; minMaxLoc(index_img,&minVal,&maxVal,&minLoc,&maxLoc); for (int i = 1 ; i<=maxVal; i++) { Mat indexlayer_img = index_img >=i; medianBlur(indexlayer_img, indexlayer_img, 5); erode(indexlayer_img, indexlayer_img, blurSize); erode(indexlayer_img, indexlayer_img, blurSize); indexlayer_img = removeSmallBlobs(indexlayer_img); indexlayer_img = fillHoleInBinary(indexlayer_img); indexlayer_img = fillConvexHulls(indexlayer_img); Mat score = calculateImageSymmetryScore(indexlayer_img); Mat scoresum; reduce(score.rowRange(0, indexlayer_img.cols/6), scoresum, 0, CV_REDUCE_SUM,CV_32FC1); minMaxLoc(scoresum,&minVal,&maxVal,&minLoc,&maxLoc); if (abs(maxLoc.x - initialHC) < abs(bestx - initialHC)) { if (sum(indexlayer_img)[0]/255 < indexlayer_img.size().area()/5*2 && sum(indexlayer_img)[0]/255 > indexlayer_img.size().area()/6) { bestx = maxLoc.x; bestlayer = i; bestIndex_img = indexlayer_img.clone(); } } } Point2f massCenter = findMassCenter_BinaryBiggestBlob(bestIndex_img); eyeCoord = Point2f(initialHC,massCenter.y); }
void CGppe::Elicit( const VectorXd & theta_x, const VectorXd& theta_t, const double& sigma, const MatrixXd& train_t, const MatrixXd &x, TypePair & train_pairs , const MatrixXd & test_t, int test_user_idx, MatrixXd idx_pairs, int Maxiter, const TypePair& Oracle , MatrixXd& F) { train_pairs.conservativeResize(train_pairs.rows()+1); int N = x.rows(); int Mtrain = train_t.rows(); int M = Mtrain + 1; int Npairs = idx_pairs.rows(); int Lgood; VectorXd vrand, idx_good; //VectorXd is_selected(Npairs); Matrix<bool, Dynamic, 1> is_selected(Npairs); is_selected.fill(false); VectorXd loss = VectorXd::Zero(Maxiter + 1); double loss_current; VectorXd evoi(Npairs), ind_t, ind_x; VectorXd idx_global_1, idx_global_2, idx_global; compute_global_index(idx_global_1, idx_global_2, train_pairs, N); unique(idx_global, idx_global_1, idx_global_2); ind2sub(ind_x, ind_t, N, M, idx_global); bool stop = false; double foo, val; int count = 0; MatrixXd new_pair; MatrixXd t; VectorXd p_12(Npairs); t.resize(M, train_t.cols()); t << train_t, test_t; for (int iter = 0;iter <= Maxiter;iter++) { Approx_CGppe_Laplace( theta_x, theta_t, sigma, t, x, train_pairs, idx_global, idx_global_1, idx_global_2, ind_t, ind_x, Mtrain, N); Predictive_Utility_Distribution(t, test_t, N, idx_global ); //dsp(mustar,"mustar"); //dsp(varstar,"varstar"); std::ptrdiff_t best_item_idx; foo = mustar.maxCoeff(&best_item_idx); double fbest = get_fbest(N); //dsp(fbest,"fbest"); MatrixXd test_pair; for (int i = 0;i < Npairs;i++) { Predict_CGppe_Laplace(sigma, t, x, idx_global, ind_t, ind_x, t.row(M-1), idx_pairs.row(i)); p_12(i)=p; } for (int i = 0;i < Npairs;i++) { if (is_selected(i)) { evoi(i) = INT_MIN; continue; } test_pair = idx_pairs.row(i); evoi(i) = expected_voi(theta_x, theta_t, sigma, t, x, train_pairs, idx_global, ind_t, ind_x, test_pair, fbest,p_12(i)); } //dsp(evoi,"evoi"); std::ptrdiff_t query_idx; val = evoi.maxCoeff(&query_idx); idx_good = find(evoi, val); //dsp(val,"val"); Lgood = idx_good.rows(); if ( Lgood > 1) { //vrand = randperm(Lgood); cout<<"Solving clashes at random"<<endl; //query_idx = idx_good(vrand(0)); query_idx = idx_good(0); } is_selected(query_idx) = true; //dsp(query_idx,"queryidx"); new_pair = make_query_toydata(Oracle, query_idx, test_user_idx); //adding the new pair train_pairs(M-1)=MatAdd(train_pairs(M-1),new_pair); compute_global_index(idx_global_1, idx_global_2, train_pairs, N); unique(idx_global, idx_global_1, idx_global_2); ind2sub(ind_x, ind_t, N, M, idx_global); //Computes the loss of making a recommendation at this point loss_query_toydata(loss_current, F, stop, test_user_idx, best_item_idx); loss(iter)=loss_current; count++; cout << "Query " << count << "[" << new_pair(0) << " " << new_pair(1) << "] done, Recommended Item= " << best_item_idx << ", loss=" << loss(iter) << endl; } }