mat - How to process a JPEG binary data in OpenCV? -


i trying process jpeg binary data in opencv. when segmentation fault (core dumped).

i read jpeg file through fread command , stored in buffer.

after reading, copied buffer data mat variable, when tried grayscale conversion on copied data using cvtcolor opencv function. segmentation fault.

int main( int argc, char** argv ) {      mat threshold_output;     mat gray_image;      unsigned char *pre_image;     file *read_image;     file *write_image;     int filesize;     size_t data, write;      read_image = fopen(argv[1] , "rb"); //read jpeg binary     write_image = fopen("output11.jpg", "wb"); //write jpeg      if(read_image == null)     {         printf("image not found\r\n");     }     fseek(read_image, 0, seek_end);     int filelen = ftell(read_image);     fseek(read_image, 0, seek_set);      pre_image = (unsigned char *)malloc(filelen);     data = fread(pre_image, 1, filelen, read_image);     write = fwrite(pre_image, 1, filelen, write_image);      // printed , verify values     printf("file size %d\r\n", filelen);     printf("read bytes %zu\r\n", data);     printf("write bytes %zu\r\n", data);      fclose(read_image);     fclose(write_image);      /* copy jpeg binary buffer mat variable*/       cv::mat image(size(640, 480), cv_8uc3, pre_image); //seg fault comes here     /* convert grayscale */     cvtcolor( image, gray_image, cv_bgr2gray);     /* threshold conversion */     threshold( gray_image, threshold_output, 80, 255, thresh_binary );      namedwindow( "thresholded", cv_window_autosize );     imshow( "thresholded", image );      waitkey(0);     return 0; } 

i have attached code reference. have verified both fread , fwrite works properly. when cvtcolor got error.

as @micka pointed out, should use cv::imdecode

you can use file*. may want use fstreams if you're using c++. can rely directly on opencv capabilities read files.

the code below show these options reading files. code writing similar (i can add if need it).

remember if want write binary stream, should use imencode

#include <opencv2\opencv.hpp> #include <fstream> #include <stdio.h>  using namespace std; using namespace cv;  int main() {      ////////////////////////////////     // method 1: using file*     ////////////////////////////////      file* read_image = fopen("path_to_image", "rb");     if (read_image == null)     {         printf("image not found\n");     }      fseek(read_image, 0, seek_end);     int filelen = ftell(read_image);     fseek(read_image, 0, seek_set);      unsigned char* pre_image = (unsigned char *)malloc(filelen);     size_t data = fread(pre_image, 1, filelen, read_image);      // printed , verify values     printf("file size %d\n", filelen);     printf("read bytes %d\n", data);      fclose(read_image);      vector<unsigned char> buffer(pre_image, pre_image + data);     mat img = imdecode(buffer, imread_anycolor);      ////////////////////////////////     //// method 2: using fstreams     ////////////////////////////////      //ifstream ifs("path_to_image", iostream::binary);     //filebuf* pbuf = ifs.rdbuf();     //size_t size = pbuf->pubseekoff(0, ifs.end, ifs.in);     //pbuf->pubseekpos(0, ifs.in);     //vector<char> buffer(size);     //pbuf->sgetn(buffer.data(), size);     //ifs.close();     //mat img = imdecode(buffer, imread_anycolor);       ////////////////////////////////     //// method 3: using imread     ////////////////////////////////      //mat img = imread("path_to_image", imread_anycolor);       // work img want      imshow("img", img);     waitkey();       return 0; } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -