Tag Archives: ximea opencv example

Capture opencv frame from Ximea MU9PC camera

 

#include <QtCore/QCoreApplication>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <QDebug>
#include <string>

#include <xiApi.h>
#include <xiExt.h>


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);


    XI_IMG image;
            image.size = sizeof(XI_IMG);
            image.bp = NULL;
            image.bp_size = 0;





            XI_RETURN stat = XI_OK;

            DWORD dwNumberOfDevices = 0;
            stat = xiGetNumberDevices(&dwNumberOfDevices);

            HANDLE handle=NULL;
            // open cameras

            stat = xiOpenDevice(0, &handle);

            char serial_number1[100]="";
            xiGetParamString(handle, XI_PRM_DEVICE_SN, serial_number1, sizeof(serial_number1));





            //Setting parameters for camera
            stat = xiSetParamInt(handle, XI_PRM_TRG_SOURCE, XI_TRG_SOFTWARE);

            stat = xiSetParamInt(handle, XI_PRM_GPO_SELECTOR, 2);

            stat = xiSetParamInt(handle, XI_PRM_GPO_MODE,  XI_GPO_FRAME_ACTIVE_NEG);


            // Setting "exposure" parameter

            stat = xiSetParamInt(handle, XI_PRM_EXPOSURE, 0.04);



            stat = xiSetParamInt(handle, XI_PRM_GAIN, 5);




            stat = xiStartAcquisition(handle);





            int ival;
            xiGetParamInt( handle, XI_PRM_EXPOSURE, &ival);




            int isColor = 0;

            stat = xiGetParamInt(handle, XI_PRM_IMAGE_IS_COLOR, &isColor);


            if(isColor)	// for color cameras
            {
                    // default image format RGB24
                    stat = xiSetParamInt( handle, XI_PRM_IMAGE_DATA_FORMAT, XI_RGB24);
                    stat = xiSetParamInt( handle, XI_PRM_AUTO_WB, 1);
            }



            stat = xiSetParamInt(handle, XI_PRM_DOWNSAMPLING_TYPE, XI_SKIPPING);
            stat = xiSetParamInt(handle, XI_PRM_DOWNSAMPLING, 4);



           int w;
           xiGetParamInt( handle, XI_PRM_WIDTH, &w);
           int h;
           xiGetParamInt( handle, XI_PRM_HEIGHT, &h);



            stat = xiSetParamInt( handle, XI_PRM_WIDTH, w);
            stat = xiSetParamInt( handle, XI_PRM_HEIGHT, h);




            Sleep(123); // wait for right moment to trigger the exposure

            // trigger acquisition on Master camera
            int retry=300;
            while (retry--)
            {
                    xiSetParamInt(handle, XI_PRM_TRG_SOFTWARE, 0);
                    stat = xiGetImage(handle, 10, &image);
                    if (stat!=XI_OK)
                            printf("Error after xiGetimage (%d)\n",stat);
                    else
                    {
                        IplImage * frame;
                        frame = cvCreateImage(cvSize( image.width, image.height), IPL_DEPTH_8U,3);
                        memcpy( frame->imageData, image.bp, image.width*image.height*3);
                        
                        cvShowImage("top",frame);
                        cvReleaseImage(&frame);
                        cvWaitKey(1);
                    }
            }

            if (handle) xiCloseDevice(handle);




    return a.exec();
}

 

 

 

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS