Using OpenCV with MFC - Using a matrox meteor II-MC

  | Back |

Here I will add code that grab frames from a Matrox Meteor II-MC and other Matrox frame grabbers.

Before continuing with this guide be sure that the installation of the framegrabber is ok - test the installation with the Intellicam application. If the Intellicam application can connect with your camera it should be possible to continue.

The step one here and two here must have been run through before this section can be used.

Press Alt+F7 or click on "Project" in the menu bar and then on "Settings" to get the "Project Settings" dialog. Mark the Project "MyApplication". Choose "All Configuration" in the "Settings for" roll down menu.
Click on the tab "C/C++" and "Preprocessor" in the roll down menu.
In the "Additional include directories" add "C:\Program Files\Matrox Imaging\mil\include" to the comma separated list.

Still with "All Configuration" in the "Settings for" roll down menu. Choose the "Link" Tab and "input" in the "Category" roll down menu and add "C:\Program Files\Matrox Imaging\mil\library\winnt\msc\dll" to the comma separated "Additional Library path".

Add "mil.lib" to the space separated "Object/library modules" list.

Open MyIplClass.h and add the following include directive under the others.

- - - Start cut'n paste - - -
#include "mil.h"
- - - End cut'n paste - - -

Double click on TheImage member variable of CMyApplicationDlg in the CVB and add the following code below it:

- - - Start cut'n paste - - -
     MIL_ID MilApplication; // Application identifier.
     MIL_ID MilSystem; // System identifier.
     MIL_ID MilDigitizer; // Digitizer identifier.
     MIL_ID MilImage; // Image buffer identifier.
     MIL_ID MilDisplay;
     long NumberOfDigitizer; // Number of digitizers available on the system
     long SizeX; // Buffer Size X
     long SizeY; // Buffer Size Y
     long DigSizeX; // Digitizer input width
     long DigSizeY; // Digitizer input heigh
     long Band; // Number of input color bands of the digitizer
     BOOL GrabIsStarted; // State of the grab
     HWND hWnd;

- - - End cut'n paste - - -

Doubleclick on OnInitDialog() and add the following code to allocate and initialize the Matrox Meteor Framegrabber before the "return TRUE;" statement.


- - - Start cut'n paste - - -
     // Allocate an application
     MappAlloc(M_DEFAULT,&MilApplication);
     // Allocate a system
     MsysAlloc(M_SYSTEM_SETUP,M_DEF_SYSTEM_NUM,M_COMPLETE,&MilSystem);
     // Inquire number of digitizers available on the system
     MsysInquire(MilSystem,M_DIGITIZER_NUM,&NumberOfDigitizer);

     // Digitizer is available
     if (NumberOfDigitizer)
     {
          // Allocate a digitizer
          MdigAlloc(MilSystem,M_DEFAULT,M_CAMERA_SETUP,M_DEFAULT,&MilDigitizer);

          // Stop live grab when window is disable
          MsysControl(MilSystem,M_STOP_LIVE_GRAB_WHEN_DISABLED,M_ENABLE);
          // Inquire digitizer informations
          MdigInquire(MilDigitizer,M_SIZE_X,&DigSizeX);
          MdigInquire(MilDigitizer,M_SIZE_Y,&DigSizeY);
          MdigInquire(MilDigitizer,M_SIZE_BAND,&Band);

          MbufAlloc2d(MilSystem, DigSizeX, DigSizeY,8L+M_UNSIGNED, M_IMAGE+M_GRAB, &MilImage);
          MdispAlloc(MilSystem, M_DEF_DISPLAY_NUM, M_DEF_DISPLAY_FORMAT, M_WINDOWED, &MilDisplay);

     if (DigSizeX > M_DEF_IMAGE_SIZE_X_MAX)
               DigSizeX = M_DEF_IMAGE_SIZE_X_MAX;
     if (DigSizeY > M_DEF_IMAGE_SIZE_Y_MAX)
               DigSizeY = M_DEF_IMAGE_SIZE_Y_MAX;
     }
     // Digitizer is not available
     else
     {
          SizeX = M_DEF_IMAGE_SIZE_X_MIN;
          SizeY = M_DEF_IMAGE_SIZE_Y_MIN;
          Band = M_DEF_IMAGE_NUMBANDS_MIN;
     }

     // Initialize the state of the grab
     GrabIsStarted = FALSE;

- - - End cut'n paste - - -

In the ResourceView Browser click on MyApplication resources then Dialog and finally double click on IDD_MYAPPLICATION_DIALOG to be able to edit the dialog. Resize the dialog to a size fitting the video output and remember also to adjust the two #defines IMAGE_WIDTH and IMAGE_HEIGHT. Add a button to the lower right part of the dialog -Right click on it and through properties change the ID: to "IDC_GRAB" and the Caption to "Grab". Close the Properties dialog box and double click on the new button to add a function called "OnGrab" Press OK to get to the code. Paste the following into the function:

- - - Start cut'n paste - - -
     CWnd *pButton; // Change the caption dependent on the grab state

     hWnd = AfxGetMainWnd()->m_hWnd;
     MdispSelectWindow(MilDisplay, MilImage, hWnd);

     if (GrabIsStarted == FALSE)
     {
         // Start a continuous grab in this view
         MdigGrabContinuous(MilDigitizer, MilImage);

         // Update the variable GrabIsStarted
         GrabIsStarted = TRUE;

         pButton = GetDlgItem(IDC_GRAB);
         pButton->SetWindowText("Stop");
     }
     else
     {
         // Stop a continuous grab in this view
         MdigHalt(MilDigitizer);

         // Update the variable GrabIsStarted
         GrabIsStarted = FALSE;

         pButton = GetDlgItem(IDC_GRAB);
         pButton->SetWindowText("Grab");

         // Now get the Grabbed pixels
         MbufInquire(MilImage, M_HOST_ADDRESS, &TheImage->imageData);

         RedrawWindow(NULL, NULL, RDW_INVALIDATE ); //Force update of dialog;
     }

- - - End cut'n paste - - -

In the function CMyApplicationDlg::OnPaint() change the code in the else part to:

- - - Start cut'n paste - - -
     if (!GrabIsStarted)
          DisplayMyData(); //Only redraw iplimage if not grabbing
     CDialog::OnPaint();

- - - End cut'n paste - - -

Add a function "SetIplData(IplImage *ipl)" returning void, to the class MyIplClass. and add the code:

- - - Start cut'n paste - - -
     memcpy(m_Ipl->imageData, ipl->imageData, m_Ipl->imageSize);
- - - End cut'n paste - - -

Change the code in CMyApplicationDlg::OnProcess() to:

- - - Start cut'n paste - - -
     MyIplClass *Img= new MyIplClass;
     Img->SetIplData(TheImage); //Copy the content of TheImage to the class for processing
     Img->ProcessIpl();
     Img->GetIplData( TheImage );
     delete Img;
     RedrawWindow(NULL, NULL, RDW_INVALIDATE ); //Force update of dialog;

- - - End cut'n paste - - -

Thats it! hit F5 to build and run your application! Press the Grab button to start grabbing, then on Stop and on Process to use OpenCV on the grabbed image.

Press on the link to go back to the start go the tutorial.

^ back to top ^

 

Last updated:
May 3 - 2004: My counter from freewebcounter.com created popups, so a new counter is used.
May 23 2003: Simpler use of MbufInquire destination in OnGrab().
May 6 2003 uploaded