概述
出处:http://www.cnblogs.com/saintbird/archive/2008/10/07/1229154.html
最近一直在做数字图像处理方面的项目,其中基于内容的图像搜索是自己最感兴趣的一个方向,项目还在进行中,今天先对之前的工作做一个小结。
提到基于内容的图像搜索,就不能不提基于文本的图像搜索。 当前几乎所有的商用图像搜索引擎如Google、百度、雅虎等都是采用基于文本的检索方式,这种方式存在很大的弊端:首先图像一般都具有丰富的细节和它的引申意义,这是难以用文字描述出来的,仅靠几个关键字或简单的注释是远远不够的;其次,对同样一幅图像,每个人都会有不同的理解,这使得利用文本标注准确的响应用户查询非常困难;第三、对图像进行文本标注只能通过手工完成,这在图像数量较少时还基本可行,但如果图像数量增长过快或总量巨大时,依靠手工来完成文本的标注将变得十分困难。而基于内容的图像检索技术很好的解决了这些问题,通过提取图像本身的特征进行检索,获得的结果将是客观而全面的,并且整个过程都是计算机自动完成的,无需人工介入,速度和精度都有了很大提高。
基于内容的图像搜索技术早在70年代就有机构在做相关的研究,目前比较成功的有IBM的QBIC系统,Virage公司的图像检索系统,MIT的Photobook及美国加州大学开发的Chabot。基于内容的图像检索最关键的就是找到适合用于搜索的特征,常用的特征提取算法有:
基于颜色特征:颜色直方图、颜色集、颜色矩、颜色聚合向量、MEPG7颜色布局算子;
基于纹理特征:Tamura纹理特征、自回归纹理模型、Gabor变换、小波变换、MPEG7边缘直方图;
基于形状特征:傅立叶形状描述符、形状无关矩、小波轮廓描述符;
如果是视频文件的话,还有基于运动的特征提取算法,以上各种算法都有自己的优缺点,适用场合也不尽相同,通常需要多个特征组合在一起才能得到比较好的结果。
图像搜索现实的一般过程:
提取图像特征值→对特征值进行处理→匹配特征值
图像的特征值有很多,基于颜色特征,纹理特征,形状特征等,下面是基于图像颜色直方图特征的图像搜索。
(参考文章:http://blog.youkuaiyun.com/jia20003/article/details/7771651#comments )
原理
巴氏系数(Bhattacharyyacoefficient)算法
其中P, P’分别代表源与候选的图像直方图数据,对每个相同i的数据点乘积开平方以后相加
得出的结果即为图像相似度值(巴氏系数因子值),范围为0到1之间。为什么是到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位表示blue
- int 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 + " ");
- }
- }
效果
源图像(要搜索的图像)
要被搜索的图像
搜索的结果,相似度从大到小
- 上一篇:基于内容的图像搜索