Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

NI-IMAQ imgGrab() steps

I have a frame grabber NI-PCIe-1437 by National Instruments.

I am trying to code in C++ to run and obtain images from it.

 

The following code was referenced and written from the NI-IMAQ Help manual.

int main()
{
  INTERFACE_ID interfaceID;
  SESSION_ID sessionID;

    if (imgInterfaceOpen("img0", &interfaceID) == IMG_ERR_GOOD) // "img0" is interfacename
    {
        std::cout << "Interface is open" << std::endl;
        if (imgSessionOpen(interfaceID, &sessionID) == IMG_ERR_GOOD)
        {
            std::cout << "Session is open" << std::endl

            imgGrabSetup(sessionID, TRUE); //manually start acquisition with imgSessionStartAcquisition

            imgSessionStartAcquisition(sessionID);

            imgGrab(sessionID, NULL, TRUE);

            imgSessionStopAcquisition(sessionID);

            imgClose(sessionID, FALSE); // closes the session using the imgClose function
            std::cout << "Session is closed" << std::endl;
        }
                           
        imgClose(interfaceID, FALSE);   // closes the interface using the imgClose function
    
     }
}
 

My enquiries are these:

  1. Have I correctly understood/implemented the steps?
  2. How do I go about visualising the image? Possibly using imgPlot?

 

My end goal is to obtain an interference signal from the frame grabber.

shiquan35_1-1633420983327.png

 

 

 

0 Kudos
Message 1 of 6
(2,085 Views)
int main()
{
  INTERFACE_ID interfaceID;
  SESSION_ID sessionID;

    if (imgInterfaceOpen("img0", &interfaceID) == IMG_ERR_GOOD) // "img0" is interfacename
    {
        std::cout << "Interface is open" << std::endl;
        if (imgSessionOpen(interfaceID, &sessionID) == IMG_ERR_GOOD)
        {
            std::cout << "Session is open" << std::endl

            imgGrabSetup(sessionID, TRUE); //manually start acquisition with imgSessionStartAcquisition

            imgSessionStartAcquisition(sessionID);

            imgGrab(sessionID, NULL, TRUE);
            
            // get attributes to get pixel depth of camera
	        imgGetAttribute(sessionID, IMG_ATTR_BITSPERPIXEL, &bitsPerPixel); // obtain pixel depth, then store in bitsPerPixel

			// declare plotFlag according to pixel depth, bitsPerPixel
			switch (bitsPerPixel)
			{
			case 10:
				plotFlag = IMGPLOT_MONO_10;
				break;
			case 12:
				plotFlag = IMGPLOT_MONO_12;
				break;
			case 14:
				plotFlag = IMGPLOT_MONO_14;
				break;
			case 16:
				plotFlag = IMGPLOT_MONO_16;
				break;
			case 24:
			case 32:
				// assumes that a 24 bits camera is a color camera.
				// in this mode, even if the camera is 24 bits the board returns 32 bits values
				plotFlag = IMGPLOT_COLOR_RGB32;
				break;
			default:
				plotFlag = IMGPLOT_MONO_8;
				break;
			}
            
            imgPlot2(ImaqSmplHwnd, NULL, 0, 0, AcqWinWidth, AcqWinHeight, CanvasLeft, CanvasTop, plotFlag);

            imgSessionStopAcquisition(sessionID);

            imgClose(sessionID, FALSE); // closes the session using the imgClose function
            std::cout << "Session is closed" << std::endl;
        }
                           
        imgClose(interfaceID, FALSE);   // closes the interface using the imgClose function
    
     }
}

This is the updated code; included imgPlot() but I couldn't obtain any form of image output.

0 Kudos
Message 2 of 6
(2,063 Views)

Hello shiquan35,

 

Could you please provide more depth information: 

  • What do you want to do?
  • What environment do you work in? 
  • What driver version do you use?

Best regards.

0 Kudos
Message 3 of 6
(2,011 Views)

How do you think this can work?

 

imgGrab() has three parameters, the second is for a pointer to a memory buffer in which the function writes the image into. You pass NULL so the function can store the image information nowhere at all. If you did not just ignore all error return codes for all functions you would have seen that this function returns an error as it received an invalid buffer parameter.

 

What you should do is to create a variable somewhere of type void * and assign NULL to it, then pass the reference to that variable as second parameter.

 

Then there is imgPlot2(). Where did you setup the ImgSamplHwnd? This must be a valid HWND for a window that exists somewhere. And as second parameter you have a buffer, and yes here you need to pass the buffer in, that you got from imgGrab(). And of course you need to make sure that the  AcqWinWidth and AcqWinHeight do match with the actual acquisition ROI, as the data in the buffer will only contain the data as provided by the camera and you can't just invent your own image size.

 

And last but not least you need to dispose of the buffer returned by imgGrab() by calling imgDisposeBuffer().

Rolf Kalbermatter
My Blog
Message 4 of 6
(1,974 Views)

Thanks for the answer .It cleared my confusion very well. Will you please explain the line below a bit more.

 

"Where did you setup the ImgSamplHwnd? This must be a valid HWND for a window that exists somewhere."

0 Kudos
Message 5 of 6
(1,930 Views)

@outdoorbasketballshop wrote:

Thanks for the answer .It cleared my confusion very well. Will you please explain the line below a bit more.

 

"Where did you setup the ImgSamplHwnd? This must be a valid HWND for a window that exists somewhere."


imgPlot2() takes as first argument a window handle to plot the image into, so it better is a valid window handle that you created somewhere.

Rolf Kalbermatter
My Blog
0 Kudos
Message 6 of 6
(1,922 Views)