09-03-2008 06:17 AM
Hi,
I wrote this question last week in the .NET forum, but it seems that no one knows what is the problem, so I'm trying here too.
I'm trying to call ParticleFilter2 method in my C# code, but it throws an exception when I exceute the code and I don't know why.
This is the code:
*******************************************************************************************
CWIMAQParticleFilter2Data pfd2 = new CWIMAQParticleFilter2Data();
object oNum = new object ();
pfd2.RemoveAll();
pfd2.Add(1);
pfd2.Item(1).Parameter = CWIMAQMeasurementTypes.cwimaqMeasurementArea;
pfd2.Item(1).Range.Initialize(0, 50);
pfd2.Item(1).Exclude = false;
pfd2.Item(1).Calibrated = false;
imaq.ParticleFilter2(imgU8_tmp2, imgU8_tmp1, pfd2, true, ref oNum);
*******************************************************************************************
imgU8_tmp2, and imgU8_tmp1 are image variables and they works with others methods.
imaq is the activeX control AxCWIMAQVision and it works properly in other points of the code, but in this one I recived the followin exception:
An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
Additional information: Exception has been thrown by the target of an invocation.
I don't know if I had something bad defined or if I'm forgotting something but, I can't use neither ParticleFilter2 nor ParticleFilter2. However, I can call ParticleFilter, but I need to use some of the new filtters added in the new one
Anyone knows what is happening?
Thanks in advance
09-03-2008 10:55 AM
Isasi -
I did a similar example on my machine and it seems to work fine. One difference that I noticed is that I had to either specify a connectivity or pass Type.Missing as the 6th argument to ParticleFilter2 - otherwisethe call won't compile.
Can you post a small sample project that exhibits this behavior? I'd be happy to take a look at it and see what's going on...
Greg Stoll
Vision R&D
National Instruments
09-04-2008 02:50 AM
First of all, thanks for your reply
I can compile the project with and without giving the conectivity value. But if I give it (with 1, true or System.Type.Missing), the error is different (the same for the three options):
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll
Additional information: Tipo incorrecto. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
I'm working on Visual Studio 2005 (C#, windows application) and with IMAQ 8.5
I attached a sample project, just read image, theshold it and pass the particle filter method (I added an image example):
Again, thanks for your help
09-04-2008 11:25 AM
Hi Isasi,
Your problem has been solved
modified Code :
object oNumPart = new object(); CWIMAQParticleFilter2Data pf2d = new CWIMAQParticleFilter2Data();
img_tmpU81.Type = CWIMAQImageTypes.cwimaqImageTypeU8;img_tmpU82.Type =
CWIMAQImageTypes.cwimaqImageTypeU8;img_tmpU83.Type = CWIMAQImageTypes.cwimaqImageTypeU8;
//you could've used imaq.ExtractSingleColorPlane instead
imaq.ExtractColorPlanes(img_original, img_tmpU81, img_tmpU82, img_tmpU83, CWIMAQColorFormats.cwimaqColorFormatRGB);imaq.Threshold(img_tmpU82, img_tmpU81, 100, 255, true, 255);
imaq.Copy(img_tmpU81, viewer.Image);
pf2d.RemoveAll();
pf2d.Add(1);
pf2d.Item(1).Parameter = CWIMAQMeasurementTypes.cwimaqMeasurementArea;
pf2d.Item(1).Range.Initialize(0, 500);
pf2d.Item(1).Exclude = false;pf2d.Item(1).Calibrated = false;
//Casting of connectivivity8 as it is boolean in documentation but object in CLR
imaq.ParticleFilter2(img_tmpU81, img_tmpU83, pf2d, true,ref oNumPart,(object)true);
//Converting palette into binary to display the thresholded image
viewer.Image.Type = CWIMAQImageTypes.cwimaqImageTypeU8;viewer.Palette.Type = CWIMAQPaletteTypes.cwimaqPaletteBinary;imaq.Copy(img_tmpU83, viewer.Image);
With Love
09-05-2008 04:40 AM
Hi again,
I had to mention earlier that you have to perform the following modifications:
NationalInstruments.CWIMAQControls
Replace CWIMAQIMAGECLASS with CWIMAQIMAGE.
Apparently you used legacy ActiveX IMAQ control.
09-05-2008 09:51 AM
Isasi -
Did VBCoder's advice solve your problem?
Greg Stoll
Vision R&D
National Instruments
09-08-2008 01:25 AM
Sorry for the delay, but on Friday I've no time to try the new code
But now, I've just try it and it works. I've to change the "using CWIMAQControls" line to "using NationalInstruments.CWIMAQControls" and taking into account the other suggested changes, the code runs perfectly.
Thanks for your help
09-08-2008 01:50 AM