converting jpeg char data to opencv mat

≯℡__Kan透↙ 提交于 2019-12-13 03:01:44

问题


i have a mjpeg tcp stream and want to modify it. So i can access the image data via recv-function and save it in a char-buffer.

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

Now i can extract the jpeg image data out of the char-array; i created a test char array (16*16 jpeg image):

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};

The next step is, to create a cv::Mat out of it. I guess i have to use

Mat imdecode(InputArray buf, int flags)

to achieve this, but i dont know how. I found a solution, but I don't really like it, cause it needs an unnecessary conversion between OpenCV data types:

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();

Is there a direct way to convert a signed char-array to cv::Mat?

edit: just to clearify the data format, you can directly save the char-array to a jpeg file and open it with a random image viewer

FILE *output;
output = fopen("test.jpg", "wb");
fwrite(img,sizeof(char),sizeof(img),output);
fclose(output);

回答1:


I think you mean this:

#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);
}


来源:https://stackoverflow.com/questions/49492053/converting-jpeg-char-data-to-opencv-mat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!