Opencv study NOTE 2: Use opencv for Image Comparison

Source: Internet
Author: User

Comparing the two images is similar, this is widely used. At the beginning, I thought this technology was very advanced (of course it was also very advanced) and I had to learn a lot of related knowledge to get started, at least you must have a certain understanding of all aspects before you can do this research.

However, opencv provides a set of APIS for this comparison, so that we can easily compare two images. This is the comparison of histograms. histogram is used in English for histograms, the principle is to convert the image into a histogram, and then compare the histogram. To some extent, the similarity of the image is actually reflected. The code below is as follows:

# Include "opencv2/highgui. HPP "# include" opencv/cv. HPP "// use int histogrambins = 256 to draw a histogram; float histogramrange1 [2] = {0,255}; float * histogramrange [1] = {& histogramrange1 [0]}; /** imagefile1: * imagefile2: * method: cocould be cv_comp_chisqr, cosine, cv_comp_correl, cv_comp_intersect */INT comparehist (const char * imagefile1, const char * imagefile2) {iplimage * image1 = cvloadimage (imagefile1, 0); iplimage * image2 = cvloadimage (imagefile2, 0); cvhistogram * histogram1 = cvcreatehist (1, & values, dimensions, histogramrange ); cvhistogram * histogram2 = cvcreatehist (1, & amp; #, metrics, histogramrange); cvcalchist (& image1, histogram1); cvcalchist (& image2, histogram2); histogram (histogram1, 1 ); cvnormalizehist (histogram2, 1); // cv_comp_chisqr, cv_comp_bhattacharyya can be used for histogram comparison. The smaller the value, the more similar the image is to printf ("cv_comp_chisqr: %. 4f \ n ", cvcomparehist (histogram1, histogram2, cv_comp_chisqr); printf (" cv_comp_bhattacharyya: %. 4f \ n ", cvcomparehist (histogram1, histogram2, histogram); // compare the cv_comp_correl and cv_comp_intersect histograms. A larger value indicates that the image is more similar to printf (" cv_comp_correl: %. 4f \ n ", cvcomparehist (histogram1, histogram2, cv_comp_correl); printf (" cv_comp_intersect: %. 4f \ n ", cvcomparehist (histogram1, histogram2, latency); cvreleaseimage (& image1); cvreleaseimage (& image2); histogram (& histogram1); cvreleasehist (& histogram2 ); return 0;} int main (INT argc, char * argv []) {comparehist (argv [1], argv [2]); // comparehist ("D: \ camera.jpg "," d: \ camera1.jpg "); Return 0 ;}

Now I use two photos taken by camera to compare them. camera.jpgand camera1.jpg

The running result of these two graphs is:

Cv_comp_chisqr: 0.2902

Cv_comp_bhattacharyya: 0.1707

Cv_comp_correl: 0.8017

Cv_comp_intersect: 0.8070

If you use the same image to compare the running results, it should be

Cv_comp_chisqr: 0.0000

Cv_comp_bhattacharyya: 0.0000

Cv_comp_correl: 1.0000

Cv_comp_intersect: 1.0000

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.