【问题标题】:CV::imwrite from a buffer of std::vector来自 std::vector 缓冲区的 CV::imwrite
【发布时间】:2015-11-05 06:40:49
【问题描述】:

我是opencv编程的初学者,我正在尝试读取图像(cv::Mat)并将其数据复制到Uchar的某个向量中,然后从该向量创建图像。

即读取 Mat ,将 Mat 转换为 std::vector ,然后再次从该向量创建一个 Mat 。

像素数据的中间变换需要向量。 我推荐了 Convert Mat to Array/Vector in OpenCV

用于vector-Mat的转换。

int main()
{
    Mat image = imread("c:\\cv\\abc.bmp");
    std::vector<unsigned char> src_vec;
    src_vec.assign(image.datastart, image.dataend);
    cv::Mat dest(src_vec, true);
    try {
        imwrite("dest.bmp", dest);

    }
    catch (runtime_error& ex) {
        fprintf(stderr, "Exception saving the image: %s\n", ex.what());
        return 1;
    }
    return 0;
}

输出图像似乎是垃圾,如何使用矢量数据设置 dest Mat 还是我以错误的方式创建矢量本身。 任何指导都会有所帮助。

【问题讨论】:

    标签: c++ opencv vector


    【解决方案1】:

    您缺少标题信息。 vector 仅包含像素数据。您必须将标头数据保存在某处并再次将其传递给mat。 在以下示例中,标题数据直接取自源图像。您也可以将其保存在一些整数变量中,并再次将其传递给新的mat 的标头。

    Mat image = imread("c:\\cv\\abc.bmp");
        std::vector<unsigned char> src_vec;
        src_vec.assign(image.datastart, image.dataend);
        cv::Mat dest(image.rows,image.cols,image.type());
        dest.data = src_vec.data();
        try {
            imshow("sss", dest);
            cv::waitKey();
            //or show using imshow - nothing is shown
        }
        catch (runtime_error& ex) {
            fprintf(stderr, "Exception saving the image: %s\n", ex.what());
            return 1;
        }
        return 0;
    

    附:尽量不要使用 \\ 这是 Windows 的东西。使用/,它是跨平台的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 2017-12-01
      相关资源
      最近更新 更多