颜色聚合向量

时间:2022-05-04
本文章向大家介绍颜色聚合向量,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
package com.imageretrieval.features;

/**
 * 颜色聚合向量<br>
 * 参考链接:http://www.docin.com/p-396527256.html
 * 
 * @author VenyoWang
 *
 */
public class ColorCoherenceVector {
	private static int BIN_WIDTH = 4;

	public static void main(String[] args) {
		int[][] matrix = getFeatureMatrix("");
		int[][] matrix1 = getFeatureMatrix("");
		System.out.println(calculateSimilarity(matrix2vector(matrix), Util.matrix2vector(matrix1)));
	}

	public static int[][] getFeatureMatrix(String imagePath) {
		// 均匀量化
		int[][] grayMatrix = getGrayPixel(imagePath, 200, 200);
		int width = grayMatrix[0].length;
		int height = grayMatrix.length;
		for(int i = 0; i < height; i++){
			for(int j = 0; j < width; j++){
				grayMatrix[i][j] /= ColorCoherenceVector.BIN_WIDTH;
			}
		}

		// 划分连通区域
		int[][] groupNums = new int[grayMatrix.length][grayMatrix[0].length];
		int groupNum = groupMatrix(grayMatrix, groupNums);

		// 判断聚合性
		// 统计每个分组下的像素数
		int[] groupCount = new int[groupNum];
		for(int i = 0; i < height; i++){
			for(int j = 0; j < width; j++){
				groupCount[groupNums[i][j]]++;
			}
		}
		
		// 阈值
		int threshold = width * height / 100;
		for(int i = 0; i < groupNum; i++){
			if(groupCount[i] < threshold){
				// 0表示非聚合
				groupCount[i] = 0;
			}
			else{
				// 1表示聚合
				groupCount[i] = 1;
			}
		}
		
		for(int i = 0; i < height; i++){
			for(int j = 0; j < width; j++){
				groupNums[i][j] = groupCount[groupNums[i][j]];
			}
		}

		// 计算图像特征
		int[][] feature = new int[256 / ColorCoherenceVector.BIN_WIDTH][2];
		for(int i = 0; i < height; i++){
			for(int j = 0; j < width; j++){
				if(groupNums[i][j] == 0){
					feature[grayMatrix[i][j] / ColorCoherenceVector.BIN_WIDTH][0]++;
				}
				else {
					feature[grayMatrix[i][j] / ColorCoherenceVector.BIN_WIDTH][1]++;
				}
			}
		}

		return feature;
	}
	
	private static int groupMatrix(int[][] matrix, int[][] groupNums) {
		for(int i = 0; i < groupNums.length; i++){
			for(int j = 0; j < groupNums[0].length; j++){
				groupNums[i][j] = -1;
			}
		}
		
		int groupNum = 0;
		for(int i = 0; i < groupNums.length; i++){
			for(int j = 0; j < groupNums[0].length; j++){
				if(groupNums[i][j] < 0){
					// 该像素点未进行分组,对其进行分组
					groupNums[i][j] = groupNum;
					recursive(matrix, i, j, groupNum, groupNums);
					groupNum++;
				}
			}
		}
		return groupNum + 1;
	}
	
	private static void recursive(int[][] matrix, int i, int j, int groupNum, int[][] groupNums){
		int num = matrix[i][j];
		int x = i - 1, y = j - 1;
		int maxX = matrix.length, maxY = matrix[0].length;
		if(x >= 0 && y >= 0 && x < maxX && y < maxY && groupNums[x][y] < 0 && matrix[x][y] == num){
			groupNums[x][y] = groupNum;
			recursive(matrix, x, y, groupNum, groupNums);
		}
		y = j;
		if(x >= 0 && y >= 0 && x < maxX && y < maxY && groupNums[x][y] < 0 && matrix[x][y] == num){
			groupNums[x][y] = groupNum;
			recursive(matrix, x, y, groupNum, groupNums);
		}
		y = j + 1;
		if(x >= 0 && y >= 0 && x < maxX && y < maxY && groupNums[x][y] < 0 && matrix[x][y] == num){
			groupNums[x][y] = groupNum;
			recursive(matrix, x, y, groupNum, groupNums);
		}
		x = i;y = j - 1;
		if(x >= 0 && y >= 0 && x < maxX && y < maxY && groupNums[x][y] < 0 && matrix[x][y] == num){
			groupNums[x][y] = groupNum;
			recursive(matrix, x, y, groupNum, groupNums);
		}
		y = j + 1;
		if(x >= 0 && y >= 0 && x < maxX && y < maxY && groupNums[x][y] < 0 && matrix[x][y] == num){
			groupNums[x][y] = groupNum;
			recursive(matrix, x, y, groupNum, groupNums);
		}
		x = i + 1;y = j - 1;
		if(x >= 0 && y >= 0 && x < maxX && y < maxY && groupNums[x][y] < 0 && matrix[x][y] == num){
			groupNums[x][y] = groupNum;
			recursive(matrix, x, y, groupNum, groupNums);
		}
		y = j;
		if(x >= 0 && y >= 0 && x < maxX && y < maxY && groupNums[x][y] < 0 && matrix[x][y] == num){
			groupNums[x][y] = groupNum;
			recursive(matrix, x, y, groupNum, groupNums);
		}
		y = j + 1;
		if(x >= 0 && y >= 0 && x < maxX && y < maxY && groupNums[x][y] < 0 && matrix[x][y] == num){
			groupNums[x][y] = groupNum;
			recursive(matrix, x, y, groupNum, groupNums);
		}
	}

    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;
	}

    public static int[] matrix2vector(int[][] matrix){
		if(matrix.length <= 0 || matrix[0].length <= 0){
			return null;
		}
		int[] vector = new int[matrix.length * matrix[0].length];
		int index = 0;
		for(int i = 0; i < matrix.length; i++){
			for(int j = 0; j < matrix[0].length; j++, index++){
				vector[index] = matrix[i][j];
			}
		}
		return vector;
	}
}