Image processing-similar image recognition (histogram Application)

Source: Internet
Author: User

Image processing-similar image recognition (histogram Application)

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

Algorithm Overview:

First, histogram data is collected for the source image and the image to be filtered, and then the respective image histograms are normalized.

Calculate the histogram data using the babacache coefficient algorithm to obtain the image similarity value. The value ranges from 0 to 1.

0 represents extremely different, and 1 represents extremely similar (identical ).

 

Detailed algorithm steps:

The histogram data is generated based on the pixel data of the source image and the candidate image. Step 2: Enable

Use the histogram result output in step 1 and the bhattacharyya coefficient algorithm to calculate the similarity

Degree value.

 

Step 1: histogram Calculation

The histogram is divided into a grayscale histogram and an RGB histogram. It is very easy to calculate a grayscale image histogram, as long as you initialize

Histogram array h with a size of 256, and then completes the frequency distribution statistics based on the pixel value. If the pixel value is 124

H [124] + = 1, while for color RGB pixels, there are two ways to express the histogram, one is a single histogram, the other is

Three-dimensional histograms, which are simple and clear, correspond to three RGB colors respectively, and define three histograms HR,

HG, Hb. Assume that the RGB value of P of a certain pixel is (4,231,129), then the histogram is calculated as HR [4] + = 1,

Hg [231] + = 1, Hb [129] + = 1. After each pixel is calculated, the RGB color histogram data is generated.

The sh of a single histogram of RGB pixels indicates a slightly complex point. The value range of each color is 0 ~ Between 255, suppose

Can be divided into a certain range of equal portions, when the 8 equal portions, each equal portions of the value range is 32, 16 equal portions, each equal value range

The circumference is 16. When the value is 4, the range of each equal value is 64. Assume that the RGB value is (14, 68,221 ),

Then, it corresponds to the histogram index values (INDEX) respectively: (0, 4, 13), according to the formula for calculating the index value:Index = R + G * 16 + B * 16*16

 

Corresponding histogram Index = 0 + 4*16 + 13*16*16, SH [3392] + = 1

In this way, all RGB pixel values are traversed to complete histogram data calculation.

 

Step 2: Calculate the barrier coefficient. The formula is as follows:

 

P and P' represent the histogram data of the source and candidate images respectively. After the product of each data point with the same I value is squared, the data points are added.

The obtained result is the image similarity value (coefficient factor value), ranging from 0 to 1.

Program effect:

Similarity exceeds 99%, extremely similar

Similarity: 72%, generally similar

The source code for program histogram calculation is as follows:

 

[Java]View plaincopyprint?
  1. Publicvoid setgreenbincount (INT greenbincount ){
  2. This. greenbins = greenbincount;
  3. }
  4. Publicvoid setbluebincount (INT bluebincount ){
  5. This. bluebins = bluebincount;
  6. }
  7. Publicfloat [] filter (bufferedimage SRC, bufferedimage DEST ){
  8. Int width = SRC. getwidth ();
  9. Int Height = SRC. getheight ();
  10. Int [] inpixels = newint [width * Height];
  11. Float [] histogramdata = newfloat [redbins * greenbins * bluebins];
  12. Getrgb (SRC, 0, 0, width, height, inpixels );
  13. Int Index = 0;
  14. Int redidx = 0, greenidx = 0, and blueidx = 0;
  15. Int singleindex = 0;
  16. Float Total = 0;
  17. For (int row = 0; row
  18. Int TA = 0, TR = 0, Tg = 0, TB = 0;
  19. For (INT Col = 0; Col <width; Col ++ ){
  20. Index = row * width + Col;
  21. Ta = (inpixels [Index]> 24) & 0xff;
  22. Tr = (inpixels [Index]> 16) & 0xff;
  23. Tg = (inpixels [Index]> 8) & 0xff;
  24. TB = inpixels [Index] & 0xff;
  25. Redidx = (INT) getbinindex (redbins, TR, 255 );
  26. Greenidx = (INT) getbinindex (greenbins, TG, 255 );
  27. Blueidx = (INT) getbinindex (bluebins, TB, 255 );
  28. Singleindex = redidx + greenidx * redbins + blueidx * redbins * greenbins;
  29. Histogramdata [singleindex] + = 1;
  30. Total + = 1;
  31. }
  32. }
  33. // Start to normalize the histogram data
  34. For (INT I = 0; I
  35. {
  36. Histogramdata [I] = histogramdata [I]/total;
  37. }
  38. Return histogramdata;
  39. }
public void setGreenBinCount(int greenBinCount) {this.greenBins = greenBinCount;}public void setBlueBinCount(int blueBinCount) {this.blueBins = blueBinCount;}public float[] filter(BufferedImage src, BufferedImage dest) {int width = src.getWidth();        int height = src.getHeight();                int[] inPixels = new int[width*height];        float[] histogramData = new float[redBins * greenBins * blueBins];        getRGB( src, 0, 0, width, height, inPixels );        int index = 0;        int redIdx = 0, greenIdx = 0, blueIdx = 0;        int singleIndex = 0;        float total = 0;        for(int row=0; row

The code for calculating the pasteurization coefficient is as follows:

 

 

[Java]View plaincopyprint?
  1. /**
  2. * Bhattacharyya Coefficient
  3. * Http://www.cse.yorku.ca /~ Kosta/compvis_notes/bhattacharyya.pdf
  4. *
  5. * @ Return
  6. */
  7. Publicdouble modelmatch (){
  8. Histogramfilter hfilter = new histogramfilter ();
  9. Float [] sourcedata = hfilter. Filter (sourceimage, null );
  10. Float [] candidatedata = hfilter. Filter (candidateimage, null );
  11. Double [] mixeddata = newdouble [sourcedata. Length];
  12. For (INT I = 0; I <sourcedata. length; I ++ ){
  13. Mixeddata [I] = math. SQRT (sourcedata [I] * candidatedata [I]);
  14. }
  15. // The values of bhattacharyya coefficient ranges from 0 to 1,
  16. Double similarity = 0;
  17. For (INT I = 0; I <mixeddata. length; I ++ ){
  18. Similarity + = mixeddata [I];
  19. }
  20. // The degree of similarity
  21. Return similarity;
  22. }

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.