首页 文章

未处理的异常...访问违规读取位置

提问于
浏览
0
#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 .

this is the code

1 回答

  • 1

    我对OpenCV一无所知,但这看起来有问题:

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

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

    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 ,隐藏了前一个 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 变量将被分配并初始化一次 . 每次通话不一次 . 随后进入变量范围将从最后一个入口开始,完全是这种情况下所需要的 . 但是对于一个多次调用函数的大型程序,需要谨慎使用此解决方案 .

相关问题