MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Is there any robust solution to match two images and find the percentage of match between them in MATLAB ?

Asked by meghna bhatt on 15 Jul 2013

Problem description -

->say we already have some no of template(base) images in one folder

Input -

`    - Image to find its perfect matching image`

Output -

`    -Matched Image (if found)`
`    -Otherwise Message - no matching found `

I need robust solution to this ..

I have tried using SURF Features but its not giving me robust output meaning it sometimes gives wrong match - though it its giving quite good result but not 100 %

Here is the rough logic of my current algorithm :

` Input_Image_gray = rgb2gray(Input_Image);`
` Input_keys = detectSURFFeatures(Input_Image_gray);`
` [Input_features Input_validPts] = extractFeatures(Input_Image_gray,  Input_keys);`

----iterate for loop for the Template images we have in a Folder

` templateImage=templateImagesArray{loopCounter};`
` ref_img_gray = rgb2gray(templateImage);`
` ref_keys=detectSURFFeatures(ref_img_gray);`
` [~, MATCH_METRIC] = matchFeatures(ref_features,Input_features  ,'MatchThreshold',3);`

-- Calculate percentage for each image on the base of MATCH_MATRIC

` if percentage > 50`
`    display the matched Image`
`    otherwise display message -  no matching image fond`

----end loop

please share if any body has any idea regarding how to achieve this

## Products

Answer by Alex Taylor on 15 Jul 2013
Edited by Alex Taylor on 15 Jul 2013

When solving registration problems, there is typically not any one algorithm that will be robust 100% of the time. That said, you may find that you can achieve very good results with a particular dataset if you choose your algorithm appropriately.

As far as which algorithm to use, you should consider the following questions:

1) What types of geometric deformations do you expect are present in your data? 2) Do the images in your dataset have strong features in terms of Corners, SURF features, etc. 3) Are all the images in your dataset collected with a similar sensor? 4) Are all of the images in your dataset illuminated the same?

You have mentioned already that there is skew present in your images. The presence of shear means that you require an affine model to solve the registration.

You will not be able to use normxcorr2, because there is no known correlation based approach for resolving shear.

If you are getting good results with feature based approach using SURF and estimateGeometricTransform in CVST, I would see if you can tune the feature matching and RANSAC steps to get the kind of robustness you are looking for. SURF is an appropriate algorithm for solving affine registration problems.

meghna bhatt on 16 Jul 2013

I think your approach is good and I am already using SURF option

I also think SURF will be best suited for my requirements

answers of the questions you just specified are :

let me explain

1)Yes My input data will have its quality deformed and distorted some time as the medium is scanner to capture the form

2)Yes Images in Dataset has not that much strong features that are identical compare to others

3)No all the images in my dataset collected are either of Scanner or Camera Captured Images

4)Any extra illumination does not applied on the image but in case of camera captured image it will be there in different variation

Here only issue is result should be Accurate - means if not matched then algorithm should be able to tell that there is no match found rather giving the wrong matched image

meghna bhatt on 16 Jul 2013

Hey , My another problem is performance

When I am using SURF Method and matchFeatures it is taking huge time to give output

I found the two internal methods of MATLAB which are bottlenecks for slow performance through profiler

cvalgMatchFeatures>metricSSD repmat

why these are taking so much time?if any body knows please share...

Answer by Image Analyst on 15 Jul 2013

If you're searching for a "perfect matching image" simply subtract the two images:

```diffImage = double(image1) - double(image2);
perfectMatch = (sum(diffImage(:)) == 0);
```

You can use nnz if you want to find the percentage that aren't the very same pixel value. Only use SURF if you want to compare approximate matches, not exact matches.

meghna bhatt on 15 Jul 2013

Here perfect meaning accuracy of matched image - but the two images are not mirror image

Let me give an example say I have one Registration form which student needs to fill the details in ,

there are so many templates like this-

Here its template (core part) would be same but the filled details by every student will be different

so i need to find the template from that filled form by comparing it to existing all templates

say i have x image which we consider as a template Registration form scanned image

and y filled form image whose format is same but is scanned image and with some skew and minor difference

So in this case images will be varying with very minor difference and therefore,

perfectMatch = (sum(diffImage(:)) == 0); perfectMatch will not always be true means 1

but yes we can do something using sum(diffImage(:))

Image Analyst on 15 Jul 2013

Then it's not perfect, it's approximate like I said. You can use normxcorr2() if you want to find out where and if a smaller template image appears in a larger image. Or you can use SURF, like they use often in CBIR. Or look to other sophisticated cbir methods.