比较两张图片的相似度 - 开源中国社区

1. _[代码][Java]代码     _跳至 [1] [全屏预览]

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

package compic;

import java.awt.image.BufferedImage;

import java.io.File;

import javax.imageio.ImageIO;

/**

* 比较两张图片的相似度

*

* @author

*

*/

public class BMPLoader {

// 改变成二进制码

public static String[][] getPX(String args) {

int`[] rgb = new int[3`];

File file = new File(args);

BufferedImage bi = null`;`

try {

bi = ImageIO.read(file);

} catch (Exception e) {

e.printStackTrace();

}

int width = bi.getWidth();

int height = bi.getHeight();

int minx = bi.getMinX();

int miny = bi.getMinY();

String[][] list = new String[width][height];

for (`int i = minx; i < width; i++) {`

for (`int j = miny; j < height; j++) {`

int pixel = bi.getRGB(i, j);

rgb[`0] = (pixel &` `0xff0000) >> 16`;

rgb[`1] = (pixel &` `0xff00) >> 8`;

rgb[`2] = (pixel &` `0xff);`

list[i][j] = rgb[`0] +` `","` `+ rgb[1] +` `","` `+ rgb[2`];

}

}

return list;

}

public static void compareImage(String imgPath1, String imgPath2) {

String[] images = { imgPath1, imgPath2 };

if (images.length == 0`) {`

System.out.println(`"Usage >java BMPLoader ImageFile.bmp"`);

System.exit(`0`);

}

// 分析图片相似度 begin

String[][] list1 = getPX(images[`0`]);

String[][] list2 = getPX(images[`1`]);

int xiangsi = 0`;`

int busi = 0`;`

int i = 0`, j = 0`;

for (String[] strings : list1) {

if ((i + 1`) == list1.length) {`

continue`;`

}

for (`int m = 0`; m < strings.length; m++) {

try {

String[] value1 = list1[i][j].toString().split(`","`);

String[] value2 = list2[i][j].toString().split(`","`);

int k = 0`;`

for (`int n = 0`; n < value2.length; n++) {

if (Math.abs(Integer.parseInt(value1[k]) - Integer.parseInt(value2[k])) < 5`) {`

xiangsi++;

} else {

busi++;

}

}

} catch (RuntimeException e) {

continue`;`

}

j++;

}

i++;

}

list1 = getPX(images[`1`]);

list2 = getPX(images[`0`]);

i = 0`;`

j = 0`;`

for (String[] strings : list1) {

if ((i + 1`) == list1.length) {`

continue`;`

}

for (`int m = 0`; m < strings.length; m++) {

try {

String[] value1 = list1[i][j].toString().split(`","`);

String[] value2 = list2[i][j].toString().split(`","`);

int k = 0`;`

for (`int n = 0`; n < value2.length; n++) {

if (Math.abs(Integer.parseInt(value1[k]) - Integer.parseInt(value2[k])) < 5`) {`

xiangsi++;

} else {

busi++;

}

}

} catch (RuntimeException e) {

continue`;`

}

j++;

}

i++;

}

String baifen = ""`;`

try {

baifen = ((Double.parseDouble(xiangsi + ""`) / Double.parseDouble((busi + xiangsi) + "")) +` `"");`

baifen = baifen.substring(baifen.indexOf(`".") +` `1, baifen.indexOf(".") + 3`);

} catch (Exception e) {

baifen = "0"`;`

}

if (baifen.length() <= 0`) {`

baifen = "0"`;`

}

if (busi == 0`) {`

baifen = "100"`;`

}

System.out.println(`"相似像素数量:" + xiangsi + " 不相似像素数量:" + busi + " 相似率:" + Integer.parseInt(baifen) + "%"`);

}

public static void main(String[] args) {

for (`int i = 1; i <=` `9; i++) {`

BMPLoader.compareImage(`"F:/img/19-21-32.jpg",` `"F:/img/"` `+ i +` `".jpg");`

}

}

}


原网址: 访问
创建于: 2018-12-29 16:48:49
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论