0

i am working on a project related to template matching image processing , i have done the matching algorithm but the problem i am facing that , template matcher always yeilds the best co-relation matched in source image of template image but i want to notify or respond only when the desired output comes neither on false output. i want to serially communicate MATLAB code with arduino board UNO R3 which will generate the pulse birectionaly when the output comes ? so what should i supposed to do ? this is the code :

cam=videoinput('winvideo',2,'YUY2_320x240');


start(cam);
preview(cam);
set(cam,'ReturnedColorSpace','RGB');
get=input('get frame ???');

frame=getsnapshot(cam);
imwrite(frame,'got.jpg');

I=imread('D:\Template matcher\got.jpg');
H_Eq=vision.HistogramEqualizer;
Temlate_matcher=vision.TemplateMatcher;
Temlate_matcher.Metric='Maximum absolute difference';
Temlate_matcher.OutputValue='Metric matrix';
marker_inserter=vision.MarkerInserter('Size',30,'Fill',false,'FillColor','White','Opacity',0.75);
I=rgb2gray(I);
I=step(H_Eq,I);
Template1=imread('D:\Template matcher\ge.jpg');


Template1=rgb2gray(Template1);


H_Eq=vision.HistogramEqualizer;
Template1=step(H_Eq,Template1);


Location1=step(Temlate_matcher,I,Template1);

 marker_inserter.Shape='Square'
 output_image=step(marker_inserter,I,Location1);

figure();imshow(output_image);
user3703949
  • 1
  • 1
  • 3
  • 1
    You can always set a threshold, something like if the best score is 80% or more. If the score of your template match is higher than this threshold, then this should be very indicative of a good match and you can signal your Arduino board that you have found something worthwhile. – rayryeng Jun 03 '14 at 16:49
  • need some more explanation as i am unfamiliar with threshold – user3703949 Jun 03 '14 at 17:19
  • You stated in your question that when you do template matching, the output will be the correlation with the **best possible score**. Sometimes, the best possible score could be very poor... something like 30%. This means that even with this best score, it is very **unlikely** that you have found the object you are looking for. That's why it's a good idea to enforce a **threshold**. When you find the best score, if that score is **higher than some threshold** (say 80%), then this may indicate that the object is truly in the image. – rayryeng Jun 03 '14 at 17:22
  • As such, use your correlation score, and check to see if it's above some threshold. If it is, then the object is there (hopefully), and if it isn't, then it shouldn't be (hopefully!). – rayryeng Jun 03 '14 at 17:22
  • that is quite understandable to me but i use built in fucntions like template matcher , histrogram equalizer and processed the images in grayscale then how i could set threshold over there ? in MATLAB 2012 – user3703949 Jun 03 '14 at 17:39
  • All you have to do is add an additional `if` statement that checks to see if the template matcher produces a threshold that is above your set value. If it is, then you issue your command to the Arduino. You leave the hardware board untouched. Do all of this completely in MATLAB. – rayryeng Jun 03 '14 at 17:42
  • that was my code :::: – user3703949 Jun 03 '14 at 17:52
  • cam=videoinput('winvideo',2,'YUY2_320x240'); start(cam); preview(cam); set(cam,'ReturnedColorSpace','RGB'); get=input('get frame ???'); frame=getsnapshot(cam); imwrite(frame,'got.jpg'); I=imread('D:\Template matcher\got.jpg'); H_Eq=vision.HistogramEqualizer; Temlate_matcher=vision.TemplateMatcher; Temlate_matcher.Metric='Maximum absolute difference'; – user3703949 Jun 03 '14 at 17:52
  • Temlate_matcher.OutputValue='Metric matrix'; marker_inserter=vision.MarkerInserter('Size',30,'Fill',false,'FillColor','White','Opacity',0.75); I=rgb2gray(I); I=step(H_Eq,I); Template1=imread('D:\Template matcher\ge.jpg'); Template1=rgb2gray(Template1); H_Eq=vision.HistogramEqualizer; Template1=step(H_Eq,Template1); Location1=step(Temlate_matcher,I,Template1); % marker_inserter.Shape='Square' % output_image=step(marker_inserter,I,Location1); %figure();imshow(output_image); – user3703949 Jun 03 '14 at 17:53
  • Please edit your post with this code. Code that is more than two lines in comments looks like garbage – rayryeng Jun 03 '14 at 17:56
  • i had updated the post with code – user3703949 Jun 03 '14 at 18:16
  • I'll create an answer for you – rayryeng Jun 03 '14 at 20:07

1 Answers1

0

As we have discussed in our comments, template matching using the Computer Vision toolbox will produce the best template match in the frame. However, this does not necessarily mean that the object you are searching for is located within the location of where the template best matched.

As such, what I would recommend you do is take a look at what the metric gives you for that template. In your case, you're using the maximum absolute difference. If this metric is less than some threshold, then that could mean that the template was found in the frame you are examining. If it's greater, then there is a good possibility that it wasn't. This threshold you'll have to play around with as it totally dependent on what the template looks like and the contents of the frame you're trying to look at. If it is less than some threshold, then you can send your signal to the Arduino board. Before we do this, you must change your Template Matcher setup so it looks like this:

Temlate_matcher=vision.TemplateMatcher('OutputValue', ...
                'Best match location', 'BestMatchNeighborhoodOutputPort', true);

This will allow us to get what we eventually want. As such, you can't use Metric Matrix as the OutputValue field anymore so get rid of this line in your code. Now that this is set up, you can override the behaviour of the template matching by replacing this code:

Location1=step(Temlate_matcher,I,Template1);

With this:

[Location1, NVALS, NVALID] = step(Temlate_matcher,I,Template1);

NVALID returns true if the match is fully contained within the frame and false otherwise. For your case, NVALID should always be true as the metric you have chosen is guaranteed to do matching as long as the template is contained within the frame. NVALS is the matrix of metric values that best matched the template image. In essence, the centre of this matrix gives you the best metric value that the matcher produced, and so this is the value that you want. You can cheat and just find what the minimum value is by:

val = min(NVALS(:));

As such, after this point you can check to see if val is less than some threshold. This I don't know what it would be. I guess something to play around with would be that if the best match was off by... say 5, then there may be something interesting to take a look at in the frame. As such, set thresh = 5.

Therefore, if val is less than thresh, then go ahead and signal your Arduino board.

Good luck!

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • Error using vision.TemplateMatcher/step Too many output arguments; must be between 1 and 1 (3 requested). Error in TM (line 32) [LOC, NVALS, NVALID] = step(Temlate_matcher,I,Template1); That was the error i m facing – user3703949 Jun 04 '14 at 13:43
  • Shouldn't be happening. I got that straight from the documentation. Type in `help vision.TemplateMatcher` and you'll see – rayryeng Jun 04 '14 at 13:47
  • yeah i checked it out in help . the syntax is same but it declaring an error of output arguments are 3 that should be 1 and 1 – user3703949 Jun 04 '14 at 13:53
  • @user3703949 Just read the documentation again. You need to set up your Template Matcher differently. Check my edit. – rayryeng Jun 04 '14 at 14:50
  • @user3703949 : I also edited how to compute the thresholds. Check that out too. – rayryeng Jun 04 '14 at 15:05
  • can you tell me that about my program. i did my template matching using vision.templateMatcher i want to update my code by replacing LOC=vision.templateMatcher(template_matcher,I,template); with this [LOC,NVALS,NVALID]=vision.templateMatcher(template_matcher,I,template); I am facing the error of too many output arguments. i also use this code with template_matcher=vision.templateMatcher('OutputValue','Best Matched Location' , 'bestMatchNeighbourhoodValues',true); still i am facing the same error in same line ? what should i supposed to do ? – user3703949 Jun 19 '14 at 17:30