Search code examples
c#image-processingdllimportframe-rateframe-grab

Frame Capture using Matrox Commands


I'm working on a project where I have to set the fps of a video stream (as 10) from a camera and grab a frame every 5th frame. I'm working on a program that has already been half written by someone else. The thing is, they have used Matrox Framegrabber dlls. There is also Matrox Frame Grabber on the device. But I cant find any commands for framegrab in C#. I found the following code for C++.

MIL_ID MdispAlloc(SystemId, DispNum, DispFormat, InitFlag,
DisplayIdPtr)

where

MIL_ID SystemId; System identifier
long DispNum; Display number
char *DispFormat; Display format name or file name
long InitFlag; Initialization flag
MIL_ID *DisplayIdPtr; Storage location for the display identifier

The above command allocates a display. Can someone please help me write the program in C#. Also, anyone with experience in Matrox dlls please give me an idea on how to approach frame capture and fps set up. Thanks.


Solution

  • This is for everyone who is new to matrox framegrabber. The first thing you should do is add matrox dll as reference. Be aware that there are currently two matrox versions out- Matrox 9 and Matrox 10. Depending on version of matrox installed in user system dll should be added. (This can be checked by looking for "MIL_PATH" in system directories. Then, declare some variables which will be used in matrox grabbing.

    Some of mine are below:

     public static MIL_ID MilApplication = MIL.M_NULL;       // Application identifier.
        public static MIL_ID MilSystem = MIL.M_NULL;       // System identifier.     
        public static MIL_ID MilDisplay = MIL.M_NULL;       // Display identifier.    
        public static MIL_ID MilDigitizer = MIL.M_NULL;       // Digitizer identifier.  
        public static MIL_ID MilImage = MIL.M_NULL;       // Image identifier.
        public static MIL_ID MilRecord = MIL.M_NULL;       // 8 bit Pointer only for Video Recording.
        public MIL_INT MilINT = MIL.M_NULL;
        public MIL_INT NbPixelsPtr = MIL.M_NULL;
        MIL_ID MilImageDisp = MIL.M_NULL;
        MIL_ID[] MilGrabBufferList = new MIL_ID[BUFFERING_SIZE_MAX];
    

    Then run the following code

    string MilSystemDet = "";
    MilSystemDet = Environment.GetEnvironmentVariable("Mil_Path");
    if (MilSystemDet != null)
    { 
        string dcfFilePath = "";
        FileDialog OpenFile = new OpenFileDialog();
        OpenFile.Filter = "File Formats(*.dcf)|*.DCF;";
        if (OpenFile.ShowDialog() == DialogResult.OK)
        { 
            dcfFilePath = OpenFile.FileName;
            MIL.MdigAlloc(MilSystem, MIL.M_DEFAULT, dcfFilePath, MIL.M_DEFAULT, ref MilDigitizer);
            MIL.MbufAlloc2d(
                MilSystem, 
                MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_X, MIL.M_NULL), 
                MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_Y, MIL.M_NULL), 
                8 + MIL.M_UNSIGNED, 
                MIL.M_IMAGE + MIL.M_DISP + MIL.M_GRAB, 
                ref MilImage);
            MIL.MdispAlloc(MilSystem, MIL.M_DEFAULT, ("M_DEFAULT"), MIL.M_DEFAULT, ref MilDisplay);
            MIL.MdigHalt(MilDigitizer);
        }
    }
    

    When you want to start capture, run the following

     MIL.MbufClear(MilImage, 0);
     MIL.MdigGrabContinuous(MilDigitizer, MilImage);
     MIL.MdispControl(MilDisplay, MIL.M_VIEW_MODE, MIL.M_AUTO_SCALE);
     MIL.MdispControl(MilDisplay, MIL.M_SCALE_DISPLAY, MIL.M_ENABLE);
    

    To copy current image into a buffer, use

    MIL.MbufGet(MilImage, myBuffer);
    

    where myBuffer is a ushort buffer with size equal to total number of pixels in image.

    To save current image to a file, use

    MIL.MbufSave(address,MilImage);
    

    If you dont have a .dcf file you can get a default one from matrox installation cd for free. Or just install matrox viewer and in program files you can have one. The image parameters such as width, height and bit depth are got from dcf file. But if you want, you can allocate them in Mbufalloc2d function above.

    I'll try to check this answer periodically. If anyone has any questions ask me. Ill try to answer them to the best of my knowledge.