使用Open CV Iterators segfault

时间:2011-05-03 22:33:58

标签: c++ ubuntu opencv iterator segmentation-fault

我想做eigenfaces之类的事情但是有不同的图像(不是面孔)。我有一个图像矢量images。从这些图像中,我想创建一个Mat对象data,其中每行包含一个写为行向量的图像。这就是我试图做的事情:

// This is basically a matrix that needs to have a bunch of images as rows.
Mat data(numImages, IMAGE_SIZE * IMAGE_SIZE, CV_8UC1);
// I also replaced CV_8U by images[0].type() and CV_8U. no change
MatIterator_<unsigned short> iter = data.begin<unsigned short> (),
                             iter_end = data.end<unsigned short> (), 
                             iter2;

for (i = 0; i < numImages; ++i)
{
    MatIterator_<unsigned short> begin = images[i].begin<unsigned short> ();
    MatIterator_<unsigned short> end = images[i].end<unsigned short> ();
    for (iter2 = begin; iter2 != end; iter2++)
    {
        *iter = *iter2; // Segfault is here.
        if (iter != iter_end) // safety check
            iter++;
        else
            perror("Screwed!\n"); // This does not execute!
    }
}

帮助!

谢谢!

1 个答案:

答案 0 :(得分:1)

我认为在你的矩阵中每个字段有1个字节(CV_8UC1),但你的迭代器是“unsigned short”迭代器。无符号短通常有2个字节。将其更改为

data.begin<unsigned char> ()

还要检查stdint.h:http://en.wikipedia.org/wiki/Stdint.h