圖像處理之相似圖片識別(直方圖應用篇)算法概述:
首先對源圖像與要篩選的圖像進行直方圖數據采集,對采集的各自圖像直方圖進行歸一化再
使用巴氏系數算法對直方圖數據進行計算,最終得出圖像相似度值,其值范圍在[0, 1]之間
0表示極其不同,1表示極其相似(相同)。
算法步驟詳解:
大致可以分為兩步,根據源圖像與候選圖像的像素數據,生成各自直方圖數據。第二步:使
用第一步輸出的直方圖結果,運用巴氏系數(Bhattacharyya coefficient)算法,計算出相似程
度值。
第一步:直方圖計算
直方圖分為灰度直方圖與RGB直方圖,對于灰度圖像直方圖計算十分簡單,只要初始化一
個大小為256的直方圖數組H,然后根據像素值完成頻率分布統計,假設像素值為124,則
H[124] += 1, 而對于彩色RGB像素來說直方圖表達有兩種方式,一種是單一直方圖,另外一
種是三維直方圖,三維直方圖比較簡單明了,分別對應RGB三種顏色,定義三個直方圖HR,
HG, HB, 假設某一個像素點P的RGB值為(4, 231,129), 則對于的直方圖計算為HR[4] += 1,
HG[231] += 1, HB[129] += 1, 如此對每個像素點完成統計以后,RGB彩色直方圖數據就生成了。
而RGB像素的單一直方圖SH表示稍微復雜點,每個顏色的值范圍為0 ~ 255之間的,假設
可以分為一定范圍等份,當8等份時,每個等份的值范圍為32, 16等份時,每個等份值范
圍為16,當4等份時候,每個等份值的范圍為64,假設RGB值為(14, 68, 221), 16等份之
后,它對應直方圖索引值(index)分別為: (0, 4, 13), 根據計算索引值公式:index = R + G*16 + B*16*16
對應的直方圖index = 0 + 4*16 + 13 * 16 * 16, SH[3392] += 1
如此遍歷所有RGB像素值,完成直方圖數據計算。
第二步:巴氏系數計算,計算公式如下:
其中P, P’分別代表源與候選的圖像直方圖數據,對每個相同i的數據點乘積開平方以后相加
得出的結果即為圖像相似度值(巴氏系數因子值),范圍為0到1之間。
程序效果:

相似度超過99%以上,極其相似
相似度為:72%, 一般相似
程序直方圖計算源代碼如下:
[java]view plaincopy- publicvoidsetGreenBinCount(intgreenBinCount){
- this.greenBins=greenBinCount;
- }
- publicvoidsetBlueBinCount(intblueBinCount){
- this.blueBins=blueBinCount;
- }
- publicfloat[]filter(BufferedImagesrc,BufferedImagedest){
- intwidth=src.getWidth();
- intheight=src.getHeight();
- int[]inPixels=newint[width*height];
- float[]histogramData=newfloat[redBins*greenBins*blueBins];
- getRGB(src,0,0,width,height,inPixels);
- intindex=0;
- intredIdx=0,greenIdx=0,blueIdx=0;
- intsingleIndex=0;
- floattotal=0;
- for(introw=0;row<height;row++){
- intta=0,tr=0,tg=0,tb=0;
- for(intcol=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;
- }
- }
- //starttonormalizethehistogramdata
- for(inti=0;i<histogramData.length;i++)
- {
- histogramData[i]=histogramData[i]/total;
- }
- returnhistogramData;
- }
計算巴氏系數的代碼如下:
[java]view plaincopy- /**
- *BhattacharyyaCoefficient
- *http://www.cse.yorku.ca/~kosta/CompVis_Notes/bhattacharyya.pdf
- *
- *@return
- */
- publicdoublemodelMatch(){
- HistogramFilterhfilter=newHistogramFilter();
- float[]sourceData=hfilter.filter(sourceImage,null);
- float[]candidateData=hfilter.filter(candidateImage,null);
- double[]mixedData=newdouble[sourceData.length];
- for(inti=0;i<sourceData.length;i++){
- mixedData[i]=Math.sqrt(sourceData[i]*candidateData[i]);
- }
- //ThevaluesofBhattacharyyaCoefficientrangesfrom0to1,
- doublesimilarity=0;
- for(inti=0;i<mixedData.length;i++){
- similarity+=mixedData[i];
- }
- //Thedegreeofsimilarity
- returnsimilarity;
- }