【问题标题】:OpenCV Error: Assertion failed ((unsigned)i0 < (unsigned)size.p[0]) in cv::Mat::atOpenCV 错误: cv::Mat::at 中的断言失败 ((unsigned)i0 < (unsigned)size.p[0])
【发布时间】:2019-05-04 00:33:01
【问题描述】:

我正在尝试在白色 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();
}

问题就在这里:

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

我无法避免这个错误:

OpenCV 错误:在 cv::Mat::at,文件 c:\users\toder\desktop\anul4\srf\laburi_srf\opencvapplication 中断言失败 ((unsigned)i0

【问题讨论】:

  • 哪种语言? C++ 或 C - 选择一个。考虑到您正在使用模板以及 C++ OpenCV API 的一部分,我想说 C 是不可能的... |我没有看到对输入的任何验证。您确定pxpy 的所有值都在图像的范围内吗? | BTW,cv::Mat::at 的第一个参数是行号。我将px 读作x 坐标,即列号。

标签: c++ c opencv


【解决方案1】:

您将 Mat 声明为

Mat(500, 500, CV_8UC3); 

CV_8UC3 表示它具有三个通道:一个红色通道,一个蓝色通道,一个绿色通道。您不能在具有三个通道 (Vec3b) 的 Mat 中设置 0(整数)。考虑到您想在图像中的给定点设置值 0,要绘制的点颜色为黑色。

你可以这样做:

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

或者,为了与您的代码风格保持一致:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-15
    • 2018-11-29
    • 2017-12-24
    • 2014-11-25
    • 2023-03-23
    • 2016-02-21
    • 1970-01-01
    • 2021-02-26
    相关资源
    最近更新 更多