libgphoto2 convert char* to cv::Mat

asked 2014-08-18 13:08:48 -0600

ralff gravatar image

updated 2014-08-18 15:30:32 -0600

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

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.

edit retag flag offensive close merge delete


you probably need to show, where the data ptr is coming from, what format the image is in originally, etc.

berak gravatar imageberak ( 2014-08-18 14:34:39 -0600 )edit

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.

ralff gravatar imageralff ( 2014-08-18 15:25:43 -0600 )edit

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

berak gravatar imageberak ( 2014-08-18 15:33:46 -0600 )edit

Thanks! imdecode is what I needed.

ralff gravatar imageralff ( 2014-08-18 15:44:33 -0600 )edit