Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

How to mask a Region out of a couple different regions

I have defined a couple of regions on an image and I want to mask a region for analysis

 

The " vision.RegionsToMask(mask, viewer.Regions, false, 255);  " function  second argument only accept regions. Right now my solution is to add one region at a time mask it and analyse the mask... I basically have a loop that cycles 72 times just to analyse certain regions on the image. I was wondering if you guys have a better solution for this.

 

Is there a RegionToMask function? I cant seem to find it in cwimaq help file

 

Thanks,

Riscoh

 

0 Kudos
Message 1 of 10
(4,810 Views)

Any suggestions on this guys...

 

Thanks for your time.

0 Kudos
Message 2 of 10
(4,795 Views)
Hi riscoh, I think it might help if you could give me a little more explanation of what you are trying to do. You obviously have found the RegionsToMask function. From you description it sounds like it is doing what you want. Is it just that you do not want to have to do it in a loop? I have not been able to find a function called RegionToMask but if you can explain a little more I might be able to find something that will work for you.
Adam H
National Instruments
Applications Engineer
0 Kudos
Message 3 of 10
(4,771 Views)

Adam,


I basically have defined 72 regions on an image (see attachment) and want to mask each region for analysis. From what Ive understand... the RegionsToMask method will store masked image to a CWIMAQImage object...but if I have defined many regions it will store everything into one image.... correct me if Im wrong... so what I did was remove all the regions first by calling Regions.RemoveAll() function, add a new region, mask the region and do analysis and loop this process a couple of times... see the code below...

 

viewer.Regions.RemoveAll();
viewer.Regions.AddRotatedRectangle(rectangle);
vision.RegionsToMask(mask, viewer.Regions, false, 255);
viewer.Regions.Item(1).Visible = false;

 

vision.Histogram2(viewer.Image, report, options, mask);

 

I dont mine looping through it, I just want to find a way to avoid calling the Regions.RemoveAll() and AddRotatedRectangle() functions

 

Ive re-read the help file again and it says that .... This method operates on all region objects whose Active property is True.

Do you think if I just set the viewer.Regions.Item(1).Active = true to the region I currently want to analyse.....

I could finally get rid of the functions I didnt want to use so the code would look like below

 

//Set all regions to false first............ and enable as needed

 

for(int i = 0; i<topCoverRegions.Count; i++)
{

       viewer.Regions.Item(i).Active = true;

       vision.RegionsToMask(mask, viewer.Regions, false, 255);

      

       // Do histogram analysis on the mask...     

}

 

 

 

Thanks for your time

Riscoh

 

 

 

0 Kudos
Message 4 of 10
(4,768 Views)
Refactoring my code allow the program to run 40% to 60% faster
0 Kudos
Message 5 of 10
(4,753 Views)

Hi riscoh,

Thanks for the explanation.   I would suggest just trying the idea you have.  It does not seem like it will take much time to implement.  Other than this, I cannot think of a way to make things work faster on my end.  When you said refactoring my code aloud you cod run fast are you saying that you have already tried this?

Adam H
National Instruments
Applications Engineer
0 Kudos
Message 6 of 10
(4,735 Views)

Adam,

 

Yes I've tried it and the program ran faster...

 

Thanks

Riscoh

0 Kudos
Message 7 of 10
(4,733 Views)

Hi riscoh,

Just out of curiosity what exactly did you change to make it run that much faster?  It would be useful to know for future reference for both myself and other people on the forums.

Adam H
National Instruments
Applications Engineer
0 Kudos
Message 8 of 10
(4,721 Views)

Adam,

 

I basically got rid of these...

 labelRectangle = new CWIMAQRotatedRectangle();

..................

..................

viewer.Regions.RemoveAll();
viewer.Regions.AddRotatedRectangle(
labelRectangle );

 

The previous code I had keeps on creating CWIMAQRotatedRectangle object and add them to the viewer's region collection and this is where it consumes a lot of time.

 

And I have to call Regions.RemoveAll() coz there is no RegionToMask() function...

 

Instead of removing and adding 72 regions I just made all 72 regions inactive and set a region active when I need to do analysis....

 

Heres what the program does

- Check fiducial using pattern matching

- Reposition 72 regions based on the fiducial pattern match report.. I use Region.Item(1).Move(x,y) method to reposition all 72 regions... I didnt like using TransformRegions method its confusing me, just want to make it simple and offset the center position from the fiducial matchReport

- Mask regions one at a time and call Histogram2 method

- show results

 

heres how i initialize the my label tools

while (++count < points)
            {
                thetaX = Math.Cos(Math.PI * angle / 180);
                thetaY = Math.Sin(Math.PI * angle / 180);

                labelRectangle = new CWIMAQRotatedRectangle();

                x = thetaX * labelTool.getRadius() + labelTool.getCenterX();
                y = thetaY * labelTool.getRadius() + labelTool.getCenterY();

                labelRectangle.Initialize((float)x, (float)y, labelTool.getWidth(), labelTool.getHeight(), (float)(360 - angle));

                form.getViewer().Regions.AddRotatedRectangle(labelRectangle);
                form.getViewer().Regions.Item(form.getViewer().Regions.Count).Active = false;
                form.getViewer().Regions.Item(form.getViewer().Regions.Count).Visible = false;
                form.getViewer().Regions.Item(form.getViewer().Regions.Count).PenColor = (uint)ColorTranslator.ToOle(Color.Magenta);

                angle += angleIncrement;
            }

 

And to inspect a region....

viewer.Regions.Item(regionIndex).Active = true;      
vision.RegionsToMask(mask, viewer.Regions, false, 255);
vision.Histogram2(viewer.Image, report, options, mask);

viewer.Regions.Item(regionIndex).Active = true;

 

Hope this helps........

 

Thanks

Cliff

0 Kudos
Message 9 of 10
(4,713 Views)

Hi riscoh,

Thanks for the info.  I am sure it will benefit others in the future.

Adam H
National Instruments
Applications Engineer
0 Kudos
Message 10 of 10
(4,688 Views)