基于直方图特征的图像搜索

来源:互联网 发布:云盘搬家软件 编辑:程序博客网 时间:2024/05/29 15:13

概述

       图像搜索现实的一般过程:

提取图像特征值→对特征值进行处理→匹配特征值

       图像的特征值有很多,基于颜色特征,纹理特征,形状特征等,下面是基于图像颜色直方图特征的图像搜索。

(参考文章:http://blog.csdn.net/jia20003/article/details/7771651#comments )

原理

       巴氏系数(Bhattacharyyacoefficient)算法


       其中P, P’分别代表源与候选的图像直方图数据,对每个相同i的数据点乘积开平方以后相加

得出的结果即为图像相似度值(巴氏系数因子值),范围为01之间。为什么是到1之间,这是数学的问题,就不追究了。

步骤

一、     求源图像和要被搜索图像的直方图特征

二、     根据直方图特征,用巴氏系数算法求出源图像和要搜索图像的相似度

       彩色图像的每个像素由red,green,blue三种组成,如何好地表示彩色图像的直方图更呢?一般有两种方式:

       一种是用三维的直方图表示,这种方式简单明了,如hist[][]hist[0][]表示red的直方图,hist[1][]表示green的直方图,hist[2][]表示blue的直方图;如一个像素为(156,72,89),hist[0][156]++; hist[0][72]++, hist[0][89]++;

       另一种方式是降低灰度的级数,用一维直方图表示,如将256级的灰度降至16级,可用12位的int表示灰度值,前4位表示red,中间4们表示green,后面4位表示blue;一个像素为(156,72,89), r=156/16=9; g=72/16=4,b=89/16=5; index = r<<(2*4) | g<<4 | b; hist[index] ++;

 源码


三维直方图表示

/** * 求三维的灰度直方图 * @param srcPath * @return */public static double[][] getHistgram(String srcPath) {BufferedImage img = ImageDigital.readImg(srcPath);return getHistogram(img);}/** * hist[0][]red的直方图,hist[1][]green的直方图,hist[2][]blue的直方图 * @param img 要获取直方图的图像 * @return 返回r,g,b的三维直方图 */public static double[][] getHistogram(BufferedImage img) {int w = img.getWidth();int h = img.getHeight();double[][] hist = new double[3][256]; int r, g, b;int pix[] = new int[w*h]; pix = img.getRGB(0, 0, w, h, pix, 0, w);for(int i=0; i<w*h; i++) {r = pix[i]>>16 & 0xff;g = pix[i]>>8 & 0xff;b = pix[i] & 0xff;/*hr[r] ++;hg[g] ++;hb[b] ++;*/hist[0][r] ++;hist[1][g] ++;hist[2][b] ++;}for(int j=0; j<256; j++) {for(int i=0; i<3; i++) {hist[i][j] = hist[i][j]/(w*h);//System.out.println(hist[i][j] + "  ");}}return hist;}public double indentification(String srcPath, String destPath) {BufferedImage srcImg = ImageDigital.readImg(srcPath);BufferedImage destImg = ImageDigital.readImg(destPath);return indentification(srcImg, destImg);}public double indentification(BufferedImage srcImg, BufferedImage destImg) {double[][] histR = getHistogram(srcImg);double[][] histD = getHistogram(destImg);return indentification(histR, histD);}public static double indentification(double[][] histR, double[][] histD) {double p = (double) 0.0;for(int i=0; i<histR.length; i++) {for(int j=0; j<histR[0].length; j++) {p += Math.sqrt(histR[i][j]*histD[i][j]);}}return p/3;}/** * 用三维灰度直方图求图像的相似度 * @param n * @param str1 * @param str2 */public static void histogramIditification(int n, String str1, String str2) {double p = 0;double[][] histR = GreyIdentification.getHistgram(str1); double[][] histD = null;for(int i=0; i<n; i++) {histD = GreyIdentification.getHistgram(str2 + (i+1) + ".jpg");p = GreyIdentification.indentification(histR, histD);System.out.print((i+1) + "--" + p + "    ");}}

一维直方图表示

/** * 求一维的灰度直方图 * @param srcPath * @return */public static double[] getHistgram2(String srcPath) {BufferedImage img = ImageDigital.readImg(srcPath);return getHistogram2(img);}/** * 求一维的灰度直方图 * @param img * @return */public static double[] getHistogram2(BufferedImage img) {int w = img.getWidth();int h = img.getHeight();int series = (int) Math.pow(2, GRAYBIT);//GRAYBIT=4;用12位的int表示灰度值,前4位表示red,中间4们表示green,后面4位表示blueint greyScope = 256/series;double[] hist = new double[series*series*series]; int r, g, b, index;int pix[] = new int[w*h]; pix = img.getRGB(0, 0, w, h, pix, 0, w);for(int i=0; i<w*h; i++) {r = pix[i]>>16 & 0xff;r = r/greyScope;g = pix[i]>>8 & 0xff;g = g/greyScope;b = pix[i] & 0xff;b = b/greyScope;index = r<<(2*GRAYBIT) | g<<GRAYBIT | b; hist[index] ++;}for(int i=0; i<hist.length; i++) {hist[i] = hist[i]/(w*h);//System.out.println(hist[i] + "  ");}return hist;}public double indentification2(String srcPath, String destPath) {BufferedImage srcImg = ImageDigital.readImg(srcPath);BufferedImage destImg = ImageDigital.readImg(destPath);return indentification2(srcImg, destImg);}public double indentification2(BufferedImage srcImg, BufferedImage destImg) {double[] histR = getHistogram2(srcImg);double[] histD = getHistogram2(destImg);return indentification2(histR, histD);}public static double indentification2(double[] histR, double[] histD) {double p = (double) 0.0;for(int i=0; i<histR.length; i++) {p += Math.sqrt(histR[i]*histD[i]);}return p;}/** * 用一维直方图求图像的相似度 * @param n * @param str1 * @param str2 */public static void histogramIditification2(int n, String str1, String str2) {double p = 0;double[] histR = GreyIdentification.getHistgram2(str1); double[] histD = null;for(int i=0; i<n; i++) {histD = GreyIdentification.getHistgram2(str2 + (i+1) + ".jpg");p = GreyIdentification.indentification2(histR, histD);System.out.print((i+1) + "--" + p + "    ");}}

效果

源图像(要搜索的图像)


要被搜索的图像


搜索的结果,相似度从大到小