Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

Checking if a CWIMAQRegion object is really a CWIMAQRectangle object in .NET

I am using C# .NET to do some vision work. I have a viewer object that is displaying an image and allows a user to use the toolbar to mark regions on the image. In the background, I pull the region from the image (if any regions exist) and want to use the marked region to perform a different function. My purposes require that the region be a rectangle so I want to make sure that the region I am pulling from the viewer is, indeed, a rectangle.

So what I would like to do is something along the lines of

if ( region_from_viewer is an instance of CWIMAQRectangle ) do_things;

I have tried using the IsInstanceOfType method but the problem is that the method "goes both ways" so to speak. In other words, if I invoke the method in this fashion:

RegionObject.IsInstanceOfType(RectangleObject), it will always be true because the InstanceOf method checks to see if one of two things is true (from the MSDN docs):

if the current Type is in the inheritance hierarchy of the object represented by [...] parameter, or if the current Type is an interface that [the parameter] supports

that is to say, if the function as implemented above returns true it just is simply indicating that the Rectangle Object supports the CWIMAQRegion interface, but what I really need to know is if that RegionObject can legally be cast down to a CWIMAQRectangle object (ie. is the RegionObject *really* an instance of CWIMAQRectangle).

the only way I currently know to do this is to simply attempt the cast, catch any exceptions and take that as an appropriate indication of what I need to know. That's pretty dirty though...so if anyone knows of another way, that would be great. Below is some code I have written to further clarify what I am trying to do. Note, I am still using the IsInstanceOf() method since using it "sounds" right when, in fact, it doesnt seem to be the appropriate function.

=====================================================================

CWIMAQRegionsClass searchRegion = new CWIMAQRegionsClass();
CWIMAQRectangle searchRectangle = new CWIMAQRectangle();
int LeftBoundry = currentImage.Width/4;
int TopBoundry = currentImage.Height/4;
searchRectangle.Initialize(LeftBoundry, TopBoundry, LeftBoundry * 2, TopBoundry * 2);

CWIMAQRegion viewerRegion;
if(imageViewer.Regions.Count > 0)
{
viewerRegion = imageViewer.Regions.Item(1);
if(viewerRegion.GetType().IsInstanceOfType(searchRectangle))
searchRegion.AddRectangle((CWIMAQRectangle)viewerRegion);
else
searchRegion.AddRectangle(searchRectangle);
}
else
searchRegion.AddRectangle(searchRectangle);

=====================================================================

thanks in advance for help!
0 Kudos
Message 1 of 3
(3,024 Views)
Hello aroon,

Thank you for contacting National Instruments.

I have had success using the CompareTo method of the region's Shape object. If the CompareTo method returns 0, then the types are the same. In the code you attached, you could modify the second if statement to the following:

if(viewerRegion.Shape.CompareTo(CWIMAQRegionShapes.cwimaqRegionRect) == 0)

I ran a quick test on this, and I believe it will accomplish what you need.

Best Regards,

Jesse D.
Applications Engineering
National Instruments
0 Kudos
Message 2 of 3
(3,009 Views)
ah hah! thanks jesse, that worked!
0 Kudos
Message 3 of 3
(3,007 Views)