This article describes the Java implementation based on histogram application of similar image recognition, is a very practical skill. Share to everyone for your reference. The specific analysis is as follows:
An overview of the algorithm:
Firstly, the histogram data is collected from the source image and the image to be screened, the histogram of each image is normalized and then the histogram is computed with the PAP coefficient algorithm, and the image similarity value is obtained, and the value range is between [0, 1]
0 means extremely different, and 1 represents extremely similar (same).
Second, the algorithm steps detailed:
Can be divided into two steps, according to the source image and the candidate image pixel data, generate their own histogram data. The second step: using the histogram result of the first step output, using the PAP coefficient (Bhattacharyya coefficient) algorithm, calculates the similarity degree value.
The first step: Histogram calculation
Histogram is divided into gray histogram and RGB histogram, for gray-scale image histogram calculation is very simple, as long as the initialization of a size of 256 histogram array h, and then complete the frequency distribution according to the pixel value, assuming that the pixel value is 124, then h[124] + = 1, For color RGB pixels, there are two ways of histogram expression, one is a single histogram, the other is a three-dimensional histogram, three-dimensional histogram is relatively simple and clear, corresponding to RGB three colors, the definition of three histogram hr,hg, HB, assuming that a pixel point P RGB value (4, 231,129), The histogram for the calculation of hr[4] + = 1,hg[231] + = 1, hb[129] + = 1, so for each pixel after the completion of statistics, RGB color histogram data generated.
And the single histogram of the RGB pixel sh represents a slightly more complex point, the value range of each color is between 0 ~ 255, the assumption can be divided into a certain range of equal parts, when 8 equal, each equal value range of 32, 16 equal parts, each equal value range of 16, when 4 equal portions, each equal value of the range of 64, Assuming RGB values (14, 68, 221), 16 equal parts, it corresponds to the Histogram index value (index) is: (0, 4, 13), according to the computed index value formula: index = R + g*16 + b*16*16
Corresponding Histogram index = 0 + 4*16 + *, sh[3392] + = 1
So traversing all RGB pixel values, complete the histogram data calculation.
The second step: Pap coefficient calculation, the calculation formula is as follows:
where p, p ' respectively represents the source and candidate image histogram data, each of the same I data point product squared after the sum
The result is the image similarity value (PAP coefficient factor value), the range is between 0 and 1.
The program effect is shown in the following illustration:
More than 99% similarity, very similar
Similarity is: 72%, generally similar
Third, the program histogram calculation source code is as follows:
public void Setgreenbincount (int greenbincount) {this.greenbins = Greenbincount;
The 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 to calculate the PAP coefficient is as follows:
/**
* Bhattacharyya coefficient *
http://www.cse.yorku.ca/~kosta/CompVis_Notes/bhattacharyya.pdf
*
* @return
*
/public double Modelmatch () {
Histogramfilter hfilter = new Histogramfilter ();
float[] SourceData = hfilter.filter (sourceimage, null);
float[] Candidatedata = hfilter.filter (candidateimage, null);
double[] Mixeddata = new Double[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;
}
I hope this article will help you with your Java programming.