2015-11-05 75 views
3

我在OpenCV的编程初学者,我尝试读取图像(CV :: MAT)和它的数据复制到UCHAR的一些载体,然后创建图像回从矢量。CV :: imwrite从STD的缓冲:: vector的

即读垫,垫转换到std :: vector的,然后再创建从载体垫。需要

载体为像素数据的中间转换。 我选择了 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垫与矢量数据抑或是我创造的错误的方式载体本身。 任何指导都会有所帮助。

回答

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; 

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

+0

谢谢lot..that是有帮助:) – Hummingbird