Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

Using NI IMAQ card with MATLAB

Hi.
I'm trying to use a NI IMAQ with MATLAB. I've created a gui application which contains an activex control (cwimaq.ocx) and tried to execute it.
The acquiring command is very simple, and looks like that:
 
handles.activex1.AcquireImage
 
The return code was 0 (which I undrestand is OK)
The problem is that I dont know how to extract the aqcuired image into a MATLAB valid structure (or an array).
 
Hope someone could help me,
Amnon.
0 Kudos
Message 1 of 5
(4,040 Views)
Amnon,

If the only thing you would like to do is display the image, it would probably be easiest to use the CWIMAQViewer contlrol that comes with the ActiveX component you referenced in your original question (cwimaq.ocx).  That is if Matlab supports the control in its GUI.  If, on the other hand, you would like to do some image processing of some sort using Matlab (or if the CWIMAQViewer is not supported in Matlab) it would probably be best to use the ImageToArray method to convert your image to a 2-D array.  Your code would look something like this:

ImageArray = handles.activex1.Images.Item(1).ImageToArray

Where ImageArray is the Matlab array you want to store your pixel values in.

Hope this helps.

S. Arves S.
National Instruments
Applications Engineer
0 Kudos
Message 2 of 5
(4,029 Views)

Thanks a lot!

I'll try this as soon as I can (a little technical problem). my goal is to process images which was acquired with NI device using MALAB.
Before trying to use the activeX component using the MATLAB GUI, I've tried to use the DLL, a try which led to MATLAB's crash, I have probably made a big mistake, so maybe you can help me with that too?

This is the MATLAB script which I was trying to run:

loadlibrary('C:\WINNT\system32\imaq.dll','C:\Program Files\National Instruments\NI-IMAQ\include\niimaq.h','alias','imaqlib');

 name='img0';
 Iname=int8(name);
 pname=libpointer('int8Ptr',int8(name));

 intrfc=libpointer('uint32Ptr',0);
 [err pname intrfc] = calllib('imaqlib','imgInterfaceOpen',pname,intrfc)

 sId=libpointer('uint32Ptr',0);
 [err sId]=calllib('imaqlib','imgSessionOpen',intrfc,sId)

 [err]=calllib('imaqlib','imgGrabSetup',sId,1)
 img=libpointer('voidPtrPtr',0);
 [err img]=calllib('imaqlib','imgSnap',sId ,img)
  
MATLAB crashes on the call to "imgSnap".
Thanks again!
Amnon.
0 Kudos
Message 3 of 5
(4,012 Views)

Updating-

The command you've suggested doesn't work. an error occurs, as following:

    ??? No appropriate method or public field Item for class Interface.CWIMAQControlsLib_CWIMAQ_1.Images.

Since I've tried to use this activeX in VB too, I know there are methods which exist when using VB, but does not appear
when trying to use the "methods" command in MATLAB. The same is for the field 'Item' of 'Images'- MATLAB doesn't
recognize it.

Hope you can help me further on this,
Amnon.

0 Kudos
Message 4 of 5
(4,006 Views)
Amnon,

After taking a look at the two functions (AcquireImage, and ImageToArray) the only differences I can see that concern me are in the return types.  AcquireImage returns a Long Integer while ImageToArray returns a Variant.  It may be that MATLAB does not support the Variant data type and therefore doesn't provide access to that function, but I'm not sure.  I'm not all that familliar with The MathWorks software and do not have access to it here at NI so I can't really advise you on wether you were calling the dlls correctly or as to why the ActiveX functions are available.  I think your best bet right now would be to contact The MathWorks on MATLAB syntax and why various ActiveX functions are not available to you in MATLAB.
S. Arves S.
National Instruments
Applications Engineer
0 Kudos
Message 5 of 5
(3,983 Views)