Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

backround worker

Solved!
Go to solution

I am programming in VS2008 C#.

 

I create a template by drawing a RIO within an image on a viwer and extracting it. I then display that image in one viewer and snap a new picture and place that image in another viewer. I click a button and it calls a method:  Search() which finds the pattern and draws a border around it. Here is the Search code.

 

public int Search()

{

CWIMAQMatchPatternOptions MatchOptions = new CWIMAQMatchPatternOptions();

CWIMAQPatternMatchReport MatchReport = new CWIMAQPatternMatchReport();

CWIMAQRectangle SearchRectangle = new CWIMAQRectangle();

CWIMAQPoint FoundPoint = new CWIMAQPoint();

CWIMAQImage Template = axCWIMAQViewer6.Image;

CWIMAQImage Picture = axCWIMAQViewer7.Image;

int Result = 0;

 

MatchOptions.MatchMode = CWIMAQMatchModes.cwimaqMatchRotationInvariant;

MatchOptions.MinimumMatchScore = 450;

MatchOptions.NumMatchesRequested = 1;

MatchOptions.MinimumContrast = 30;

MatchOptions.SubPixelAccuracy =
false;

 

SearchRectangle.Left = 1F;

SearchRectangle.Top = 1F;

SearchRectangle.Width = 599F; // (float)axCWIMAQViewer7.Image.Width;

SearchRectangle.Height = 469F; // (float)axCWIMAQViewer7.Image.Height;

  

try

{

//Result = this.axCWIMAQVision1.MatchPattern2(Picture, Template, MatchOptions, MatchReport, SearchRectangle);

//Result = this.axCWIMAQVision1.MatchPattern2(this.axCWIMAQViewer7.Image, this.axCWIMAQViewer6.Image, MatchOptions, MatchReport, SearchRectangle);

 this.axCWIMAQVision1.Invoke((MethodInvoker)delegate { axCWIMAQVision1.MatchPattern2(this.axCWIMAQViewer7.Image, this.axCWIMAQViewer6.Image, MatchOptions, MatchReport, SearchRectangle); });

}

catch

{

MessageBox.Show("No image or template.", "Error");

Vision.FoundPoint.X = -1;

return -1;

}

if (MatchReport.Count != 1)

{

MessageBox.Show("Template pattern not found.", "Error");

Vision.FoundPoint.X = 0;

return -1;

}

//Display results

axCWIMAQViewer1.Image.Overlays.Item(1).Clear();

for (int I = 0; I < MatchReport.Count; I++)

{

Result = axCWMachineVision1.DrawPatternMatch(axCWIMAQViewer1.Image.Overlays.Item(1), MatchReport.Item(I + 1), System.Drawing.
ColorTranslator.ToOle(System.Drawing.Color.Red));

//CheckCameraError(w, "DrawPatternMatch");

}

axCWMachineVision1.DrawSquarePoint(axCWIMAQViewer1.Image.Overlays.Item(1), FoundPoint, 5,
CWIMAQOverlayModes.cwimaqOverlayModePaint, System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green)); return 0;

}

 

Well, this works great when I call the method by clicking the button but now I need to have it done automatically during processing so I put it in a background worker

 

private void FindGlass_DoWork(object sender, DoWorkEventArgs e)

{

 

while (!FindGlass.CancellationPending)

{

switch (Step)

{

0 Kudos
Message 1 of 3
(3,509 Views)

I hit a wrong button and this this subbitted before I was through, Let me finish here. 

 

private
void FindGlass_DoWork(object sender, DoWorkEventArgs e)

{

int Step = 10, Result = 0;

 

while (!FindGlass.CancellationPending)

{

switch (Step)

{

      // do things here

 

     case 100: 

              btnSearch.Invoke((MethodInvoker)delegate { btnSearch.PerformClick(); });

              Step = 110;

              break;

 

 

This fails to find the pattern every time. I can alternate between clicking btnSearch on the form and having the thread do it and the click passes every time and the thread fails every time.

 

Note, I called the Search() method directly in the background thread but that failed the try/catch so I went to performclick instead. 

0 Kudos
Message 2 of 3
(3,502 Views)
Solution
Accepted by topic author bob13873
I knew as soon as I posted this I would figure this out. The picture is being snapped in the main thread. The Search is also on the main thread. When I click the buttons to snap and search the search cannot happen until the snap is finished and the image has been attached to the viewer. By calling these even indirectly from an async background worker it seems the search can be called while the image is still being aquired. Nothing there, nothing found.
0 Kudos
Message 3 of 3
(3,482 Views)