void writeCameraFile(const string &filename, const Mat &cameraMatrix, const Mat &distCoeffs, const Mat &R, const Mat &t) { FileStorage cameraFS(filename, FileStorage::WRITE); CV_Assert(cameraFS.isOpened()); cameraFS << "camera" << "{"; cameraFS << "K" << cameraMatrix; cameraFS << "D" << distCoeffs; cameraFS << "pose" << "{"; Mat rvec; Rodrigues(R, rvec); cameraFS << "rvec" << rvec; cameraFS << "tvec" << t.reshape(1, 3); cameraFS << "estimated" << true; cameraFS << "}" << "}"; cameraFS.release(); }
static Mat histc_(const Mat& src, int minVal=0, int maxVal=255, bool normed=false) { Mat result; // Establish the number of bins. int histSize = maxVal-minVal+1; // Set the ranges. float range[] = { minVal, maxVal+1 } ; const float* histRange = { range }; // calc histogram calcHist(&src, 1, 0, Mat(), result, 1, &histSize, &histRange, true, false); // normalize if(normed) { result /= src.total(); } return result.reshape(1,1); }
void graphcut1(Mat& im, Mat& probs, Mat& dx, Mat& dy,int num_lables,Mat& lables = Mat()) { GCoptimizationGridGraph gc(im.cols,im.rows,num_lables); int N = im.cols*im.rows; //probs = probs.reshape(1,N); double log2 = log(1.3); for(int i=0; i<N; i++) { double* ppt = probs.ptr<double>(i); for(int l=0; l<num_lables; l++) { int icost = MAX(0,(int)floor(-log(ppt[l])/log2)); gc.setDataCost(i,l,icost); } } Mat Sc = 5.0 * (Mat::ones(num_lables,num_lables,CV_32FC1) - Mat::eye(num_lables,num_lables,CV_32FC1)); //int score[9] = {0,50,50, // 50,0,50, // 50,50,0}; gc.setSmoothCostVH((float*)(/*score*/Sc.data),(float*)dx.data,(float*)dy.data); lables.create(N,1,CV_8UC1); while(true) { printf("\nBefore optimization energy is %d\n",gc.compute_energy()); gc.expansion(1);// run expansion for 2 iterations. For swap use gc->swap(num_iterations); printf("\nAfter optimization energy is %d\n",gc.compute_energy()); for ( int i = 0; i < N; i++ ) ((uchar*)(lables.data + lables.step * i))[0] = gc.whatLabel(i); { Mat _tmp = lables.reshape(1,im.rows); { vector<Mat> chns(3); for(unsigned int ch=0; ch<chns.size(); ch++) chns[ch] = (_tmp == ch); Mat _tmpUC; cv::merge(chns,_tmpUC); imshow("tmp", _tmpUC); cout << "Press 'q' to finish GC iterations"<<endl; int c = waitKey(); if(c=='q') break; } } } }
PERF_TEST_P( EstimateAffine, EstimateAffinePartial2D, ESTIMATE_PARAMS ) { AffineParams params = GetParam(); const int n = get<0>(params); const double confidence = get<1>(params); const int method = get<2>(params); const size_t refining = get<3>(params); Mat aff = rngPartialAffMat(); int m; // LMEDS can't handle more than 50% outliers (by design) if (method == LMEDS) m = 3*n/5; else m = 2*n/5; const float shift_outl = 15.f; const float noise_level = 20.f; Mat fpts(1, n, CV_32FC2); Mat tpts(1, n, CV_32FC2); randu(fpts, 0., 100.); transform(fpts, tpts, aff); /* adding noise*/ Mat outliers = tpts.colRange(m, n); outliers.reshape(1) += shift_outl; Mat noise (outliers.size(), outliers.type()); randu(noise, 0., noise_level); outliers += noise; Mat aff_est; vector<uchar> inliers (n); warmup(inliers, WARMUP_WRITE); warmup(fpts, WARMUP_READ); warmup(tpts, WARMUP_READ); TEST_CYCLE() { aff_est = estimateAffinePartial2D(fpts, tpts, inliers, method, 3, 2000, confidence, refining); } // we already have accuracy tests SANITY_CHECK_NOTHING(); }
//Calculate Airlight Vec<float,3> Airlight(Mat img, Mat dark) { int n_bright=_topbright*SizeH_W; Mat dark_1=dark.reshape(1,SizeH_W); Vector<int> max_idx; float max_num=0; int max_pos=0; Vec<float,3> a; Vec<float,3> A(0,0,0); Mat RGBPixcels=Mat::ones(n_bright,1,CV_32FC3); Mat HLSPixcels=Mat::ones(n_bright,1,CV_32FC3); Mat IdxPixcels=Mat::ones(n_bright,1,CV_32SC1); for(int i=0;i<n_bright;i++) { max_num=0; max_idx.push_back(max_num); for(float * p = (float *)dark_1.datastart;p!=(float *)dark_1.dataend;p++) { if(*p>max_num) { max_num = *p; max_idx[i] = (p-(float *)dark_1.datastart); RGBPixcels.at<Vec<float,3>>(i,0) = ((Vec<float,3> *)img.data)[max_idx[i]]; IdxPixcels.at<int>(i,0) = (p-(float *)dark_1.datastart); //((Vec<float,3> *)img.data)[max_idx[i]] = Vec<float,3>(0,0,1); } } ((float *)dark_1.data)[max_idx[i]]=0; } float maxL=0.0; //int maxIdx=0; for(int j=0; j<n_bright; j++) { A[0]+=RGBPixcels.at<Vec<float,3>>(j,0)[0]; A[1]+=RGBPixcels.at<Vec<float,3>>(j,0)[1]; A[2]+=RGBPixcels.at<Vec<float,3>>(j,0)[2]; } A[0]/=n_bright; A[1]/=n_bright; A[2]/=n_bright; return A; }
/* Preprocesses the image; If image is a face image then select face ROI, resize it to 64x64 and make and return a column vector. If image is a palm image then just resize it to 64x64 and make and return a column vector. */ Mat impreprocess(Mat &img, int type){ Mat vector; int size = 64; if (type == FACE){ Rect roi(40,50,100,130); vector = img(roi); resize(vector, vector, Size(size, size)); } else if (type == PALM){ resize(img, vector, Size(size, size)); //equalizeHist (vector, vector); } vector = vector.reshape(0, size * size); return vector; }
static void RTToLie(InputArray _R, InputArray _T, OutputArray Lie ){ Mat R = _R.getMat(); Mat T = _T.getMat(); Lie.create(1,6,T.type()); Mat p = Lie.getMat(); CV_Assert(p.size()==Size(6,1)); p=p.reshape(1,6); if(T.rows==1){ T = T.t(); } rodrigues(R).copyTo(p.rowRange(Range(0,3))); T.copyTo(p.rowRange(Range(3,6))); CV_Assert(Lie.size()==Size(6,1)); }
void SurfFeaturesFinder::find(const Mat &image, ImageFeatures &features) { Mat gray_image; CV_Assert(image.type() == CV_8UC3); cvtColor(image, gray_image, CV_BGR2GRAY); if (surf == 0) { detector_->detect(gray_image, features.keypoints); extractor_->compute(gray_image, features.keypoints, features.descriptors); } else { Mat descriptors; (*surf)(gray_image, Mat(), features.keypoints, descriptors); features.descriptors = descriptors.reshape(1, (int)features.keypoints.size()); } }
CostVolume::CostVolume(Mat image, FrameID _fid, int _layers, float _near, float _far, cv::Mat R, cv::Mat T, cv::Mat _cameraMatrix, float initialCost, float initialWeight): R(R),T(T),initialWeight(initialWeight) { //For performance reasons, OpenDTAM only supports multiple of 32 image sizes with cols >= 64 CV_Assert(image.rows % 32 == 0 && image.cols % 32 == 0 && image.cols >= 64); // CV_Assert(_layers>=8); checkInputs(R, T, _cameraMatrix); fid = _fid; rows = image.rows; cols = image.cols; layers = _layers; near = _near; far = _far; depthStep = (near - far) / (layers - 1); cameraMatrix = _cameraMatrix.clone(); solveProjection(R, T); FLATALLOC(lo); FLATALLOC(hi); FLATALLOC(loInd); dataContainer.create(layers, rows * cols, CV_32FC1); Mat bwImage; image=image.reshape(0,1); cv::cvtColor(image, bwImage, CV_RGB2GRAY); baseImage.upload(image); baseImageGray.upload(bwImage); baseImage=baseImage.reshape(0,rows); baseImageGray=baseImageGray.reshape(0,rows); loInd.setTo(Scalar(0, 0, 0),cvStream); dataContainer.setTo(Scalar(initialCost),cvStream); data = (float*) dataContainer.data; hits = (float*) hitContainer.data; count = 0; //messy way to disguise cuda objects _cuArray=Ptr<char>((char*)(new cudaArray_t)); *((cudaArray**)(char*)_cuArray)=0; _texObj=Ptr<char>((char*)(new cudaTextureObject_t)); *((cudaTextureObject_t*)(char*)_texObj)=0; ref=Ptr<char>(new char); }
void myPCA::recoginize(double dis, double threshold, Mat& disMatrix) { float min = disMatrix.at<float>(0,0); int minCol = 0; int flagKnow = 1; int flagUnknow = 1; int i; for(i = 0; i < disMatrix.cols; ++i) { float x = disMatrix.at<float>(0,i); if(x < threshold) { flagUnknow = 0; if(min > x) { min = x; minCol = i; } } else { flagKnow = 0; } if(flagKnow + flagUnknow == 0) { cout << "No I don't know this guy!" << endl; break; } } if(flagKnow) { cout << "Yes I know this guy!" <<endl; cout << "It's the "<<trainImageID.at<short>(0,minCol) << "th person in the database."; Mat imgFace = sampleMatrixOri.col(minCol).clone(); Mat toShow; imgFace.reshape(1,ROWDB).copyTo(toShow); toShow = toGrayscale(toShow); imshow("recognize",toShow); waitKey(0); } if(flagUnknow) { cout << "Sorry I think it's not a human face" <<endl; } }
TEST_P(EstimateAffinePartial2D, testNPoints) { // try more transformations for (size_t i = 0; i < 500; ++i) { Mat aff = rngPartialAffMat(); const int method = GetParam(); const int n = 100; int m; // LMEDS can't handle more than 50% outliers (by design) if (method == LMEDS) m = 3*n/5; else m = 2*n/5; const float shift_outl = 15.f; const float noise_level = 20.f; Mat fpts(1, n, CV_32FC2); Mat tpts(1, n, CV_32FC2); randu(fpts, 0., 100.); transform(fpts, tpts, aff); /* adding noise to some points */ Mat outliers = tpts.colRange(m, n); outliers.reshape(1) += shift_outl; Mat noise (outliers.size(), outliers.type()); randu(noise, 0., noise_level); outliers += noise; vector<uchar> inliers; Mat aff_est = estimateAffinePartial2D(fpts, tpts, inliers, method); EXPECT_FALSE(aff_est.empty()); EXPECT_NEAR(0., cvtest::norm(aff_est, aff, NORM_INF), 1e-4); bool inliers_good = count(inliers.begin(), inliers.end(), 1) == m && m == accumulate(inliers.begin(), inliers.begin() + m, 0); EXPECT_TRUE(inliers_good); } }
void myPCA::show10EigenFaces() { Mat all; for (int i =0; i < min(10, eigenVectors.cols); i++) { Mat imgFace = eigenVectors.col(i).clone(); if(i == 0) all = imgFace.clone(); else all = all + imgFace; } all.reshape(1, ROW).copyTo(all); all = toGrayscale(all); resize(all,all,Size(COL*4,ROW*4),0,0,1); imshow("Top 10",all); waitKey(0); }
//------------------------------------------------------------------------------ // cv::subspace::project //------------------------------------------------------------------------------ Mat cv::subspace::project(InputArray _W, InputArray _mean, InputArray _src) { // get data matrices Mat W = _W.getMat(); Mat mean = _mean.getMat(); Mat src = _src.getMat(); // create temporary matrices Mat X, Y; // copy data & make sure we are using the correct type src.convertTo(X, W.type()); // get number of samples and dimension int n = X.rows; int d = X.cols; // center the data if correct aligned sample mean is given if(mean.total() == d) subtract(X, repeat(mean.reshape(1,1), n, 1), X); // finally calculate projection as Y = (X-mean)*W gemm(X, W, 1.0, Mat(), 0.0, Y); return Y; }
CostVolume::CostVolume(Mat image, FrameID _fid, int _layers, float _near, float _far, cv::Mat R, cv::Mat T, cv::Mat _cameraMatrix, float initialCost, float initialWeight): R(R),T(T),initialWeight(initialWeight),_cuArray((char*)0) { //For performance reasons, OpenDTAM only supports multiple of 32 image sizes with cols >= 64 CV_Assert(image.rows % 32 == 0 && image.cols % 32 == 0 && image.cols >= 64); CV_Assert(_layers>=8); checkInputs(R, T, _cameraMatrix); fid = _fid; rows = image.rows; cols = image.cols; layers = _layers; near = _near; far = _far; depthStep = (near - far) / (layers - 1); cameraMatrix = _cameraMatrix.clone(); solveProjection(R, T); FLATALLOC(lo); FLATALLOC(hi); FLATALLOC(loInd); dataContainer.create(layers, rows * cols, CV_32FC1); GpuMat tmp; baseImage.upload(image.reshape(0,1),cvStream); cv::cuda::cvtColor(baseImage,baseImageGray,COLOR_BGR2GRAY,0,cvStream); baseImage=baseImage.reshape(0,rows); baseImageGray=baseImageGray.reshape(0,rows); cudaSafeCall(cudaMemsetAsync(loInd.data,0,rows*cols*sizeof(float),cv::cuda::StreamAccessor::getStream(cvStream))); dataContainer.setTo(initialCost,cvStream); data = (float*) dataContainer.data; //hitContainer.create(layers, rows * cols, CV_32FC1); //hitContainer = initialWeight; hits = (float*) hitContainer.data; count = 0; //messy way to disguise cuda objects _cuArray=Ptr<char>((char*)(new cudaArray*)); *((cudaArray**)(char*)_cuArray)=0; _texObj=Ptr<char>((char*)(new cudaTextureObject_t)); *((cudaTextureObject_t*)(char*)_texObj)=0; }
// I am taking a dataset of only 10 images void createInputVec(Mat_<float> features,Mat_<int> labels) { Mat img; char file[255]; for (int j = 0; j < 10; j++) { sprintf(file, "%s%d.png", PATH, j); img = imread(file, 1); if (!img.data) { std::cout << "File " << file << " not found\n"; exit(1); } img = trainPrePos(img); img = img.reshape(1,1); for(int i=0;i<SX*SY;++i) { features.at<float>(j,i)=float(img.at<uchar>(0,i)); } labels.at<int>(0,j)=j; } }
inline static void to_nv12(cv::InputArray bgr, cv::Mat & Y, cv::Mat & UV) { const int height = bgr.rows(); const int width = bgr.cols(); Mat yuv; cvtColor(bgr, yuv, CV_BGR2YUV_I420); CV_Assert(yuv.isContinuous()); Mat Y_(Y, Rect(0, 0, width, height)); yuv.rowRange(0, height).copyTo(Y_); Mat UV_planar(height, width / 2, CV_8UC1, yuv.ptr(height)); Mat u_and_v[2] = { UV_planar.rowRange(0, height / 2), UV_planar.rowRange(height / 2, height), }; Mat uv; cv::merge(u_and_v, 2, uv); Mat UV_(UV, Rect(0, 0, width, height / 2)); uv.reshape(1).copyTo(UV_); }
Mat calcRvec(const vector<Point3f>& points, const Size& cornerSize) { Point3f p00 = points[0]; Point3f p10 = points[1]; Point3f p01 = points[cornerSize.width]; Vec3d ex(p10.x - p00.x, p10.y - p00.y, p10.z - p00.z); Vec3d ey(p01.x - p00.x, p01.y - p00.y, p01.z - p00.z); Vec3d ez = ex.cross(ey); Mat rot(3, 3, CV_64F); *rot.ptr<Vec3d>(0) = ex; *rot.ptr<Vec3d>(1) = ey; *rot.ptr<Vec3d>(2) = ez * (1.0/norm(ez)); Mat res; Rodrigues(rot.t(), res); return res.reshape(1, 1); }
static void onTrackbar(int pos, void* ptr) { cout << "Retained Variance = " << pos << "% "; cout << "re-calculating PCA..." << std::flush; double var = pos / 100.0; struct params *p = (struct params *)ptr; p->pca = PCA(p->data, cv::Mat(), CV_PCA_DATA_AS_ROW, var); Mat point = p->pca.project(p->data.row(0)); Mat reconstruction = p->pca.backProject(point); reconstruction = reconstruction.reshape(p->ch, p->rows); reconstruction = toGrayscale(reconstruction); imshow(p->winName, reconstruction); cout << "done! # of principal components: " << p->pca.eigenvectors.rows << endl; }
void getLBPplusHistFeatures(const Mat& image, Mat& features) { // TODO Mat grayImage; cvtColor(image, grayImage, CV_RGB2GRAY); Mat lbpimage; lbpimage = libfacerec::olbp(grayImage); Mat lbp_hist = libfacerec::spatial_histogram(lbpimage, 64, 8, 4); //features = lbp_hist.reshape(1, 1); Mat greyImage; cvtColor(image, greyImage, CV_RGB2GRAY); //Mat src_hsv; //cvtColor(image, src_hsv, CV_BGR2HSV); //std::vector<cv::Mat> hsvSplit; //split(src_hsv, hsvSplit); /*std::vector<cv::Mat> bgrSplit; split(image, bgrSplit);*/ //grayImage = histeq(grayImage); Mat img_threshold; threshold(greyImage, img_threshold, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY); Mat histomFeatures = getHistogram(img_threshold); /*Mat img_threshold2; threshold(bgrSplit[1], img_threshold2, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY); Mat greenHistomFeatures = getTheFeatures(img_threshold2); Mat histomFeatures; hconcat(blueHistomFeatures.reshape(1, 1), greenHistomFeatures.reshape(1, 1), histomFeatures);*/ //Mat histomFeatures = getTheColorFeatures(greyImage); //features.push_back(histomFeatures.reshape(1, 1)); hconcat(lbp_hist.reshape(1, 1), histomFeatures.reshape(1, 1), features); //std::cout << features << std::endl; //features = histomFeatures; }
int main (int argc, char **argv) { const int cluster_count = 10; /* number of cluster */ // (1)load a specified file as a 3-channel color image const char *imagename = argc > 1 ? argv[1] : "../image/boat.png"; Mat src_img = imread(imagename); if(!src_img.data) return -1; // (2)reshape the image to be a 1 column matrix Mat points; src_img.convertTo(points, CV_32FC3); points = points.reshape(3, src_img.rows*src_img.cols); // (3)run k-means clustering algorithm to segment pixels in RGB color space Mat_<int> clusters(points.size(), CV_32SC1); Mat centers; kmeans(points, cluster_count, clusters, cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0), 1, KMEANS_PP_CENTERS, ¢ers); // (4)make a each centroid represent all pixels in the cluster Mat dst_img(src_img.size(), src_img.type()); MatIterator_<Vec3f> itf = centers.begin<Vec3f>(); MatIterator_<Vec3b> itd = dst_img.begin<Vec3b>(), itd_end = dst_img.end<Vec3b>(); for(int i=0; itd != itd_end; ++itd, ++i) { Vec3f color = itf[clusters(1,i)]; (*itd)[0] = saturate_cast<uchar>(color[0]); (*itd)[1] = saturate_cast<uchar>(color[1]); (*itd)[2] = saturate_cast<uchar>(color[2]); } // (5)show source and destination image, and quit when any key pressed namedWindow("src_img", CV_WINDOW_AUTOSIZE); imshow("src_img", src_img); namedWindow("dst_img", CV_WINDOW_AUTOSIZE); imshow("dst_img", dst_img); waitKey(0); return 0; }
void BMS::whitenFeatMap(const cv::Mat& img, float reg) { assert(img.channels() == 3 && img.type() == CV_8UC3); vector<Mat> featureMaps; if (!mWhitening) { split(img, featureMaps); for (int i = 0; i < featureMaps.size(); i++) { normalize(featureMaps[i], featureMaps[i], 255.0, 0.0, NORM_MINMAX); medianBlur(featureMaps[i], featureMaps[i], 3); mFeatureMaps.push_back(featureMaps[i]); } return; } Mat srcF,meanF,covF; img.convertTo(srcF, CV_32FC3); Mat samples = srcF.reshape(1, img.rows*img.cols); calcCovarMatrix(samples, covF, meanF, CV_COVAR_NORMAL | CV_COVAR_ROWS | CV_COVAR_SCALE, CV_32F); covF += Mat::eye(covF.rows, covF.cols, CV_32FC1)*reg; SVD svd(covF); Mat sqrtW; sqrt(svd.w,sqrtW); Mat sqrtInvCovF = svd.u * Mat::diag(1.0/sqrtW); Mat whitenedSrc = srcF.reshape(1, img.rows*img.cols)*sqrtInvCovF; whitenedSrc = whitenedSrc.reshape(3, img.rows); split(whitenedSrc, featureMaps); for (int i = 0; i < featureMaps.size(); i++) { normalize(featureMaps[i], featureMaps[i], 255.0, 0.0, NORM_MINMAX); featureMaps[i].convertTo(featureMaps[i], CV_8U); medianBlur(featureMaps[i], featureMaps[i], 3); mFeatureMaps.push_back(featureMaps[i]); } }
/** * Main entry called from Matlab * @param nlhs number of left-hand-side arguments * @param plhs pointers to mxArrays in the left-hand-side * @param nrhs number of right-hand-side arguments * @param prhs pointers to mxArrays in the right-hand-side */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { // Check the number of arguments nargchk(nrhs>=1 && (nrhs%2)==1 && nlhs<=1); // Argument vector vector<MxArray> rhs(prhs, prhs+nrhs); // Option processing bool clockwise = false; bool returnPoints = true; for (int i=1; i<nrhs; i+=2) { string key(rhs[i].toString()); if (key=="Clockwise") clockwise = rhs[i+1].toBool(); else if (key=="ReturnPoints") returnPoints = rhs[i+1].toBool(); else mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option"); } // Process if (rhs[0].isNumeric()) { Mat points(rhs[0].toMat(rhs[0].isSingle() ? CV_32F : CV_32S)); Mat hull; // either points or indices depending on returnPoints convexHull(points, hull, clockwise, returnPoints); plhs[0] = MxArray(hull.reshape(1,0)); // Nx2 or Nx1 } else if (rhs[0].isCell()) { vector<Point> points(rhs[0].toVector<Point>()); if (returnPoints) { vector<Point> hull; convexHull(points, hull, clockwise, returnPoints); plhs[0] = MxArray(hull); } else { vector<int> hull; convexHull(points, hull, clockwise, returnPoints); plhs[0] = MxArray(hull); } } }
Ptr<FaceRecognizer> Train(vector<Mat> images, vector<int> labels, vector<Mat> testimages, vector<int> testlabels) { Ptr<FaceRecognizer> model = createEigenFaceRecognizer(10);//10 Principal components cout<<"train"<<endl; model->train(images,labels); Mat eigenvalues = model->getMat("eigenvalues"); // And we can do the same to display the Eigenvectors (read Eigenfaces): Mat W = model->getMat("eigenvectors"); // From this we will display the (at most) first 10 Eigenfaces: for (int i = 0; i < min(15, W.cols); i++) { string msg = format("Eigenvalue #%d = %.5f", i, eigenvalues.at<double>(i)); cout << msg << endl; // get eigenvector #i Mat ev = W.col(i).clone(); // Reshape to original size & normalize to [0...255] for imshow. Mat grayscale = toGrayscale(ev.reshape(1, images[0].rows)); // Show the image & apply a Jet colormap for better sensing. Mat cgrayscale; applyColorMap(grayscale, cgrayscale, COLORMAP_JET); imshow(format("%d", i), cgrayscale); } waitKey(0); int i,acc=0,predict_l; for (i=0;i<testimages.size();i++) { predict_l = model->predict(testimages[i]); if(predict_l != testlabels[i]) { cout<<"An error in recognition: sample "<<i+1<<", predict "<< predict_l<<", groundtruth "<<testlabels[i]<<endl; imshow("error 1",testimages[i]); waitKey(); } else acc++; } cout<<"Recognition Rate: "<<acc*1.0/testimages.size()<<endl; return model; }
// Propose measurements from liklihood std::vector<int> HandTracker::proposeMeasurements(cv::Mat L_image, int N) { std::vector<int> bins; // Convert likelihood to weights Mat likelihood; threshold(L_image, L_image, 255.0/2.0, 255.0, cv::THRESH_BINARY); if (cv::sum(L_image)[0]/(255.0*L_image.rows*L_image.cols)<1e-4) { return bins; } L_image.convertTo(likelihood, CV_64F, 1.0/255.0, 0); Mat vec = likelihood.reshape(0,1); vec = vec/sum(vec)[0]; std::vector<double> weights(vec); bins = resample(weights, N); return bins;//measurements; }
TEST(photoeffects_glow, regression) { string input = cvtest::TS::ptr()->get_data_path() + "photoeffects/glow_test.png"; string expectedOutput = cvtest::TS::ptr()->get_data_path() + "photoeffects/glow_test_result.png"; Mat image, rightDst; image = imread(input, CV_LOAD_IMAGE_COLOR); rightDst = imread(expectedOutput, CV_LOAD_IMAGE_COLOR); if (image.empty()) FAIL() << "Can't read " + input + " image"; if (rightDst.empty()) FAIL() << "Can't read " + expectedOutput + " image"; Mat dst; glow(image, dst, 33, 0.9f); Mat diff = abs(rightDst - dst); Mat mask = diff.reshape(1) > 1; EXPECT_EQ(0, countNonZero(mask)); }
void MyTabThree::do1ChnHist(const Mat& _i, const Mat& mask, double* h, double* cdf) { Mat _t = _i.reshape(1,1); // Get the histogram with or without the mask if (true) { cv::Mat _tm; mask.copyTo(_tm); _tm = _tm.reshape(1,1); for(int p=0; p<_t.cols; p++) { uchar m = _tm.at<uchar>(0,p); if(m > 0) { // Mask value uchar c = _t.at<uchar>(0,p); // Image value h[c] += 1.0; } } } else { for(int p=0; p<_t.cols; p++) { uchar c = _t.at<uchar>(0,p); // Image value h[c] += 1.0; } } //normalize hist Mat _tmp(1,256,CV_64FC1,h); double minVal,maxVal; minMaxLoc(_tmp,&minVal,&maxVal); _tmp = _tmp / maxVal; cdf[0] = h[0]; for(int j=1; j < 256; j++) { cdf[j] = cdf[j-1]+h[j]; } //normalize CDF _tmp.data = (uchar*)cdf; minMaxLoc(_tmp,&minVal,&maxVal); _tmp = _tmp / maxVal; }
void forward(std::vector<Mat*> &inputs, std::vector<Mat> &outputs, std::vector<Mat> &internals) { CV_TRACE_FUNCTION(); CV_TRACE_ARG_VALUE(name, "name", name.c_str()); for (size_t i = 0; i < inputs.size(); i++) { Mat srcBlob = *inputs[i]; MatShape inputShape = shape(srcBlob), outShape = shape(outputs[i]); if (performReordering) { float *dstData = internals[i].ptr<float>(); const float *srcData = srcBlob.ptr<float>(); int num = inputShape[0], channels = inputShape[1], height = inputShape[2], width = inputShape[3]; int total = num*channels*height*width; for(int i_n = 0; i_n < num; i_n++) { for(int i_c = 0; i_c < channels; i_c++) { for(int i_h = 0; i_h < height; i_h++) { for(int i_w = 0; i_w < width; i_w++) { int src_i = channels*height*width*i_n + height*width*i_c + width*i_h + i_w; int dst_i = channels*height*width*i_n + i_c + channels*width*i_h + channels*i_w; CV_Assert(dst_i < total); CV_Assert(src_i < total); dstData[dst_i] = srcData[src_i]; } } } } internals[i].copyTo(outputs[i]); } else { if (outputs[i].data != srcBlob.data) srcBlob.reshape(1, outShape).copyTo(outputs[i]); } } }
void NMPTUtils::sample(Mat &samples, const Mat& histogram, Size sampleSize) { int numels = sampleSize.width * sampleSize.height; Mat normHist ; distNorm(normHist, histogram.reshape(1,1)); //normHist = normHist.reshape(1,1); Mat randEls = Mat::zeros(1,numels, CV_64F); samples.create(sampleSize,CV_64F); MatIterator_<double> it = samples.begin<double>(); randu(randEls, 0., 1.); int useFast = numels > 10; if (useFast) { sort(randEls, randEls, CV_SORT_EVERY_ROW | CV_SORT_ASCENDING); int h = 0; double c = normHist.at<double>(0,h); for (int i = 0; i < numels; i++) { while (randEls.at<double>(0,i) > c) { h++; c += normHist.at<double>(0,h); } *it = h; it++; } printMat(samples); } else { for (int i = 0; i < numels; i++) { double target = randomFloat(); int h = 0; double c = normHist.at<double>(0,h); while(target > c) { h++; c += normHist.at<double>(0,h); } *it = h; it++; } } randShuffle(samples); }
TEST(photoeffects_matte, regression) { string input = cvtest::TS::ptr()->get_data_path() + "photoeffects/matte_test.png"; string expectedOutput = cvtest::TS::ptr()->get_data_path() + "photoeffects/matte_test_result.png"; Mat src = imread(input, CV_LOAD_IMAGE_COLOR); Mat expectedDst = imread(expectedOutput, CV_LOAD_IMAGE_COLOR); if(src.empty()) { FAIL() << "Can't read " + input + "image"; } if(expectedDst.empty()) { FAIL() << "Can't read " + expectedOutput + "image"; } Mat dst; EXPECT_EQ(0, matte(src, dst, Point(100, 100), Point(300, 300), 50.0f, 50.0f)); dst.convertTo(dst, CV_8UC3, 255); Mat diff = abs(expectedDst - dst); Mat mask = diff.reshape(1) > 1; EXPECT_EQ(0, countNonZero(mask)); }
/*! @brief Convolve two matrices, with a stride of greater than one * * This is a specialized 2D convolution algorithm with a stride of greater * than one. It is designed to convolve a filter with a feature, where at * each pixel an SVM must be evaluated (leading to a stride of SVM weight length). * The convolution can be thought of as flattened a 2.5D convolution where the * (i,j) dimension is the spatial plane and the (k) dimension is the SVM weights * of the pixels. * * The function supports multithreading via OpenMP * * @param feature the feature matrix * @param filter the filter (SVM) * @param pdf the response to return * @param stride the SVM weight length */ void SpatialConvolutionEngine::convolve(const Mat& feature, vectorFilterEngine& filter, Mat& pdf, const size_t stride) { // error checking assert(feature.depth() == type_); // split the feature into separate channels vectorMat featurev; split(feature.reshape(stride), featurev); // calculate the output Rect roi(0,0,-1,-1); // full image Point offset(0,0); Size fsize = featurev[0].size(); pdf = Mat::zeros(fsize, type_); for (size_t c = 0; c < stride; ++c) { Mat pdfc(fsize, type_); filter[c]->apply(featurev[c], pdfc, roi, offset, true); pdf += pdfc; } }