Canon LiveView: image convertion to OpenCV Mat

2019-05-31 11:24发布

问题:

I'm trying to show LiveView image in real time. I use EDSDK 2.14 + Qt5 + opencv+mingw32 under Windows. I'm not very sophisticated in image processing so now I have the following problem. I use example from Canon EDSDK and all was ok until this part of code:

// // Display image //

I googled a lot of examples but all of them was written on C# or MFC or VB. Also I found advise to use libjpegTurbo for decompressing image and then showing it using opencv. I tried to use libjpegTurbo but failed to undestand what to do :(. Maybe somebody here have code example of the conversion LiveView stream to opencv Mat or QImage (because I use Qt)?

回答1:

Based on the Canon EDSDKs example, you may append your EdsStreamRef 'stream' data with its correct length into a QByteArray. Then, use for example the following to parse the raw data from the QByteArray as a JPG into a new QImage: QImage my_image = QImage::fromData(limagedata,"JPG"); Once it's in a QImage you can convert it into a OpenCV cv::Mat (see How to convert QImage to opencv Mat)



回答2:

Here is what worked for me after following the SAMPLE 10 from the Canon EDSDK Reference. It's a starting point for a more robust solution.

In the downloadEvfData function, I replaced the "Display Image" part by the code bellow:

unsigned char *data = NULL;
EdsUInt32 size = 0;
EdsSize coords ;

// get image coordinates
EdsGetPropertyData(evfImage, kEdsPropsID_Evf_CoordinateSystem, 0, sizeof(coords), &coords);

// get buffer pointer and size
EdsGetPointer(stream, (EdsVoid**)&data);
EdsGetLenth(stream, &size);

//
// release stream and evfImage
//

// create mat object
Mat img(coords.height, coords.width, CV_8U, data);
image = imdecode(img, CV_LOAD_IMAGE_COLOR);

I've also changed the function signature:

EdsError downloadEvfData(EdsCameraRef camera, Mat& image)

And in the main function:

Mat image;
namedWindow("main", WINDOW_NORMAL);
startLiveView(camera);
for(;;) {
    dowloadEvfData(camera, image);
    imshow("main", image);
    if (waitkey(10) >= 0);
        break;
}


回答3:

Well it depends on the format of the liveview-stream.

There must be some kind of delimiter in it and you need then to convert each image and update your QImage with it.

Check out this tutorial for more information: Canon EDSDK Tutorial in C#



回答4:

Well, I know how to convert LiveView stream using c#, Vb, MFC and ATL but my idea is to create multiplatform application so I choose C++ + Qt. I don't want to use microsoft-related libraries and programming languages