Ask Your Question
4

A single metric for how similar two images are

asked 2012-08-19 11:07:59 -0600

greenlines gravatar image

updated 2012-08-20 13:29:35 -0600

sammy gravatar image

I would like to run an OpenCV program that will print out a single number that indicates how similar two images are. How do I do that?

From what I can tell, after running various algorithms, the matching computed by an algorithm can be drawn on the screen and there are green lines between the first image and the second.

Is there a way to print the number of green lines? Is there a better way?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
5

answered 2012-08-20 02:01:43 -0600

sammy gravatar image

Note: this is a repost of one of my answers on SO.


This is a really huge topic, with answers from 3 lines of code to entire research magazines.

I will outline the most common such techniques and their results.

  • Comparing histograms. One of the simplest & fastest methods. Proposed decades ago as a means to find picture simmilarities. The idea is that a forest will have a lot of green, and a human face a lot of pink, or whatever. So, if you compare two pictures with forests, you'll get some simmilarity between histograms, because you have a lot of green in both.

    Downside: it is too simplistic. What is the difference between a banana and a beach? both are yellow.

    OpenCV method: compareHist()

  • Template matching : http://stackoverflow.com/questions/8520882/matchtemplate-finding-good-match. It convolutes the search image with the one being search into. It is usually used to find smaller image parts in a bigger one.

    Downsides: It only returns good results with identical images, same size & orientation.
    OpenCV method: matchTemplate()

  • Feature matching. Considered one of the most efficient ways to do image search. A number of features are extracted from an image, in a way that guarantees the same features will be recognized again even it is rotated/scaled/skewed. The features extracted this way can be matched against other image feature sets. Another image that has a high proportion of the features in the first one is most probably depicting the same object/scene.

    There are a number of OpenCV tutorials/samples on this, and a nice video here. A whole OpenCV module (features2d) is dedicated to it.
    Downsides: It may be slow. It is not perfect.

And here is a really great answer on this topic on SO.

edit flag offensive delete link more

Comments

As a curiosity, here is a more recent video about opencv's feature matching (its Python code is within opencv samples I think): http://youtu.be/pzVbhxx6aog

Rui Marques gravatar imageRui Marques ( 2012-08-21 05:59:56 -0600 )edit
0

answered 2012-08-20 01:13:28 -0600

mrgloom gravatar image

updated 2012-08-21 01:16:45 -0600

You can use histogram comparision http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_comparison/histogram_comparison.html

here is the code in python:

import cv

def compute_histogram(src, h_bins = 30, s_bins = 32):
    #create images
    hsv = cv.CreateImage(cv.GetSize(src), 8, 3)
    hplane = cv.CreateImage(cv.GetSize(src), 8, 1)
    splane = cv.CreateImage(cv.GetSize(src), 8, 1)
    vplane = cv.CreateImage(cv.GetSize(src), 8, 1)

    planes = [hplane, splane]
    cv.CvtColor(src, hsv, cv.CV_BGR2HSV)
    cv.Split(hsv, hplane, splane, vplane, None)

    #compute histogram  (why not use v_plane?)
    hist = cv.CreateHist((h_bins, s_bins), cv.CV_HIST_ARRAY,
            ranges = ((0, 180),(0, 255)), uniform = True)
    cv.CalcHist(planes, hist)      #compute histogram
    cv.NormalizeHist(hist, 1.0)    #normalize hist

    return hist

src1 = cv.LoadImage("C:/ICP/buterfly_0.jpg", cv.CV_LOAD_IMAGE_COLOR)
src2 = cv.LoadImage("C:/ICP/buterfly_1.jpg", cv.CV_LOAD_IMAGE_COLOR)
hist1= compute_histogram(src1)
hist2= compute_histogram(src2)
sc= cv.CompareHist(hist1, hist2, cv.CV_COMP_BHATTACHARYYA)
print sc
edit flag offensive delete link more

Comments

And for feature extraction?

greenlines gravatar imagegreenlines ( 2012-08-22 19:09:17 -0600 )edit

I'm not familar with python interface for features yet, but I can give you c++ code.It computes SURF features of 2 images and than return number of matches, you can use it as metric(but I'm not sure that it's good metric).For example for initial image and fisheye distorted it give ~2300 matches, for 48 angle rotation ~900, for resize to 30%(with jpg artifacts) 600. something more about SIFT for CBIR http://www.theopavlidis.com/technology/CBIR/PaperE/AnSIFT1.htmhttp://www.theopavlidis.com/technology/CBIR/PaperD/testset.htm

mrgloom gravatar imagemrgloom ( 2012-08-23 02:16:24 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2012-08-19 11:07:59 -0600

Seen: 11,582 times

Last updated: Aug 21 '12