Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

C# exception calling ParticleFilter2 method

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

0 Kudos
Message 1 of 8
(5,543 Views)

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

Greg Stoll
LabVIEW R&D
0 Kudos
Message 2 of 8
(5,537 Views)

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

 

0 Kudos
Message 3 of 8
(5,522 Views)

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

Waleed El-Badry MSc.,MCPD, ISTQB Certified Tester
Assistant Lecturer
Mechatronics Department
Faculty of Engineering
Misr University for Science & Technology



View Waleed El-Badry's profile on LinkedIn

0 Kudos
Message 4 of 8
(5,508 Views)

Hi again,

     I had to mention earlier that you have to perform the following modifications:

 

  1. Remove  AxCWIMAQControlsLib from Refernces.
  2. Add NationalInstruments.AxCWIMAQControlsLib.Interop.dll instead which can be found   at \ Program Files\National Instruments\Vision\dotNET\Assemblies\8.5.0.0
  3. You may have build error, replace all AxCWIMAQControlsLib in your code with

    NationalInstruments.CWIMAQControls

  4. Replace CWIMAQIMAGECLASS with CWIMAQIMAGE.

 Apparently you used legacy ActiveX IMAQ control.

Waleed El-Badry MSc.,MCPD, ISTQB Certified Tester
Assistant Lecturer
Mechatronics Department
Faculty of Engineering
Misr University for Science & Technology



View Waleed El-Badry's profile on LinkedIn

0 Kudos
Message 5 of 8
(5,495 Views)

Isasi -

 

Did  VBCoder's advice solve your problem?

 

Greg Stoll

Vision R&D

National Instruments

Greg Stoll
LabVIEW R&D
0 Kudos
Message 6 of 8
(5,485 Views)

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

0 Kudos
Message 7 of 8
(5,465 Views)
It is good to hear that. And you are welcome in the community to post any problems.
Waleed El-Badry MSc.,MCPD, ISTQB Certified Tester
Assistant Lecturer
Mechatronics Department
Faculty of Engineering
Misr University for Science & Technology



View Waleed El-Badry's profile on LinkedIn

0 Kudos
Message 8 of 8
(5,463 Views)