Prev Next

Pattern search brief introduction

  • how to search for patterns in image using CogPMAlignTool
  • how to use basic settings of CogPMAlingTool

Now let’s try some direct image processing.
So far, we’ve talked about the added functionality offered by Designer, but we haven’t used any VisionPro tools yet, at least not directly.
So, let’s do a simple analysis of the image.
We can try to do such a thing that we search for this text ABC123 for example. To do this, we will use a tool that searches for patterns. So, I put a VisionPro ToolBlock in my Task.
It expects some VisionPro Image as an input.
So, I can directly connect the image from the camera here.
Alternatively, I could use the Tag we’ve created here “Cam1Image”, then I wouldn’t have that connection.
If possible, I recommend creating these connections directly so that we have a visual intuition of where the flow of information goes.
Where is it coming from and where is it going.

And in order to work with that image in the VisionPro ToolBlock, we need to initialize it somehow, so I will now hit Run All.

But before that I should switch to the folder with the images.

You will have these images available in the project or on the page next to this video.

So SimCam

I’ll insert these images and initialize again.

OK. I have this image and I will be able to use it in the VisionPro ToolBlock now. So, I double-click on the VisionPro ToolBlock and as I said I’m going to look for a pattern. So, I will use Cognex CogPMAlignTool, which has PatMax, PatQuick, PatFlex algorithms inside…

And now to insert an image into that tool, I have to link that input image with this input image of the CogPMAlignTool tool. I double-click on this tool and it opens up the options of this tool.

In order to train a pattern, I first have to pull a training image into that tool.

Here I can see what views this tool contains.

First of all, there is Current.InputImage. That’s the input image that enters the tool.

Then there is Current.TrainImage, but it is empty now because we haven’t click Grab Train Image button yet.

And then there’s LastRun.InputImage, that’s the one with the output graphics, and CompositeDataItemImage, that may help finetuning of that tool.

We need to initialize this Current.TrainImage, so when I click Grab Train Image, it copies the image from Current.InputImage and pastes it into Current.TrainImage.

This Current.TrainImage there will always be the same,

even if I close the project, even when there will be different images from camera, unless I click the Grab Train Image again.

At that time, it would copy the image from Current.InputImage to TrainImage again and it would be changed.

So, this TrainImage accompanies the project all the time.

It is therefore also reflected in the size of the project.

And I would like my training pattern to be like this.

So, I take that region at TrainImage and moved it to that area.

The area can be different. When I switch to tab called Train Region & Origin, I can choose different shapes, for example, circle, ellipse or polygon. I can define it using different coordinates, but the default shape is CogRectangleAffine. I will return to TrainParams now.

I see that the algorithm here is PatMax and PatQuick, that’s basically and it’s probably the most used combination.

So now we can click the Train button.

And I see that it has trained the pattern.

It writes, here on the bottom left, that it’s Trained.

And I see that the beginning of the coordinate system XY is still here, at the top left corner. That means, if it would find that pattern exactly here, the beginning of the coordinate system for that found pattern would be [0,0] here.

But I want some more meaningful output. So when the pattern will rotate, it will move in the image, I want it not to show the result position in the distance from the pattern, but to show, for example, the center of that C or let’s say that somewhere here on the corner of the B. So I go to Train Region & Origin and use Center Origin, for example, but I can also move it manually. For example, I’ll put it here,

to always have a stable position, where the B is, so this will be the beginning of the new coordinate system, for a new image I will explain later.

And it should work without any other settings. When I press the RUN of this tool, I see at the bottom left that it ran in green, that is, there was no error and when I look at LastRun.InputImage I see that it found that pattern and here it determined its origin point.

What I’d like to do now is, that if this CogPMAlingTool1 finds any pattern in the image, we can see it in Runtime mode or on a page with a visualization in the form of an ellipse or a circle. Let’s say a circle,

which will be 40 x 40 in size. We will put Label next to it. We will enlarge the text a little so that it is better visible to font size 20

and change that text to Pattern found.

That is, the pattern found. Colon.

And this circle will be green or red, depending on whether or not it found that pattern.

But for that, we need to set up some global variable to link the color of that circle with what this CogPMAlingTool1 gives us. CogPMAlingTool does not have an output here, whether or not it found that pattern, so we have to add it explicitly. Right click on that CogPMAlingTool1 we choose Add Terminals

Click Results and here we have Count. We see that the tool found one pattern. We click on Add Output button and we can see the Results.Count appeared and we now pull this out of this Toolblock to see it in the Task. So, I just dragged Count into this Output, like this, and

In that MainTask I now see that I have added this output connector from the VisionProToolBlock called ResultsCount and it is of Integer type.

I’m going to create a new variable using Assign To New Tag here, and I’ll write PatternCount there, for example.

Or, FoundPatternsCount.

That is, the number of patterns found.

And when I click Run All, I should initialize the Tag. But I see that he initialized it to 0 now. Ok, we will play later with this.

Let’s go back to Page

and we will now map that tag to a background color. So, Fill Color will not be Transparent anymore but it will be tied using these arrows to the tag FoundPatternsCount. We can also access the tag through the tag selector.

It shows a message.

It writes that Type doesn’t match and it’s asking whether we really want to add it.

Yes, we want to add.

And then it shows in here that it is missing association.

That’s because it doesn’t know how to determine a color by a number that is of type integer, a simple number 0,1,2,3, how to find out the color accordingly.

So, that’s why we have Associations here.

I can say that if there is 0, if the number is 0, then let’s put the color red, I will add this association. And if the number is 1, I will add green, for example. But what if it finds 3, well, then I had to add like, okay, let’s give 3 let’s give the green light and it could be done this way.

But let’s do it differently, we can reduce this result, so I’ll give Cancel. Reduce this result to only Boolean type, so TRUE or FALSE values. False will be when it finds 0 patterns and TRUE when it will find more than 0 patterns. So, we give if condition and if this number is greater than 0,

the result will be TRUE, otherwise the result will be FALSE.

Therefore, if this condition is not met, it will be FALSE and if it is met, it will be TRUE.

This is the notation of if statement in Expression Builder.

And now we still have the Missing association waring because it can’t tell color from TRUE or FALSE.

So again, we need to create an association and when it is TRUE,

we give the green light.

And when it’s false, we’ll put red.

We have this association true green, false red.

Accept.

I’ll save the project just in case if something failed.

I give a run and see that Pattern not found.

And why?

There is a red color, that is, something did not work correctly as we expected or it did not find the shape. So, I go to MainTask VisionProToolBlock and I see that the Count is 0. That means this PMAlignTool1 did not find that shape. Either it’s such a weak tool, or we misconfigured it. I think it’s the second option. And we have to look

to Run Params, because we didn’t change these.

There is an Accept threshold of 0.5, that’s fine, but the angle of the pattern changed and we have an angle of nominal value 0, that is, the tool does not expect the angle to change and we should change this setting.

So, for example, we switch it to

interval between minus 45° and 45°. Be careful that this is only indicative value for the instrument to know that it should expect some angle in some soft range. For example, if we put an angle of minus 5 and 5°, it could happen that it also finds a pattern that is deflected minus 12° or plus 20°.

It filters out bigger deviances.

Thus, if we put 5, it will no longer find about 50° angle there, but it must be taken into account that this is just soft threshold, that this is not a strict value of 5.

So, let’s say it will be ±45° now. Or actually the part can spin around, so we put 180°.

We don’t expect the scale of that pattern to change now.

So, I’m just going to try to run it like that.

And we look at the output LastRun.InputImage and we see that it has found that shape.

And in the results on the leaderboard, we see that it found him with a high score. It found it in this position with such an angle of -7.95 and there is actually a whole listing of those results. Now that we look at Run Mode,

we can still see that it is red there, and that’s because that COUNT value has not yet been transferred to the… I’ll show it in that Task.

The count value here is 1, but it has not yet been stored in this variable because this Task did not run or this one block did not run yet. So, either I can run this one block as Run Selected or I just run the whole Task and I see that it is already showing correct result. It simply finds that pattern here. And I can also show,

if I shrunk that angle in Run Params, for example, to those plus or minus 20° and I will run the project, I can see that when I have a larger deviation, it will not take it anymore, but with less deviation it will find the pattern. So, this is working.

As a basis for searching of patterns, this is enough for now. We will get back to CogPMAlingTool and explain in detail later in other videos. I will save the project and see you in next video.

Leave a Comment