Пример #1
0
int main(int argc, char ** argv)
{
  if(argc < 2)
    {
      cout << "Usage: " << endl;
      cout << "         TV norm: ./depthInpainting TV depthImage" << endl;
      cout << "       PSNR calc: ./depthInpainting P depthImage mask inpainted" << endl;
      cout << "      Inpainting: ./depthInpainting LRTV depthImage mask outputPath" << endl;
      cout << "      Generating: ./depthInpainting G depthImage missingRate outputMask outputMissing" << endl;
      cout << "         LowRank: ./depthInpainting L depthImahe mask outputpath" << endl;
      cout << "         LRTVPHI: ./depthInpainting LRTVPHI depthImage mask outputPath" << endl;
      cout << "      TVPHI norm: ./depthInpainting TVPHI depthImage" << endl;
      cout << "            LRL0: ./depthInpainting LRL0 depthImage mask outputPath initImage K lambda_L0 MaxIterCnt" << endl;
      cout << "         LRL0PHI: ./depthInpainting LRL0PHI depthImage mask outputPath initImage K lambda_L0 MaxIterCnt" << endl;
      cout << "              L0: /depthInpainting L0 depthImage" << endl;
      return 0;
    }
  string instruction = argv[1];
  if( instruction == "TV" )
    {
      Mat img = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);
      cout << NORM_TV(img) << endl;
    }
  if( instruction == "TVPHI")
    {
      Mat img = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);
      LRTVPHI lrttv;
      lrttv.setShrinkageParam(1.0, 1.0);
      cout << lrttv.NORM_TVPHI(img) << endl;
    }
  else if( instruction == "P" )
    {
      Mat original = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);
      Mat inpainted = imread(argv[4], CV_LOAD_IMAGE_GRAYSCALE);
      Mat mask = imread(argv[3], CV_LOAD_IMAGE_GRAYSCALE);
      cout << PSNR(original, inpainted, mask) << endl;
    }
  else if( instruction == "G" )
    {
      string disparityPath = argv[2];
      int missingRate = atof(argv[3]);
      string outputMask = argv[4];
      string outputMissing = argv[5];
      Mat disparityOriginal = imread(disparityPath, CV_LOAD_IMAGE_GRAYSCALE);
 
      int W = disparityOriginal.cols;
      int H = disparityOriginal.rows;

      RNG rng;
      // mask: 0 indicates missing pixels
      Mat mask = Mat::zeros(H, W, CV_8U);
      rng.fill(mask, RNG::UNIFORM, 0, 255);
      threshold(mask, mask, 255 * missingRate / 100, 255, THRESH_BINARY);
      Mat disparityMissing;
      multiply(disparityOriginal, mask / 255, disparityMissing);

      imwrite(outputMask, mask);
      imwrite(outputMissing, disparityMissing);
    }
  // Low rank
  else if(instruction == "L" )
    {
      string disparityPath = argv[2];
      string maskPath = argv[3];
      Mat disparityMissing = imread(disparityPath, CV_LOAD_IMAGE_GRAYSCALE);
      string inpaintedPath = argv[4];
      Mat mask = imread(maskPath, CV_LOAD_IMAGE_GRAYSCALE);

      multiply(disparityMissing, mask / 255, disparityMissing);
      Mat inpainted = TNNR(disparityMissing, mask, 9, 9, 0.06);
      imwrite(argv[4], inpainted);
      
    }
  // only TV inpainting
  else if( instruction == "T" )
    {
      string disparityPath = argv[2];
      string maskPath = argv[3];
      Mat disparityMissing = imread(disparityPath, CV_LOAD_IMAGE_GRAYSCALE);
      string inpaintedPath = argv[4];
      Mat mask = imread(maskPath, CV_LOAD_IMAGE_GRAYSCALE);

      multiply(disparityMissing, mask / 255, disparityMissing);
      // Mat denoised;
      // inpaint(disparityMissing, 255 - mask, denoised, 11, INPAINT_TELEA);

      // imwrite(inpaintedPath, denoised);

      // try our TV
      // we can use low rank to initialize U_
      Mat denoised = imread(argv[5], CV_LOAD_IMAGE_ANYCOLOR); //TNNR(disparityMissing, mask, 9, 9, 0.06);

      LRTV lrtv(disparityMissing, mask);
      // rho dt lambda_tv lambda_rank 100 10
      lrtv.setParameters(0, 1, 0.01, 0);
      lrtv.init_U(denoised);
      lrtv.sub_1();
      Mat M = lrtv.getM();
      Mat Y = lrtv.getY();
      Mat result = lrtv.getU();

      Mat output;
      result.convertTo(output, CV_8UC1);
      imwrite(inpaintedPath, output);
      imwrite("M.png", M);
      imwrite("Y.png", Y);
    }
  else if( instruction == "LRTV" )
    {
      string disparityPath = argv[2];
      string maskPath = argv[3];
      Mat disparityMissing = imread(disparityPath, CV_LOAD_IMAGE_GRAYSCALE);
      string inpaintedPath = argv[4];
      Mat mask = imread(maskPath, CV_LOAD_IMAGE_GRAYSCALE);

      multiply(disparityMissing, mask / 255, disparityMissing);

      // Mat denoised;
      // inpaint(disparityMissing, 255 - mask, denoised, 11, INPAINT_TELEA);

      // imwrite(inpaintedPath, denoised);

      // try our TV
      // we can use low rank to initialize U_
      //Mat denoised = TNNR(disparityMissing, mask, 9, 9, 0.06);
      Mat denoised = imread(argv[5], CV_LOAD_IMAGE_GRAYSCALE);

      LRTV lrtv(disparityMissing, mask);
      // rho dt lambda_tv lambda_rank 100 10
      lrtv.setParameters(1.2, 0.1, 40, 10);
      lrtv.init_U(denoised);
      Mat result = lrtv.compute();
      Mat M = lrtv.getM();
      Mat Y = lrtv.getY();

      
      Mat output;
      result.convertTo(output, CV_8UC1);
      imwrite(inpaintedPath, output);
      imwrite("M.png", M);
      imwrite("Y.png", Y);

    }
  // for stat
  else if( instruction == "S" )
    {
      Mat dispU = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);
      int H = dispU.rows;
      int W = dispU.cols;
      Mat disp = Mat::zeros(H, W, CV_32FC1);
      dispU.convertTo(disp, CV_32FC1);
      
      Mat kernelx_plus_ = (Mat_<float>(1,3)<<0.0,-1.0,1.0);
      Mat kernelx_minus_ = (Mat_<float>(1,3)<<-1.0,1.0,0.0);
      Mat kernely_plus_ = (Mat_<float>(3,1)<<0.0,-1.0,1.0);
      Mat kernely_minus_ = (Mat_<float>(3,1)<<-1.0,1.0,0.0);

      Mat grad_x_plus, grad_x_minus, grad_y_plus, grad_y_minus;
      filter2D(disp, grad_x_plus, -1, kernelx_plus_);
      filter2D(disp, grad_x_minus, -1, kernelx_minus_);
      filter2D(disp, grad_y_plus, -1, kernely_plus_);
      filter2D(disp, grad_y_minus, -1, kernely_minus_);
      Mat ux, uy;
      ux = (grad_x_minus + grad_x_plus) / 2.0;
      uy = (grad_y_minus + grad_y_plus) / 2.0;
      pow(ux, 2.0, ux);
      pow(uy, 2.0, uy);
      Mat grad;
      sqrt(ux + uy, grad);
      Mat r;
      grad.convertTo(r, CV_8UC1);
      vector <float> histogram(200,0);
      for(int i = 0; i < H; i++)
	for(int j = 0; j < W; j++)
	  {
	    if(r.at<uchar>(i,j) < 200)
	      {
		int id = r.at<uchar>(i,j);
		histogram[id] = histogram[id] + 1;
	      }
	  }
      
      float total = accumulate(histogram.begin(), histogram.end(), 0.0);
      fstream file(argv[3],ios::out);
      int i = 0;
      for (auto &x : histogram)
	{
	  i++;
	  cout << x << endl;
	  file << i  << " " << x / total << endl;
	}
    }
    else if( instruction == "L0" )
    {
      Mat dispU = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);
      int H = dispU.rows;
      int W = dispU.cols;
      Mat disp = Mat::zeros(H, W, CV_32FC1);
      dispU.convertTo(disp, CV_32FC1);
      
      Mat kernelx_plus_ = (Mat_<float>(1,3)<<0.0,-1.0,1.0);
      Mat kernelx_minus_ = (Mat_<float>(1,3)<<-1.0,1.0,0.0);
      Mat kernely_plus_ = (Mat_<float>(3,1)<<0.0,-1.0,1.0);
      Mat kernely_minus_ = (Mat_<float>(3,1)<<-1.0,1.0,0.0);

      Mat grad_x_plus, grad_x_minus, grad_y_plus, grad_y_minus;
      filter2D(disp, grad_x_plus, -1, kernelx_plus_);
      filter2D(disp, grad_x_minus, -1, kernelx_minus_);
      filter2D(disp, grad_y_plus, -1, kernely_plus_);
      filter2D(disp, grad_y_minus, -1, kernely_minus_);
      Mat ux, uy;
      ux = (grad_x_minus + grad_x_plus) / 2.0;
      uy = (grad_y_minus + grad_y_plus) / 2.0;

      Mat grad;
      grad = abs(ux) + abs(uy);
      Mat r;
      grad.convertTo(r, CV_8UC1);
      int l0 = 0;
      for(int i = 0; i < H; i++)
	for(int j = 0; j < W; j++)
	  {
	    if (grad.at<uchar>(i,j ) <1)
	      l0++;
	  }
      cout << W*H - l0 << endl;
    }
  else if( instruction == "LRTVPHI" )
    {
      string disparityPath = argv[2]; //"../../MiddInpaint/ArtL/disp.png";
      string maskPath = argv[3]; //"../../MiddInpaint/ArtL/mask_50.png";
      Mat disparityMissing = imread(disparityPath, CV_LOAD_IMAGE_GRAYSCALE);
      string inpaintedPath = argv[4]; //"just_a_test.png";
      Mat mask = imread(maskPath, CV_LOAD_IMAGE_GRAYSCALE);

      // int H_ = mask.rows;
      // int W_ = mask.cols;
      // mask = 255*Mat::ones(H_, W_, CV_8UC1);
      Mat orig;
      disparityMissing.copyTo(orig); orig.convertTo(orig, CV_32FC1);
      multiply(disparityMissing, mask / 255, disparityMissing);

      imshow("dd",disparityMissing);waitKey(0);
      LRTVPHI lrttv(disparityMissing, mask);
      lrttv.setShrinkageParam(1.0,1.0);

      Mat denoised = imread(argv[5], CV_LOAD_IMAGE_GRAYSCALE);
     
      // rho dt lambda_tv lambda_rank
      // rho = 1.2
      lrttv.setParameters(1.2, 0.1, 10, 0.01);
      lrttv.setNewtonParameters(0.001); // set gamma

      // imshow("temp", denoised);
      // waitKey(0);2
      lrttv.init_U(denoised);
      lrttv.init_M(denoised);
      cout << "optimal objective function value = " << lrttv.sub_1_val(orig) << endl;
      Mat result;
      
	    //      lrttv.sub_2();
	    //      lrttv.sub_3();

      lrttv.sub_1(1.0,10.0);
      cout << "optimal objective function value = " << lrttv.sub_1_val(orig) << endl;
      // cout << "OK  sub_1" << endl;
      //Mat result = lrttv.compute();
      Mat M = lrttv.getM();
      Mat Y = lrttv.getY();
      Mat output;
      result = lrttv.getU();
      result.convertTo(output, CV_8UC1);
      imwrite(inpaintedPath, output);
      imwrite("tM.png", M);
      imwrite("tY.png", Y);
    }
  else if( instruction == "X" )
    {
      Mat a = (Mat_<float>(2,2)<<0,1,2,3);
      Mat x;
      exp(a,x);
      Mat y;
      exp(-a,y);
      Mat result;
      divide(x-y,x+y,result);
      cout << result << endl;
      
    }
  else if( instruction == "LRL0" )
    {
      string disparityPath = argv[2]; //"../../MiddInpaint/ArtL/disp.png";
      string maskPath = argv[3]; //"../../MiddInpaint/ArtL/mask_50.png";
      Mat disparityMissing = imread(disparityPath, CV_LOAD_IMAGE_GRAYSCALE);
      Mat orig;
      disparityMissing.copyTo(orig);
      string inpaintedPath = argv[4]; //"just_a_test.png";
      Mat mask = imread(maskPath, CV_LOAD_IMAGE_GRAYSCALE);

      int K = atoi(argv[6]);
      float lambda_l0 = atof(argv[7]);
      int max_iter = atoi(argv[8]);

      string path1 = argv[9];
      multiply(disparityMissing, mask / 255, disparityMissing);
      Mat denoised = imread(argv[5], CV_LOAD_IMAGE_GRAYSCALE);
      LRL0 lrl0(disparityMissing, mask);
      lrl0.setParameters(1.2,0.1,lambda_l0,10);
      lrl0.init_U(denoised);
      Mat result = lrl0.compute(K, max_iter, inpaintedPath, orig, path1);
      // Mat output;
      //result.convertTo(output, CV_8UC1);
      //imwrite(inpaintedPath, output);
      
      
    }
    else if( instruction == "LRL0PHI" )
    {
      string disparityPath = argv[2]; //"../../MiddInpaint/ArtL/disp.png";
      string maskPath = argv[3]; //"../../MiddInpaint/ArtL/mask_50.png";
      Mat disparityMissing = imread(disparityPath, CV_LOAD_IMAGE_GRAYSCALE);
      Mat orig;
      disparityMissing.copyTo(orig);
      string inpaintedPath = argv[4]; //"just_a_test.png";
      Mat mask = imread(maskPath, CV_LOAD_IMAGE_GRAYSCALE);

      int K = atoi(argv[6]);
      float lambda_l0 = atof(argv[7]);
      int max_iter = atoi(argv[8]);

      string path1 = argv[9];
      multiply(disparityMissing, mask / 255, disparityMissing);
      Mat denoised = imread(argv[5], CV_LOAD_IMAGE_GRAYSCALE);
      LRL0PHI lrl0phi(disparityMissing, mask);
      lrl0phi.setParameters(1.2,0.1,lambda_l0,10,0.75);
      lrl0phi.init_U(denoised);
      Mat result = lrl0phi.compute(K, max_iter, inpaintedPath, orig, path1);
      
    }
  else
    {
      cout << "Argument Error!" << endl;
      cout << argv[1] << endl;
    }
  return 0;
}
void getSRParams(SRCAM cam, Mat& cameraMatrix, Mat& distCoeffs, Mat& rvec, Mat& tvec)
{

        int rows = SR_GetRows(cam), cols = SR_GetCols(cam);

        if(rows*cols<=0) {
          cerr << "ERROR: Camera was not opened" << endl;
        }

        // 1. get image pointer and fill with random values
        unsigned short* values = (unsigned short*)SR_GetImage(cam, 0);
        RNG rng;
        Mat _values(rows, cols, CV_16UC1, values);
        cout << "Generate random image" << endl;
        rng.fill(_values, RNG::UNIFORM, Scalar::all(10), Scalar::all(65000));
        const int s = 3*sizeof(float);
        vector<float> xyz(rows*cols*3, 0);

        // 2. convert to an XYZ image
        SR_CoordTrfFlt(cam, &xyz[0], &xyz[1], &xyz[2], s, s, s);

        // 3. initialize camera matrix
        // fit a line to x and y as camera matrix approximation
        // this has to be done in order to run the calibration with non-planar points
        Vec3f* ptr = (Vec3f*)&xyz[0];
        vector<Vec3f> objectPoints(rows*cols);
        vector<Vec2f> imagePoints(rows*cols),
                      normalizedImPointsX(rows*cols),
                      normalizedImPointsY(rows*cols);

        int i=0, j,k;
        for(j=0; j<rows; ++j)
        {
            for(k=0; k<cols; ++k, ++i)
            {
                    objectPoints[i]=ptr[j*cols+k];
                    imagePoints[i]= Vec2f((float)k+0.5f, (float)j+0.5f);
                    normalizedImPointsX[i] = Vec2f(ptr[j*cols+k][0]/ptr[j*cols+k][2], imagePoints[i][0]);
                    normalizedImPointsY[i] = Vec2f(ptr[j*cols+k][1]/ptr[j*cols+k][2], imagePoints[i][1]);
            }
        }
        double p=0, reps=0.01, aeps=0.01, ax, bx, ay, by;
        Vec4f linex, liney;
        cout << "Fit line" << endl;
        fitLine(normalizedImPointsX, linex, CV_DIST_L2, 0, reps, aeps);
        fitLine(normalizedImPointsY, liney, CV_DIST_L2, 0, reps, aeps);
        ax = linex[1]/linex[0]; bx = cols/2;
        ay = liney[1]/liney[0]; by = rows/2;
        cameraMatrix = Mat(3, 3, CV_64FC1, Scalar::all(0));
        double* _cm = (double*)cameraMatrix.ptr();
        _cm[0] = fabs(ax); _cm[2] = bx; _cm[4] = fabs(ay); _cm[5] = by; _cm[8]=1.0;
        const int distCoeffsNum = 8;
        distCoeffs = Mat(1, distCoeffsNum, CV_64FC1, Scalar::all(0));
        rvec = tvec = Mat(1, 3, CV_32FC1, Scalar::all(0));

        // 4. calibrate
        vector<vector<Vec3f> > _op(1, objectPoints);
        vector<vector<Vec2f> > _ip(1, imagePoints);
        vector<Mat> _rvec, _tvec;
        cout << "run camera calibration" << endl;
        calibrateCamera(_op, _ip, Size(cols,rows), cameraMatrix, distCoeffs, _rvec, _tvec, CV_CALIB_USE_INTRINSIC_GUESS | CV_CALIB_RATIONAL_MODEL);
        rvec = _rvec[0];
        tvec = _tvec[0];
}
Пример #3
0
void Core_ArrayOpTest::run( int /* start_from */)
{
    int errcount = 0;

    // dense matrix operations
    {
        int sz3[] = {5, 10, 15};
        MatND A(3, sz3, CV_32F), B(3, sz3, CV_16SC4);
        CvMatND matA = A, matB = B;
        RNG rng;
        rng.fill(A, CV_RAND_UNI, Scalar::all(-10), Scalar::all(10));
        rng.fill(B, CV_RAND_UNI, Scalar::all(-10), Scalar::all(10));

        int idx0[] = {3,4,5}, idx1[] = {0, 9, 7};
        float val0 = 130;
        Scalar val1(-1000, 30, 3, 8);
        cvSetRealND(&matA, idx0, val0);
        cvSetReal3D(&matA, idx1[0], idx1[1], idx1[2], -val0);
        cvSetND(&matB, idx0, val1);
        cvSet3D(&matB, idx1[0], idx1[1], idx1[2], -val1);
        Ptr<CvMatND> matC = cvCloneMatND(&matB);

        if( A.at<float>(idx0[0], idx0[1], idx0[2]) != val0 ||
           A.at<float>(idx1[0], idx1[1], idx1[2]) != -val0 ||
           cvGetReal3D(&matA, idx0[0], idx0[1], idx0[2]) != val0 ||
           cvGetRealND(&matA, idx1) != -val0 ||

           Scalar(B.at<Vec4s>(idx0[0], idx0[1], idx0[2])) != val1 ||
           Scalar(B.at<Vec4s>(idx1[0], idx1[1], idx1[2])) != -val1 ||
           Scalar(cvGet3D(matC, idx0[0], idx0[1], idx0[2])) != val1 ||
           Scalar(cvGetND(matC, idx1)) != -val1 )
        {
            ts->printf(cvtest::TS::LOG, "one of cvSetReal3D, cvSetRealND, cvSet3D, cvSetND "
                       "or the corresponding *Get* functions is not correct\n");
            errcount++;
        }
    }

    RNG rng;
    const int MAX_DIM = 5, MAX_DIM_SZ = 10;
    // sparse matrix operations
    for( int si = 0; si < 10; si++ )
    {
        int depth = (unsigned)rng % 2 == 0 ? CV_32F : CV_64F;
        int dims = ((unsigned)rng % MAX_DIM) + 1;
        int i, k, size[MAX_DIM]={0}, idx[MAX_DIM]={0};
        vector<string> all_idxs;
        vector<double> all_vals;
        vector<double> all_vals2;
        string sidx, min_sidx, max_sidx;
        double min_val=0, max_val=0;

        int p = 1;
        for( k = 0; k < dims; k++ )
        {
            size[k] = ((unsigned)rng % MAX_DIM_SZ) + 1;
            p *= size[k];
        }
        SparseMat M( dims, size, depth );
        map<string, double> M0;

        int nz0 = (unsigned)rng % max(p/5,10);
        nz0 = min(max(nz0, 1), p);
        all_vals.resize(nz0);
        all_vals2.resize(nz0);
        Mat_<double> _all_vals(all_vals), _all_vals2(all_vals2);
        rng.fill(_all_vals, CV_RAND_UNI, Scalar(-1000), Scalar(1000));
        if( depth == CV_32F )
        {
            Mat _all_vals_f;
            _all_vals.convertTo(_all_vals_f, CV_32F);
            _all_vals_f.convertTo(_all_vals, CV_64F);
        }
        _all_vals.convertTo(_all_vals2, _all_vals2.type(), 2);
        if( depth == CV_32F )
        {
            Mat _all_vals2_f;
            _all_vals2.convertTo(_all_vals2_f, CV_32F);
            _all_vals2_f.convertTo(_all_vals2, CV_64F);
        }

        minMaxLoc(_all_vals, &min_val, &max_val);
        double _norm0 = norm(_all_vals, CV_C);
        double _norm1 = norm(_all_vals, CV_L1);
        double _norm2 = norm(_all_vals, CV_L2);

        for( i = 0; i < nz0; i++ )
        {
            for(;;)
            {
                for( k = 0; k < dims; k++ )
                    idx[k] = (unsigned)rng % size[k];
                sidx = idx2string(idx, dims);
                if( M0.count(sidx) == 0 )
                    break;
            }
            all_idxs.push_back(sidx);
            M0[sidx] = all_vals[i];
            if( all_vals[i] == min_val )
                min_sidx = sidx;
            if( all_vals[i] == max_val )
                max_sidx = sidx;
            setValue(M, idx, all_vals[i], rng);
            double v = getValue(M, idx, rng);
            if( v != all_vals[i] )
            {
                ts->printf(cvtest::TS::LOG, "%d. immediately after SparseMat[%s]=%.20g the current value is %.20g\n",
                           i, sidx.c_str(), all_vals[i], v);
                errcount++;
                break;
            }
        }

        Ptr<CvSparseMat> M2 = (CvSparseMat*)M;
        MatND Md;
        M.copyTo(Md);
        SparseMat M3; SparseMat(Md).convertTo(M3, Md.type(), 2);

        int nz1 = (int)M.nzcount(), nz2 = (int)M3.nzcount();
        double norm0 = norm(M, CV_C);
        double norm1 = norm(M, CV_L1);
        double norm2 = norm(M, CV_L2);
        double eps = depth == CV_32F ? FLT_EPSILON*100 : DBL_EPSILON*1000;

        if( nz1 != nz0 || nz2 != nz0)
        {
            errcount++;
            ts->printf(cvtest::TS::LOG, "%d: The number of non-zero elements before/after converting to/from dense matrix is not correct: %d/%d (while it should be %d)\n",
                       si, nz1, nz2, nz0 );
            break;
        }

        if( fabs(norm0 - _norm0) > fabs(_norm0)*eps ||
           fabs(norm1 - _norm1) > fabs(_norm1)*eps ||
           fabs(norm2 - _norm2) > fabs(_norm2)*eps )
        {
            errcount++;
            ts->printf(cvtest::TS::LOG, "%d: The norms are different: %.20g/%.20g/%.20g vs %.20g/%.20g/%.20g\n",
                       si, norm0, norm1, norm2, _norm0, _norm1, _norm2 );
            break;
        }

        int n = (unsigned)rng % max(p/5,10);
        n = min(max(n, 1), p) + nz0;

        for( i = 0; i < n; i++ )
        {
            double val1, val2, val3, val0;
            if(i < nz0)
            {
                sidx = all_idxs[i];
                string2idx(sidx, idx, dims);
                val0 = all_vals[i];
            }
            else
            {
                for( k = 0; k < dims; k++ )
                    idx[k] = (unsigned)rng % size[k];
                sidx = idx2string(idx, dims);
                val0 = M0[sidx];
            }
            val1 = getValue(M, idx, rng);
            val2 = getValue(M2, idx);
            val3 = getValue(M3, idx, rng);

            if( val1 != val0 || val2 != val0 || fabs(val3 - val0*2) > fabs(val0*2)*FLT_EPSILON )
            {
                errcount++;
                ts->printf(cvtest::TS::LOG, "SparseMat M[%s] = %g/%g/%g (while it should be %g)\n", sidx.c_str(), val1, val2, val3, val0 );
                break;
            }
        }

        for( i = 0; i < n; i++ )
        {
            double val1, val2;
            if(i < nz0)
            {
                sidx = all_idxs[i];
                string2idx(sidx, idx, dims);
            }
            else
            {
                for( k = 0; k < dims; k++ )
                    idx[k] = (unsigned)rng % size[k];
                sidx = idx2string(idx, dims);
            }
            eraseValue(M, idx, rng);
            eraseValue(M2, idx);
            val1 = getValue(M, idx, rng);
            val2 = getValue(M2, idx);
            if( val1 != 0 || val2 != 0 )
            {
                errcount++;
                ts->printf(cvtest::TS::LOG, "SparseMat: after deleting M[%s], it is =%g/%g (while it should be 0)\n", sidx.c_str(), val1, val2 );
                break;
            }
        }

        int nz = (int)M.nzcount();
        if( nz != 0 )
        {
            errcount++;
            ts->printf(cvtest::TS::LOG, "The number of non-zero elements after removing all the elements = %d (while it should be 0)\n", nz );
            break;
        }

        int idx1[MAX_DIM], idx2[MAX_DIM];
        double val1 = 0, val2 = 0;
        M3 = SparseMat(Md);
        minMaxLoc(M3, &val1, &val2, idx1, idx2);
        string s1 = idx2string(idx1, dims), s2 = idx2string(idx2, dims);
        if( val1 != min_val || val2 != max_val || s1 != min_sidx || s2 != max_sidx )
        {
            errcount++;
            ts->printf(cvtest::TS::LOG, "%d. Sparse: The value and positions of minimum/maximum elements are different from the reference values and positions:\n\t"
                       "(%g, %g, %s, %s) vs (%g, %g, %s, %s)\n", si, val1, val2, s1.c_str(), s2.c_str(),
                       min_val, max_val, min_sidx.c_str(), max_sidx.c_str());
            break;
        }

        minMaxIdx(Md, &val1, &val2, idx1, idx2);
        s1 = idx2string(idx1, dims), s2 = idx2string(idx2, dims);
        if( (min_val < 0 && (val1 != min_val || s1 != min_sidx)) ||
           (max_val > 0 && (val2 != max_val || s2 != max_sidx)) )
        {
            errcount++;
            ts->printf(cvtest::TS::LOG, "%d. Dense: The value and positions of minimum/maximum elements are different from the reference values and positions:\n\t"
                       "(%g, %g, %s, %s) vs (%g, %g, %s, %s)\n", si, val1, val2, s1.c_str(), s2.c_str(),
                       min_val, max_val, min_sidx.c_str(), max_sidx.c_str());
            break;
        }
    }

    ts->set_failed_test_info(errcount == 0 ? cvtest::TS::OK : cvtest::TS::FAIL_INVALID_OUTPUT);
}
Пример #4
0
 void gen(int cols, int rows, Mat& mat)
 {
     RNG rng;
     mat.create(rows, cols, CV_32FC2);
     rng.fill(mat, RNG::UNIFORM, Scalar::all(0.f), Scalar::all(10.f));
 }