使用Open CV迭代器进行分段故障

Using Open CV Iterators segfault

本文关键字:分段 故障 迭代器 Open CV 使用      更新时间:2023-10-16

我想做一些像特征脸但不同的图像(不是脸)。我有一个图像向量,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个字节(CV_8UC1),但你的迭代器是"无符号短"迭代器。无符号短通常有2个字节。改为

data.begin<unsigned char> ()

也检查stint .h: http://en.wikipedia.org/wiki/Stdint.h