颜色聚合向量
时间: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;
}
}
- 讨论 | 你是否遇到过你完全不能理解的机器学习概念?
- 【专业技术】C++里面重要的几个关键字的用法
- sql monitor的使用(一) (r2第30天)
- sql_profile的使用(一) (r2笔记29天)
- 关于权限设置的一个小把戏(r2第27天)
- R语言实现混合模型
- 数据迁移判断非空约束(r2笔记45天)
- 【专业技术】linux下socket编程
- oracle并行的小细节(r2笔记44天)
- python实现朴素贝叶斯模型:文本分类+垃圾邮件分类
- 使用闪回查询备份数据(r2笔记43天)
- 生产环境sql语句调优实战第四篇(r2笔记41天)
- 生产环境sql语句调优实战第五篇(r2笔记41天)
- python实现逻辑logistic回归:预测病马的死亡率
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- pytorch实现查看当前学习率
- python3.x中安装web.py步骤方法
- pytorch 网络参数 weight bias 初始化详解
- PHP中单例模式的使用场景与使用方法讲解
- pytorch查看模型weight与grad方式
- php+ajax 文件上传代码实例
- PHP将整数数字转换为罗马数字实例分享
- PHP如何通过表单直接提交大文件详解
- PHP基于openssl实现的非对称加密操作示例
- python图片验证码识别最新模块muggle_ocr的示例代码
- virtualenv介绍及简明教程
- Keras 数据增强ImageDataGenerator多输入多输出实例
- TensorFlow中如何确定张量的形状实例
- 使用Dajngo 通过代码添加xadmin用户和权限(组)
- python和js交互调用的方法