Similar image search principle 3 (color histogram-c ++), image search Histogram

Source: Internet
Author: User

Similar image search principle 3 (color histogram-c ++), image search Histogram

The color histogram of an image can be used for image retrieval. It can be used for image retrieval with the same color and with translation, scaling, and rotation immutability. Of course, these three features are not as stable as sift or surf, in addition, the biggest limitation is that if the shape content is the same, but the color is different, the results cannot be found. However, in some cases, it achieves better results.

Two ways to calculate the color histogram:

The color histogram of a color image, which can be processed in two ways, the effect should be similar.

The first method is to divide each channel of pixels. The maximum pixel value of each channel is 255, which can be 8, 16, or 64, so that the range of each channel is 0 ~ 15 (take the 16-level score as an example. Of course, the smaller the score, the larger the pixel value range and the more accurate it is, but the larger the image dimension, the greater the time-consuming complexity ). In this way, the image dimension is 16*16*16 = 4096 (from [0, 0] to [15, 15, 15]). In the code, the subscript operation is I + (j <4) + (k <8), which is equal to I + j * 16 + k * 16*16. For example, if a pixel is [4, 1, 20], there will be hist [4 + 1*16 + 20*16*16] ++;

The second method is to calculate the number of pixel values for each channel separately. For example, if a pixel value is [, 20], then there is bhist [4] ++; ghist [1] ++; rhist [20] ++; in this way, three 256-dimensional vectors are obtained and can be superimposed.

Distance Measurement

Distance measurements generally include Euclidean distance, Pearson correlation coefficient, and Cosine distance. However, Baidu Baike says that when performing histogram similarity measurement, the best effect is babbling distance. I did a simple test here and found that the European distance does not work very well. The possible reason is that [] and [] should be similar, however, the Euclidean distance is very large. In addition, the cosine distance is used to test the result.

Pasteuration distance: Also known as pasteuration coefficient. Used to measure the two discrete probability distributions. It is often separated between measurement classes in classification. The calculation formula is as follows:


P and P' represent the source and candidate image histogram data respectively, after the product of each data point with the same I value is square, the result is the image similarity value (the factor value of the babacache coefficient), ranging from 0 to 1. Why is it between 1? This is a mathematical problem and I will not investigate it. When p (I) = P' (I) for all I, the result is 1. Both p (I) and p '(I) are 0 ~ Between 1. P (I) indicates the number of occurrences of the pixel value and the number of pixels divided by the total number of pixels, which is a probability. It can be seen in the code.

Code:

Calculation Method 1:

(1) obtain the color histogram:

// 3D histogram method void getHistogram (Mat & image, int * histValue) {MatND hist; // CvHistogram * hist = cvCreateHistint dims = 3 in cv; float r_hranges [] = {0,255}; float g_hranges [] = {0,255}; float B _hranges [] = {0,255}; const float * ranges [] = {r_hranges, g_hranges, B _hranges}; // here the const type int size [3] = {16, 16, 16}; int channels [] = {0, 1, 2 }; // represents r g Channel 2 represents B channel // calculate the histogram calcHist (& image, 1, channels, Mat (), hist, dims, size, ranges) of the image ); // cvCalcHistfor (int I = 0; I <16; I ++) {for (int j = 0; j <16; j ++) {for (int k = 0; k <16; k ++) {float value = hist. at <float> (I, j, k); // note that the histogram value is cvQueryHistValue_1Dint realValue = saturate_cast in float type cv <int> (value ); int index = I + (j <4) + (k <8); histValue [index] = realValue ;}}}}


(2) code for three distance measurements

// Euclidean distance float getDistance (int * sur, int * dst) {float sum = 0; for (int I = 0; I <MaxHistValue; I ++) {sum + = pow (sur [I]-dst [I] + 0.0, 2);} return sqrt (sum) ;}// cosine distance float getCosDistance (int * sur, int * dst) {float surSum = 0, dstSum = 0, sum = 0; for (int I = 0; I <MaxHistValue; I ++) {surSum + = pow (sur [I] + 0.0, 2); dstSum + = pow (dst [I] + 0.0, 2 ); sum + = sur [I] * dst [I];} surSum = sqrt (surSum); dstSum = sqrt (dstSum); return sum/(surSum * dstSum );} // distance, which must be divided by the total number of elements // note: In the Similarity comparison of color histograms, the best float getPSDistance (int * sur, int * dst, const float sTotal, const float dTotal) {float sum = 0; for (int I = 0; I <MaxHistValue; I ++) {sum + = sqrt (sur [I]/sTotal) * (dst [I]/dTotal) ;}return sum ;}

Test image:


Cosine result:


Results:


[I-j], I represents personi, And j Represents the Hamming distance between personi and person. The results show that phash is powerless to rotate images.

From the results, we can see that for person6, It is very similar, but the cosine result is not good, while the babbling distance is very good. In addition, the babbling distance is not 1 for the source image, because the accuracy is lost during the calculation process.

 

Calculation Method 2:

(1) obtain the color histogram.

// 3D histogram method two void getHistogram2 (Mat & image, int ** HistValue) {for (int I = 0; I <image. rows; I ++) {for (int j = 0; j <image. cols; j ++) {HistValue [0] [image. at <Vec3b> (I, j) [0] ++; HistValue [1] [image. at <Vec3b> (I, j) [1] ++; HistValue [2] [image. at <Vec3b> (I, j) [2] ++ ;}}}

(2) code for three distance measurements

// Euclidean distance float getDistance (int ** sur, int ** dst) {float sum = 0; for (int I = 0; I <3; I ++) {for (int j = 0; j <256; j ++) {sum + = pow (sur [I] [j]-dst [I] [j] + 0.0, 2) ;}} return sqrt (sum);} // the cosine distance from float getCosDistance (int ** sur, int ** dst) {float surSum = 0, dstSum = 0, sum = 0; for (int I = 0; I <3; I ++) {for (int j = 0; j <256; j ++) {surSum + = pow (sur [I] [j] + 0.0, 2); dstSum + = pow (dst [I] [j] + 0.0, 2 ); sum + = sur [I] [j] * dst [I] [j] ;}} surSum = sqrt (surSum); dstSum = sqrt (dstSum ); return sum/(surSum * dstSum);} // distance, which must be divided by the total number of elements. // note: In the Similarity comparison of color histograms, best float getPSDistance (int ** sur, int ** dst, const float sTotal, const float dTotal) {float sum = 0; for (int I = 0; I <3; I ++) {for (int j = 0; j <256; j ++) {sum + = sqrt (sur [I] [j]/sTotal) * (dst [I] [j]/dTotal) ;}} return sum/3; // because there are three}

Cosine result:


Results:


[I-j], I represents personi, And j Represents the Hamming distance between personi and person. The results show that phash is powerless to rotate images.

Complete source code (Even hash, perceived hash, and color histogram ):

Http://download.csdn.net/detail/lu597203933/8710535

References:

1: http://blog.csdn.net/jia20003/article/details/7771651

2: http://blog.csdn.net/luoweifu/article/details/8690835

3: http://baike.baidu.com/view/10343198.htmba's distance

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.