libgphoto2 convert char* to cv::Mat
I am using libgphoto2 to get images from a still camera, and I am not able to convert the images into cv::Mat data type. The image from the camera is stored as char*, and when I convert the image, the image is solid blue. This is the relevant code I have for the question.
Camera *camera;
gp_camera_new (&camera);
GPContext *context = gp_context_new();
char *data;
unsigned long size;
FILE *f;
capture_to_memory(camera, context, (const char**)&data, &size);
f = fopen("foo2.jpg", "wb");
retval=fwrite(data, size, 1, f);
fclose(f);
cv::Mat tempMat = cv::Mat(3456, 5184, CV_8UC3, (unsigned char) *data);
I am able to write the data to a file and reload the image in opencv, but I don't want to save and load from file every time I capture an image.
you probably need to show, where the data ptr is coming from, what format the image is in originally, etc.
The data pointer is filled using the function gp_file_get_data_and_size from libgphoto2 library inside the capture_to_memory function using *data = file->data where file is the camerafile libgphoto data type, but I can't find anymore information about that struct. The data can be saved to a .jpg using fwrite. Look at my edit to see how the data is stored to file. I believe I should be able to convert to a cv::Mat instead of writing to a file then reloading the image.
thanks for clarifying, that's actually quite useful.
if it can be saved to disk as a jpeg, that is not pixel data, but a full fledged jpg image (as on disk), headers, exif data, compressed pixels and all.
you probably want imdecode) instead of the Mat constructor
Thanks! imdecode is what I needed.