使用 Open CV 迭代器出现段错误

发布于 2024-11-04 13:29:30 字数 1134 浏览 1 评论 0原文

我想做类似 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!
    }
}

帮助!

谢谢!

I want to do something like eigenfaces but with different images (not faces). I have a vector of images, images. From these images, I want to make a Mat object, data which contains in each row, a image written out as a row vector. This is what I tried to do:

// 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!
    }
}

Help!

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅冕 2024-11-11 13:29:30

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

data.begin<unsigned char> ()

同时检查 stdint.h:http://en.wikipedia.org/wiki/Stdint。哈

I think that in your matrix each field has 1 byte (CV_8UC1), but you iterator is "unsigned short" iterator. Unsigned short usually has 2 bytes. change it to

data.begin<unsigned char> ()

also check stdint.h: http://en.wikipedia.org/wiki/Stdint.h

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文