600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > halcon 相似度_列举一些算法对照片 图像进行相似度对比分析比较

halcon 相似度_列举一些算法对照片 图像进行相似度对比分析比较

时间:2020-03-13 19:31:42

相关推荐

halcon 相似度_列举一些算法对照片 图像进行相似度对比分析比较

packagecom.aliyun.picture.demo;/*** @BelongsProject: maven-demo

* @BelongsPackage: com.aliyun.picture.demo

* @Author: Guoyh

* @CreateTime: -10-12 15:05

* @Description: 感知哈希算法(均值哈希算法)比较两图的相似性*/

importjavax.imageio.ImageIO;importjava.awt.Graphics;importjava.awt.Image;importjava.awt.color.ColorSpace;importjava.awt.image.BufferedImage;importjava.awt.image.ColorConvertOp;importjava.util.Arrays;importjava.io.File;public final classFingerPrint {/*** 图像指纹的尺寸,将图像resize到指定的尺寸,来计算哈希数组*/

private static final int HASH_SIZE = 16;/*** 保存图像指纹的二值化矩阵*/

private final byte[] binaryzationMatrix;public FingerPrint(byte[] hashValue) {if (hashValue.length != HASH_SIZE *HASH_SIZE) {throw new IllegalArgumentException(String.format("length of hashValue must be %d", HASH_SIZE *HASH_SIZE));

}this.binaryzationMatrix =hashValue;

}publicFingerPrint(String hashValue) {this(toBytes(hashValue));

}publicFingerPrint(BufferedImage src) {this(hashValue(src));

}private static byte[] hashValue(BufferedImage src) {

BufferedImage hashImage=resize(src, HASH_SIZE, HASH_SIZE);byte[] matrixGray = (byte[]) toGray(hashImage).getData().getDataElements(0, 0, HASH_SIZE, HASH_SIZE, null);returnbinaryzation(matrixGray);

}/*** 从压缩格式指纹创建{@linkFingerPrint}对象

*

*@paramcompactValue

*@return

*/

public static FingerPrint createFromCompact(byte[] compactValue) {return newFingerPrint(uncompact(compactValue));

}public static boolean validHashValue(byte[] hashValue) {if (hashValue.length !=HASH_SIZE) {return false;

}for (byteb : hashValue) {

{if (0 != b && 1 !=b) {return false;

}

}

}return true;

}public static booleanvalidHashValue(String hashValue) {if (hashValue.length() !=HASH_SIZE) {return false;

}for (int i = 0; i < hashValue.length(); ++i) {if ('0' != hashValue.charAt(i) && '1' !=hashValue.charAt(i)) {return false;

}

}return true;

}public byte[] compact() {returncompact(binaryzationMatrix);

}/*** 指纹数据按位压缩

*

*@paramhashValue

*@return

*/

private static byte[] compact(byte[] hashValue) {byte[] result = new byte[(hashValue.length + 7) >> 3];byte b = 0;for (int i = 0; i < hashValue.length; ++i) {if (0 == (i & 7)) {

b= 0;

}if (1 ==hashValue[i]) {

b|= 1 << (i & 7);

}else if (hashValue[i] != 0) {throw new IllegalArgumentException("invalid hashValue,every element must be 0 or 1");

}if (7 == (i & 7) || i == hashValue.length - 1) {

result[i>> 3] =b;

}

}returnresult;

}/*** 压缩格式的指纹解压缩

*

*@paramcompactValue

*@return

*/

private static byte[] uncompact(byte[] compactValue) {byte[] result = new byte[compactValue.length << 3];for (int i = 0; i < result.length; ++i) {if ((compactValue[i >> 3] & (1 << (i & 7))) == 0) {

result[i]= 0;

}else{

result[i]= 1;

}

}returnresult;

}/*** 字符串类型的指纹数据转为字节数组

*

*@paramhashValue

*@return

*/

private static byte[] toBytes(String hashValue) {

hashValue= hashValue.replaceAll("\\s", "");byte[] result = new byte[hashValue.length()];for (int i = 0; i < result.length; ++i) {char c =hashValue.charAt(i);if ('0' ==c) {

result[i]= 0;

}else if ('1' ==c) {

result[i]= 1;

}else{throw new IllegalArgumentException("invalid hashValue String");

}

}returnresult;

}/*** 缩放图像到指定尺寸

*

*@paramsrc

*@paramwidth

*@paramheight

*@return

*/

private static BufferedImage resize(Image src, int width, intheight) {

BufferedImage result= newBufferedImage(width, height,

BufferedImage.TYPE_3BYTE_BGR);

Graphics g=result.getGraphics();try{

g.drawImage(src.getScaledInstance(width, height, Image.SCALE_SMOOTH),0, 0, null);

}finally{

g.dispose();

}returnresult;

}/*** 计算均值

*

*@paramsrc

*@return

*/

private static int mean(byte[] src) {long sum = 0;//将数组元素转为无符号整数

for (byteb : src) {

sum+= (long) b & 0xff;

}return (int) (Math.round((float) sum /src.length));

}/*** 二值化处理

*

*@paramsrc

*@return

*/

private static byte[] binaryzation(byte[] src) {byte[] dst =src.clone();int mean =mean(src);for (int i = 0; i < dst.length; ++i) {//将数组元素转为无符号整数再比较

dst[i] = (byte) (((int) dst[i] & 0xff) >= mean ? 1 : 0);

}returndst;

}/*** 转灰度图像

*

*@paramsrc

*@return

*/

private staticBufferedImage toGray(BufferedImage src) {if (src.getType() ==BufferedImage.TYPE_BYTE_GRAY) {returnsrc;

}else{//图像转灰

BufferedImage grayImage = newBufferedImage(src.getWidth(), src.getHeight(),

BufferedImage.TYPE_BYTE_GRAY);new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null).filter(src, grayImage);returngrayImage;

}

}

@OverridepublicString toString() {return toString(true);

}/***@parammultiLine 是否分行

*@return

*/

public String toString(booleanmultiLine) {

StringBuffer buffer= newStringBuffer();int count = 0;for (byte b : this.binaryzationMatrix) {

buffer.append(0 == b ? '0' : '1');if (multiLine && ++count % HASH_SIZE == 0) {

buffer.append('\n');

}

}returnbuffer.toString();

}

@Overridepublic booleanequals(Object obj) {if (obj instanceofFingerPrint) {return Arrays.equals(this.binaryzationMatrix, ((FingerPrint) obj).binaryzationMatrix);

}else{return super.equals(obj);

}

}/*** 与指定的压缩格式指纹比较相似度

*

*@paramcompactValue

*@return*@see#compare(FingerPrint)*/

public float compareCompact(byte[] compactValue) {returncompare(createFromCompact(compactValue));

}/***@paramhashValue

*@return*@see#compare(FingerPrint)*/

public floatcompare(String hashValue) {return compare(newFingerPrint(hashValue));

}/*** 与指定的指纹比较相似度

*

*@paramhashValue

*@return*@see#compare(FingerPrint)*/

public float compare(byte[] hashValue) {return compare(newFingerPrint(hashValue));

}/*** 与指定图像比较相似度

*

*@paramimage2

*@return*@see#compare(FingerPrint)*/

public floatcompare(BufferedImage image2) {return compare(newFingerPrint(image2));

}/*** 比较指纹相似度

*

*@paramsrc

*@return*@see#compare(byte[], byte[])*/

public floatcompare(FingerPrint src) {if (src.binaryzationMatrix.length != this.binaryzationMatrix.length) {throw new IllegalArgumentException("length of hashValue is mismatch");

}returncompare(binaryzationMatrix, src.binaryzationMatrix);

}/*** 判断两个数组相似度,数组长度必须一致否则抛出异常

*

*@paramf1

*@paramf2

*@return返回相似度(0.0 ~ 1.0)*/

private static float compare(byte[] f1, byte[] f2) {if (f1.length !=f2.length) {throw new IllegalArgumentException("mismatch FingerPrint length");

}int sameCount = 0;for (int i = 0; i < f1.length; ++i) {

{if (f1[i] ==f2[i]) {++sameCount;

}

}

}return (float) sameCount /f1.length;

}public static float compareCompact(byte[] f1, byte[] f2) {returncompare(uncompact(f1), uncompact(f2));

}public static floatcompare(BufferedImage image1, BufferedImage image2) {return new FingerPrint(image1).compare(newFingerPrint(image2));

}/***@paramargs

*@returnvoid

*@authorGuoyh

* @date /10/12 15:07*/

public static void main(String[] args) throwsException {for (int i = 18; i <= 21; i++) {

FingerPrint fp1= new FingerPrint(ImageIO.read(new File("G:/oss/pk/" + 18 + ".jpg")));

FingerPrint fp2= new FingerPrint(ImageIO.read(new File("G:/oss/pk/" + i + ".jpg")));

System.out.println("\t" + "\t"+"G:/oss/pk/" + 18 + ".jpg"+"\t"+"与"+"\t"+"G:/oss/pk/" + i + ".jpg"+"\t"+"两张图片的相似度为:" + pare(fp2) * 100 + "%");

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。