【问题标题】:Unhandled exception at ... Access violation reading location未处理的异常在...访问冲突读取位置
【发布时间】:2023-03-15 08:56:01
【问题描述】:
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <opencv2\core\core.hpp>
#include <opencv2\objdetect\objdetect.hpp>
#include <opencv2\imgproc\imgproc.hpp>
using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    int i, M;
    Mat ycrcb, rgb, vect, Data;
    vector < String > files;
    /*Names of the pictures*/
    glob("C:\\Users\\lenovo\\Desktop\\cubicle\\trainning\\*.jpg", files); // M=number of training images

    M = files.size();
// calculatong of the matrix Data
    for (i = 0; i < M; i++)
    {

// Lecture of RGB image

        rgb = imread(files[i]);
        namedWindow("RGB image", WINDOW_AUTOSIZE);
        imshow("RGB image", rgb);
        waitKey(10);
        if (i == 0)
        { //for the first iteration
            Mat Data(M, rgb.cols * rgb.rows * 6, CV_32FC1); //statement and allocation of matrix Data
        }
        rgb.convertTo(rgb, CV_32FC3, 1.0 / 255.0);

// Convert to float // Convert the RGB color space to the color space Ycrcbb*/

        cvtColor(rgb, ycrcb, CV_BGR2YCrCb);
        //making each image a vector line

        rgb = rgb.reshape(1, rgb.total() * 3);
        ycrcb = ycrcb.reshape(1, ycrcb.total() * 3);
        /*Concatenate rgb and ycrcb*/

        hconcat(rgb, ycrcb, vect);
        fprintf(stdout,
                "rgb=[%d,%d] , ycrcb=[%d,%d], vect=[%d,%d\n",
                rgb.rows,
                rgb.cols,
                ycrcb.rows,
                ycrcb.cols,
                vect.rows,
                vect.cols);
        vect.copyTo(Data.row(i));
    }

    int nclusters = 35;
    Mat labels, centers(nclusters, Data.cols, CV_32FC1);
    /* clustering Data by kmeans*/

    kmeans(Data,
           nclusters,
           labels,
           TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 0.65, 200),
           3,

           KMEANS_PP_CENTERS,
           centers);

}

这是完整的代码,我遇到了错误:

PB.exe 中 0x00b85c10 处未处理的异常:0xC0000005:访问冲突读取位置 0xc35de59f。

【问题讨论】:

  • 我将首先在调试菜单的异常项中启用访问冲突中断,然后将调用堆栈向上移动到导致问题的代码行。此外,您可能应该检查图像是否已成功加载。

标签: c++ visual-studio-2010 opencv background-subtraction


【解决方案1】:

我对 OpenCV 一无所知,但这看起来值得怀疑:

int main(int argc, char** argv)
{
    int i, M;
    Mat ycrcb, rgb, vect, Data;

刚刚定义了Data 类型的变量Mat。不幸的是,这是完全合理的事情

        if (i == 0)
        { //for the first iteration
            Mat Data(M, rgb.cols * rgb.rows * 6, CV_32FC1); //statement and allocation of matrix Data
        }

制作了第二个Mat Data,它隐藏了前一个,并且只存在于if 的主体范围内。当内部Data 超出范围时,设置此Data 完成的所有工作都被丢弃在右花括号中。在if 主体之外,原始Data 仍然处于活动状态且可访问,但从未正确初始化。它的用途有些可疑,所以当

vect.copyTo(Data.row(i));

已到达,Data 可能没有可以将vect 复制到其中的行。 Data 的后续使用同样值得怀疑,任何一个都可能导致 seg 错误。

我的建议是在所有数据可用之前推迟创建Data

既然你有一个很简单的功能,那就改

Mat ycrcb, rgb, vect, Data; 

Mat ycrcb, rgb, vect;

替换

    if (i == 0)
    { //for the first iteration
        Mat Data(M, rgb.cols * rgb.rows * 6, CV_32FC1); //statement and allocation of matrix Data
     }

    static Mat Data(M, rgb.cols * rgb.rows * 6, CV_32FC1);

可能是你需要的。

Read up on Static local variables here.

编辑

static 局部变量有点奇怪。它们是“某种”全球性的。它们的生命周期从首次使用到程序终止,但仅在定义它们的范围内可见。

如果在将被多次调用的函数中定义,static 变量将被分配和初始化一次。不是每次调用一次。随后进入变量的范围将从最后一个入口剩下的任何内容开始,这正是这种情况下所需要的。但是对于一个较大的程序多次调用一个函数,这个方案需要谨慎使用。

【讨论】:

  • 是的,谢谢,这很有帮助,但如果数据不是全局变量,那么现在对于函数来说是未知的:vect.copyTo(Data.row(i)) 和 Kmeans。
猜你喜欢
  • 1970-01-01
  • 2015-05-01
  • 2013-11-22
  • 1970-01-01
  • 2011-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-05
相关资源
最近更新 更多