简体   繁体   English

OpenCV错误:cv :: Mat :: at中的断言失败((无符号)i0 <(无符号)size.p [0])

[英]OpenCV Error: Assertion failed ((unsigned)i0 < (unsigned)size.p[0]) in cv::Mat::at

I am trying to plot the points on a white 500x500 background image. 我试图在白色500x500背景图像上绘制点。

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 OpenCV错误:断言失败((无符号)i0 <(无符号)size.p [0])在cv :: Mat :: at,文件c:\\ users \\ toder \\ desktop \\ anul4 \\ srf \\ laburi_srf \\ opencvapplication-vs2015_31_basic \\ opencv \\ include \\ opencv2 \\ core \\ mat.inl.hpp,第917行

You declared your Mat as 你宣称你的Mat为

Mat(500, 500, CV_8UC3); 

CV_8UC3 means it has three channels: one for red, one for blue, one for green. CV_8UC3表示它有三个通道:一个用于红色,一个用于蓝色,一个用于绿色。 You cannot set a 0 (integer) in a Mat with three channels (Vec3b). 您不能在具有三个通道(Vec3b)的Mat中设置0(整数)。 Considering you wanted set the value 0 at the given point in the image, the point color to be plotted is black. 考虑到您希望在图像中的给定点设置值0,要绘制的点颜色为黑色。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OpenCV 断言失败 ((unsigned)(i1 * DataType&lt;_Tp&gt;::channels) &lt; (unsigned)(size.p[1] * channels())) 在 cv::Mat::at - OpenCV Assertion failed ((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels())) in cv::Mat::at 如何修复此OpenCV错误:cv :: Vec中的断言失败((unsigned)i &lt;(unsigned)cn) <unsigned char,3> :: operator(),文件 - How to fix this OpenCV Error: Assertion failed ((unsigned)i < (unsigned)cn) in cv::Vec<unsigned char,3>::operator (), file CV :: Mat导致运行时错误-OpenCV错误-断言失败 - CV::Mat causing runtime error - OpenCV Error - Assertion failed OpenCV错误:断言在cv :: Mat行522中失败 - OpenCV Error: Assertion failed in cv::Mat line 522 OpenCV(3.4.1) 错误: cv::Mat::locateROI 中的断言失败 (dims &lt;= 2 &amp;&amp; step[0] &gt; 0) - OpenCV(3.4.1) Error: Assertion failed (dims <= 2 && step[0] > 0) in cv::Mat::locateROI 如何将cv :: Mat&Img复制到未签名的char * img? - How to copy cv::Mat & Img to unsigned char* img?opencv unsigned char ** to opencv mat - unsigned char ** to opencv mat opencv错误:cv :: Mat :: at文件中的断言失败:mat.inl.hpp行930 - opencv error: assertion failed in cv::Mat::at file: mat.inl.hpp line 930 cv :: erode导致错误:OpenCV错误:断言失败(m.dims&gt; = 2)在Mat中 - cv::erode causes error: OpenCV Error: Assertion failed (m.dims >= 2) in Mat Mat OpenCV中的断言失败 - Assertion failed in Mat OpenCV
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM