均匀LBP纹理特征


package com.imageretrieval.features;

/**
 * 均匀LBP纹理特征<br>
 * 参考链接:http://blog.csdn.net/lu597203933/article/details/17184503
 * @author VenyoWang
 *
 */
public class UniformLBP {

	public static void main(String[] args) {
		int[] vector = getFeatureVector("");
		int[] vector1 = getFeatureVector("");
		System.out.println(calculateSimilarity(vector, vector1));
	}
	
	public static int[] getFeatureVector(String imagePath){
		// 获取灰度矩阵
		int[][] grayMatrix = getGrayPixel(imagePath, 200, 200);
		
		// 为特征值(0-255)分组(降维)
		int[] groupNums = groupFeatureValue();
		
		// 遍历像素点,计算其特征值,并确定其分组,并进行分组统计
		int[] vector = new int[59];
		for(int i = 1; i < grayMatrix.length - 1; i++){
			for(int j = 1; j < grayMatrix[0].length - 1; j++){
				int center = grayMatrix[i][j];
				int feature = 0;
				feature = grayMatrix[i - 1][j - 1]>=center ? (feature<<1)+1 : (feature<<1);
				feature = grayMatrix[i][j - 1]>=center ? (feature<<1)+1 : (feature<<1);
				feature = grayMatrix[i + 1][j - 1]>=center ? (feature<<1)+1 : (feature<<1);
				feature = grayMatrix[i + 1][j]>=center ? (feature<<1)+1 : (feature<<1);
				feature = grayMatrix[i + 1][j + 1]>=center ? (feature<<1)+1 : (feature<<1);
				feature = grayMatrix[i][j + 1]>=center ? (feature<<1)+1 : (feature<<1);
				feature = grayMatrix[i - 1][j + 1]>=center ? (feature<<1)+1 : (feature<<1);
				feature = grayMatrix[i - 1][j]>=center ? (feature<<1)+1 : (feature<<1);
				feature = getMinFeature(feature);
				vector[groupNums[feature]]++;
			}
		}
		
		return vector;
	}
	
	/**
	 * 对256个特征值进行分组<br>
	 * groupNums的下标为特征值,value为组号
	 * @return
	 */
	private static int[] groupFeatureValue(){
		int[] groupNums = new int[256];
		int num = 1;
		for(int i = 0; i <= 255; i++){
			if(getHopCount(i) <= 2){
				groupNums[i] = num;
				num++;
			}
		}
		return groupNums;
	}
	
	/**
	 * 计算跳变次数
	 * @param i
	 * @return
	 */
	private static int getHopCount(int i)  
	{  
	    int[] a = new int[8];
	    int cnt = 0;  
	    int k = 7;  
	    while(i > 0)  
	    {  
	        a[k] = i & 1;  
	        i = i >> 1;  
	        --k;  
	    }  
	    for(k = 0; k < 7; k++)  
	    {  
	        if(a[k] != a[k+1])  
	        {  
	            ++cnt;  
	        }  
	    }  
	    if(a[0] != a[7])  
	    {  
	        ++cnt;  
	    }  
	    return cnt;  
	}  
	
	/**
	 * 旋转不变性<br>
	 * 此处的feature的二进制位数固定为8
	 * @param feature
	 * @return
	 */
	private static int getMinFeature(int feature){
		int minFeature = feature;
		for(int i = 0; i < 7; i++){
			// 循环右移一位
			feature = (feature>>1 | feature<<7) & 0xff;
			if(feature < minFeature) minFeature = feature;
		}
		
		return minFeature;
	}

    public static double calculateSimilarity(int[] vector, int[] vector1) {
		double len = 0, len1 = 0, numerator = 0;
		for (int i = 0; i < vector.length; i++) {
			len += Math.pow(vector[i], 2);
			len1 += Math.pow(vector1[i], 2);
			numerator += vector[i] * vector1[i];
		}
		len = Math.sqrt(len);
		len1 = Math.sqrt(len1);

		return numerator / (len * len1);
	}

    public static int[][] getGrayPixel(String imagePath, int width, int height) {
		BufferedImage bi = null;
		try {
			bi = resizeImage(imagePath, width, height, BufferedImage.TYPE_INT_RGB);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		int minx = bi.getMinX();
		int miny = bi.getMinY();
		int[][] matrix = new int[width - minx][height - miny];
		for (int i = minx; i < width; i++) {
			for (int j = miny; j < height; j++) {
				int pixel = bi.getRGB(i, j);
				int red = (pixel & 0xff0000) >> 16;
				int green = (pixel & 0xff00) >> 8;
				int blue = (pixel & 0xff);
				int gray = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
				matrix[i][j] = gray;
			}
		}
		return matrix;
	}

	public static BufferedImage resizeImage(String srcImgPath, int width, int height, int imageType)
			throws IOException {
		File srcFile = new File(srcImgPath);
		BufferedImage srcImg = ImageIO.read(srcFile);
		BufferedImage buffImg = null;
		buffImg = new BufferedImage(width, height, imageType);
		buffImg.getGraphics().drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
		return buffImg;
	}
}
优质内容筛选与推荐>>
1、会借鉴 一个美国人对我的帮助
2、web自动化
3、DCMTK3.6.0(MD支持库)安装说明
4、每日英语:Microsoft's Windows 8 Test: Courting Consumers
5、LeetCode 80


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号