3
votes

I am trying to plot the points on a white 500x500 background image.

void Lab1() {

    float px[500];
    float py[500];
    float x, y;
    int nrPoints;
        //citire puncte din fisier
    FILE *pfile;
    pfile = fopen("points0.txt", "r");

        //punere in variabila
    fscanf(pfile, "%d", &nrPoints);

       //facem o imagine de 500/500 alba
    Mat whiteImg(500, 500, CV_8UC3);

    for (int i = 0; i < 500; i++) {
        for (int j = 0; j < 500; j++) {
            whiteImg.at<Vec3b>(i, j)[0] = 255; // b
            whiteImg.at<Vec3b>(i, j)[1] = 255; // g
            whiteImg.at<Vec3b>(i, j)[2] = 255; // r

        }
    }


      //punem punctele intr-un vector,pentru a le putea pozitiona ulterior in imaginea alba.

    for (int i = 0; i < nrPoints; i++) {
        fscanf(pfile, "%f%f", &x, &y);

        px[i] = x;
        py[i] = y;
        //afisam punctele
        printf("%f ", px[i]);
        printf("%f\n", py[i]);
    }

      //punem punctele pe imagine

    for (int i = 0; i < nrPoints; i++) {
        whiteImg.at<Vec3b>(px[i],py[i]) = 0;
    }

    imshow("img",whiteImg);
    fclose(pfile);
     //system("pause");
    waitKey();
}

and the problem is here:

whiteImg.at<Vec3b>(px[i],py[i]) = 0;

I can't avoid this error:

OpenCV Error: Assertion failed ((unsigned)i0 < (unsigned)size.p[0]) in cv::Mat::at, file c:\users\toder\desktop\anul4\srf\laburi_srf\opencvapplication-vs2015_31_basic\opencv\include\opencv2\core\mat.inl.hpp, line 917

1
Which language? C++ or C -- pick one. Considering that you're using templates as well as parts of the C++ OpenCV API, I'd say C is out of the question... | I don't see any validation of the input. Are you sure that all the values of px and py are within the bounds of the image? | BTW, the first parameter of cv::Mat::at is the row number. I read px as the x coordinate, which would be column number. - Dan MaĊĦek

1 Answers

4
votes

You declared your Mat as

Mat(500, 500, CV_8UC3); 

CV_8UC3 means it has three channels: one for red, one for blue, one for green. You cannot set a 0 (integer) in a Mat with three channels (Vec3b). Considering you wanted set the value 0 at the given point in the image, the point color to be plotted is black.

You can do it this way:

whiteImg.at<Vec3b>(px[i],py[i]) = Vec3b(0,0,0);

Or, to be consistent with your code style:

whiteImg.at<Vec3b>(px[i],py[i])[0] = 0; //Blue Channel
whiteImg.at<Vec3b>(px[i],py[i])[1] = 0; //Green Channel
whiteImg.at<Vec3b>(px[i],py[i])[2] = 0; //Red Channel