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?
- Publicvoid setgreenbincount (INT greenbincount ){
- This. greenbins = greenbincount;
- }
- Publicvoid setbluebincount (INT bluebincount ){
- This. bluebins = bluebincount;
- }
- Publicfloat [] filter (bufferedimage SRC, bufferedimage DEST ){
- Int width = SRC. getwidth ();
- Int Height = SRC. getheight ();
- Int [] inpixels = newint [width * Height];
- Float [] histogramdata = newfloat [redbins * greenbins * bluebins];
- Getrgb (SRC, 0, 0, width, height, inpixels );
- Int Index = 0;
- Int redidx = 0, greenidx = 0, and blueidx = 0;
- Int singleindex = 0;
- Float Total = 0;
- For (int row = 0; row
- Int TA = 0, TR = 0, Tg = 0, TB = 0;
- For (INT Col = 0; Col <width; Col ++ ){
- Index = row * width + Col;
- Ta = (inpixels [Index]> 24) & 0xff;
- Tr = (inpixels [Index]> 16) & 0xff;
- Tg = (inpixels [Index]> 8) & 0xff;
- TB = inpixels [Index] & 0xff;
- Redidx = (INT) getbinindex (redbins, TR, 255 );
- Greenidx = (INT) getbinindex (greenbins, TG, 255 );
- Blueidx = (INT) getbinindex (bluebins, TB, 255 );
- Singleindex = redidx + greenidx * redbins + blueidx * redbins * greenbins;
- Histogramdata [singleindex] + = 1;
- Total + = 1;
- }
- }
- // Start to normalize the histogram data
- For (INT I = 0; I
- {
- Histogramdata [I] = histogramdata [I]/total;
- }
- Return histogramdata;
- }
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?
- /**
- * Bhattacharyya Coefficient
- * Http://www.cse.yorku.ca /~ Kosta/compvis_notes/bhattacharyya.pdf
- *
- * @ Return
- */
- Publicdouble modelmatch (){
- Histogramfilter hfilter = new histogramfilter ();
- Float [] sourcedata = hfilter. Filter (sourceimage, null );
- Float [] candidatedata = hfilter. Filter (candidateimage, null );
- Double [] mixeddata = newdouble [sourcedata. Length];
- For (INT I = 0; I <sourcedata. length; I ++ ){
- Mixeddata [I] = math. SQRT (sourcedata [I] * candidatedata [I]);
- }
- // The values of bhattacharyya coefficient ranges from 0 to 1,
- Double similarity = 0;
- For (INT I = 0; I <mixeddata. length; I ++ ){
- Similarity + = mixeddata [I];
- }
- // The degree of similarity
- Return similarity;
- }