OPENCV implementation of histogram method for image similarity calculation

Source: Internet
Author: User
Tags float range ranges

Operation Steps:

1. Load images (grayscale or color) and make them consistent in size;

2. If the color map, improve the line color space transformation, from RGB to HSV, if the gray-scale map does not need to transform;

3. If the histogram is calculated directly, and the histogram is normalized;

4. If the color graph, the color histogram is computed, and the color histogram normalization;

5. Use similarity formulas, such as correlation coefficients, chi-square, intersections, or babbitt distances, to calculate the similarity value.

String strsrcimagename = "Src.jpg"; Cv::mat matsrc, matSrc1, matsrc2;matsrc = Cv::imread (Strsrcimagename, Cv_load_image_ unchanged); Cv::resize (MATSRC, MatSrc1, Cv::size (357, 419), 0, 0, cv::inter_nearest); Cv::resize (MATSRC, MATSRC2, CV:: Size (2177, 3233), 0, 0, Cv::inter_lanczos4), Cv::mat matDst1, matdst2;cv::size sizeimage = cv::size (500, 500); Cv::resize (MatSrc1, MatDst1, sizeimage, 0, 0, cv::inter_cubic);//cv::flip (MatDst1, MatDst1, 1); Cv::resize (MATSRC2, MatDst2, sizeimage, 0, 0, cv::inter_cubic); if (matsrc.channels () = = 1) {int histsize = 256;float range[] = {0, 256};const float* Histrange = {Range};bool uniform = true;bool accumulate = False;cv::mat Hist1, hist2;cv::calchist (&matDst1, 1, 0, Cv::mat (), Hist1, 1, &histsize, &histrange, uniform, accumulate); Cv::normalize (Hist1, Hist1, 0, 1, cv::norm_mi NMAX,-1, Cv::mat ()); Cv::calchist (&matdst2, 1, 0, Cv::mat (), Hist2, 1, &histsize, &histrange, uniform, Accumu Late); Cv::normalize (Hist2, Hist2, 0, 1, Cv::norm_minmaX,-1, Cv::mat ());d ouble dsimilarity = cv::comparehist (Hist1, Hist2, Cv_comp_correl);//,cv_comp_chisqr,cv_comp_ intersect,cv_comp_bhattacharyyacout<< "similarity =" &LT;&LT;DSIMILARITY&LT;&LT;ENDL;} else {Cv::cvtcolor (MatDst1, MatDst1, CV::COLOR_BGR2HSV); Cv::cvtcolor (MatDst2, MatDst2, CV::COLOR_BGR2HSV); int H_bins = S_bins = 60;int histsize[] = {h_bins, s_bins};float h_ranges[] = {0, 180};float s_ranges[] = {0, 256};const float* r Anges[] = {h_ranges, s_ranges};int channels[] = {0, 1};cv::matnd hist1, Hist2;cv::calchist (&matdst1, 1, channels, CV:: Mat (), Hist1, 2, histsize, ranges, true, false) Cv::normalize (Hist1, Hist1, 0, 1, Cv::norm_minmax,-1, Cv::mat ()); Cv::calc Hist (&matdst2, 1, channels, Cv::mat (), Hist2, 2, histsize, ranges, true, false), Cv::normalize (Hist2, Hist2, 0, 1, CV:: Norm_minmax,-1, Cv::mat ());d ouble dsimilarity = cv::comparehist (Hist1, Hist2, Cv_comp_correl);cout<< " similarity = "&LT;&LT;DSIMILARITY&LT;&LT;ENDL;}


OPENCV implementation of histogram method for image similarity calculation

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.