0

I am new to OPENCV, I am trying to compare two images so I used absdiff function on two Mat images but this comparison is easily affected by the lighting and luminescence, Could you please advice me something more robust ?

IndieTech Solutions
  • 2,429
  • 1
  • 18
  • 31
  • 1
    possible duplicate of [Simple and fast method to compare images for similarity](http://stackoverflow.com/questions/4196453/simple-and-fast-method-to-compare-images-for-similarity) – NathanOliver Apr 08 '15 at 13:19

1 Answers1

2

There are a number of methods to compare two images and the best method really depends on your desired accuracy and application.

One method that may work for you is to use histogram equalisation before taking the difference of the images. Histogram equalisation makes the intensity of the scene more uniform and for a lot of applications can minimise the effect of lighting on the scene. In OpenCV this can be done with the following function:

cv::equalizeHist(src, dst);

More complicated methods involve using feature detectors and then doing feature matching. A simple method OpenCV offers if FLANN. Essentially it identifies easily recognisable regions in the image (i.e. corners) and looks for the patterns in another image.

You can use the features that are matched in the two images to calculate the transformations between the two images such as rotation, scale, skew etc. The presence of the features and the distance they've moved between the two images is what you then use for your similarity score.

It is a more involved method and it's fully documented in the OpenCV docs, if you'd like I can go into more depth here.

Docs: http://docs.opencv.org/doc/tutorials/features2d/feature_flann_matcher/feature_flann_matcher.html#feature-flann-matcher

user1646196
  • 589
  • 6
  • 28
  • Thank you for replying to the question, I tried using both method they are effective but as I am calculating the pixel values of output Matrix and trying to get a mathematical value of it, I think it is needs more stability.. I was reading about Normalized cross correlation(NCC) which maybe better in producing a stable result than absDiff .. Is there any function to compare two same size Matrices by NCC ? – Abhishek Jandial Apr 14 '15 at 14:30
  • Refer to this question for a code example http://stackoverflow.com/questions/11321316/how-to-use-opencvs-normalized-correlation :) – user1646196 Apr 14 '15 at 14:57
  • This function matchTemplate offer only to compare the template or part of the main image.. I dont think it is suitable for comparing two different images to get a resultant image.. – Abhishek Jandial Apr 15 '15 at 07:50
  • Have you tried feature matching like I mentioned in my answer? Also transfering the image into HSV and doing absdiff on something like the Hue channel may give more stable results as it's more lighting invariant. You can also refer to this existing question for other ideas http://stackoverflow.com/questions/11541154/checking-images-for-similarity-with-opencv – user1646196 Apr 15 '15 at 09:57
  • Sir, I have been working on the advice you gave me.. I tried Histogram, equalizeHist() it was not very effective as i think I am using gray-scale images for comparing, maybe because of that. And Hue is very effective approach for human skin and dark objects but in my case I was comparing more of physical environments, so that causes lot of noise and improper results. I am still searching how to deal with light conditions like light changes in morning and evenings so if I am comparing same images in taken in different times, it becomes different. Can you please help? thank you! – Abhishek Jandial Apr 23 '15 at 08:28
  • Can you provide example images? And comparing hue is generally very good when working with variances in lighting etc. This question (http://stackoverflow.com/questions/1500498/how-to-use-sift-algorithm-to-compute-how-similiar-two-images-are) has someone comparing two images by comparing feature descriptors like I recommended and contains code and an explanation. If you don't get anywhere with that ask a question again and describe these approaches you've tried you should get more help – user1646196 Apr 23 '15 at 11:50