代码之家  ›  专栏  ›  技术社区  ›  schubi

将jpeg字符数据转换为opencv mat

  •  0
  • schubi  · 技术社区  · 6 年前

    我有一个mjpeg tcp流,想修改它。因此,我可以通过recv函数访问图像数据,并将其保存在char缓冲区中。

    //....
    const int buf_size = 512;
    char buf[buf_size];
    //...
    int bytesReceived = recv(sock, buf, buf_size, MSG_WAITALL);
    //...
    

    现在我可以从char数组中提取jpeg图像数据;我创建了一个测试字符数组(16*16 jpeg图像):

    char img[] = {-1,-40,-1,-32,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0,-1,-37,0,67,0,5,3,4,4,4,3,5,4,4,4,5,5,5,6,7,12,8,7,7,7,7,15,11,11,9,12,17,15,18,18,17,15,17,17,19,22,28,23,19,20,26,21,17,17,24,33,24,26,29,29,31,31,31,19,23,34,36,34,30,36,28,30,31,30,-1,-37,0,67,1,5,5,5,7,6,7,14,8,8,14,30,20,17,20,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,-1,-64,0,17,8,0,16,0,16,3,1,34,0,2,17,1,3,17,1,-1,-60,0,31,0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,-1,-60,0,-75,16,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,125,1,2,3,0,4,17,5,18,33,49,65,6,19,81,97,7,34,113,20,50,-127,-111,-95,8,35,66,-79,-63,21,82,-47,-16,36,51,98,114,-126,9,10,22,23,24,25,26,37,38,39,40,41,42,52,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,-125,-124,-123,-122,-121,-120,-119,-118,-110,-109,-108,-107,-106,-105,-104,-103,-102,-94,-93,-92,-91,-90,-89,-88,-87,-86,-78,-77,-76,-75,-74,-73,-72,-71,-70,-62,-61,-60,-59,-58,-57,-56,-55,-54,-46,-45,-44,-43,-42,-41,-40,-39,-38,-31,-30,-29,-28,-27,-26,-25,-24,-23,-22,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-1,-60,0,31,1,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,-1,-60,0,-75,17,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,119,0,1,2,3,17,4,5,33,49,6,18,65,81,7,97,113,19,34,50,-127,8,20,66,-111,-95,-79,-63,9,35,51,82,-16,21,98,114,-47,10,22,36,52,-31,37,-15,23,24,25,26,38,39,40,41,42,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,-126,-125,-124,-123,-122,-121,-120,-119,-118,-110,-109,-108,-107,-106,-105,-104,-103,-102,-94,-93,-92,-91,-90,-89,-88,-87,-86,-78,-77,-76,-75,-74,-73,-72,-71,-70,-62,-61,-60,-59,-58,-57,-56,-55,-54,-46,-45,-44,-43,-42,-41,-40,-39,-38,-30,-29,-28,-27,-26,-25,-24,-23,-22,-14,-13,-12,-11,-10,-9,-8,-7,-6,-1,-38,0,12,3,1,0,2,17,3,17,0,63,0,-15,58,-13,127,26,127,-56,-51,119,-1,0,0,-1,0,-48,22,-109,-2,18,125,115,-2,127,-65,-14,18,127,-123,102,-34,-35,79,121,114,-9,55,47,-26,74,-8,-36,-40,3,56,24,29,61,-123,126,23,-57,-100,121,-105,113,14,93,12,46,22,19,82,83,82,-9,-108,82,-78,-116,-105,73,61,117,93,15,-25,14,42,-30,-84,30,111,-124,-115,10,17,-110,106,73,-22,-107,-83,102,-70,55,-36,-1,-39};
    

    下一步是,用它创建cv::Mat。我想我必须使用

    Mat imdecode(InputArray buf, int flags)
    

    要做到这一点,我不知道怎么做。我找到了一个解决方案,但我并不喜欢它,因为它需要在OpenCV数据类型之间进行不必要的转换:

    CvMat mat = cvMat(16, 16, CV_8UC3, img);
    IplImage *pIplImage = cvDecodeImage(&mat, 1);
    cv::Mat mtx(pIplImage); 
    cv::imshow("decoded img", mtx);
    cvReleaseImage(&pIplImage);
    cv::waitKey();
    

    有没有直接的方法来转换 已签署 字符数组到cv::Mat?

    编辑: 只需清除数据格式,就可以直接将字符数组保存到jpeg文件中,并使用随机图像查看器打开它

    FILE *output;
    output = fopen("test.jpg", "wb");
    fwrite(img,sizeof(char),sizeof(img),output);
    fclose(output);
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Mark Setchell    6 年前

    我想你的意思是:

    #include <iostream>
    #include <opencv2/opencv.hpp>
    #include <cstdio>
    
    using namespace cv;
    using namespace std;
    
    int
    main(int argc,char*argv[])
    {
    char img[] = {-1,-40,-1,-32,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0,-1,-37,0,67,0,5,3,4,4,4,3,5,4,4,4,5,5,5,6,7,12,8,7,7,7,7,15,11,11,9,12,17,15,18,18,17,15,17,17,19,22,28,23,19,20,26,21,17,17,24,33,24,26,29,29,31,31,31,19,23,34,36,34,30,36,28,30,31,30,-1,-37,0,67,1,5,5,5,7,6,7,14,8,8,14,30,20,17,20,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,-1,-64,0,17,8,0,16,0,16,3,1,34,0,2,17,1,3,17,1,-1,-60,0,31,0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,-1,-60,0,-75,16,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,125,1,2,3,0,4,17,5,18,33,49,65,6,19,81,97,7,34,113,20,50,-127,-111,-95,8,35,66,-79,-63,21,82,-47,-16,36,51,98,114,-126,9,10,22,23,24,25,26,37,38,39,40,41,42,52,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,-125,-124,-123,-122,-121,-120,-119,-118,-110,-109,-108,-107,-106,-105,-104,-103,-102,-94,-93,-92,-91,-90,-89,-88,-87,-86,-78,-77,-76,-75,-74,-73,-72,-71,-70,-62,-61,-60,-59,-58,-57,-56,-55,-54,-46,-45,-44,-43,-42,-41,-40,-39,-38,-31,-30,-29,-28,-27,-26,-25,-24,-23,-22,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-1,-60,0,31,1,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,-1,-60,0,-75,17,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,119,0,1,2,3,17,4,5,33,49,6,18,65,81,7,97,113,19,34,50,-127,8,20,66,-111,-95,-79,-63,9,35,51,82,-16,21,98,114,-47,10,22,36,52,-31,37,-15,23,24,25,26,38,39,40,41,42,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,-126,-125,-124,-123,-122,-121,-120,-119,-118,-110,-109,-108,-107,-106,-105,-104,-103,-102,-94,-93,-92,-91,-90,-89,-88,-87,-86,-78,-77,-76,-75,-74,-73,-72,-71,-70,-62,-61,-60,-59,-58,-57,-56,-55,-54,-46,-45,-44,-43,-42,-41,-40,-39,-38,-30,-29,-28,-27,-26,-25,-24,-23,-22,-14,-13,-12,-11,-10,-9,-8,-7,-6,-1,-38,0,12,3,1,0,2,17,3,17,0,63,0,-15,58,-13,127,26,127,-56,-51,119,-1,0,0,-1,0,-48,22,-109,-2,18,125,115,-2,127,-65,-14,18,127,-123,102,-34,-35,79,121,114,-9,55,47,-26,74,-8,-36,-40,3,56,24,29,61,-123,126,23,-57,-100,121,-105,113,14,93,12,46,22,19,82,83,82,-9,-108,82,-78,-116,-105,73,61,117,93,15,-25,14,42,-30,-84,30,111,-124,-115,10,17,-110,106,73,-22,-107,-83,102,-70,55,-36,-1,-39};
    
        Mat rawData(1,sizeof(img),CV_8SC1,(void*)img);
        Mat decodedMat= imdecode(rawData,CV_LOAD_IMAGE_ANYDEPTH);
        imwrite("a.jpg",decodedMat);
    }