统计像素图像素个数

统计像素图像素个数

统计像素图像素个数

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
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

//用于计算图像RGB

public class Main3 {

public static void main(String[] arg) {
//白色
int writecount = 0;
//黑色
int blackcount = 0;
//红色
int redcount = 0;
//绿色
int greencount = 0;
//蓝色
int bluecount = 0;

//总数
int count = 0;

System.out.println("开始读取图片");

File file = new File("e:\\3px.gif");

BufferedImage bi = null;
if (null != file) {

try {
bi = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}

int width = bi.getWidth();
int height = bi.getHeight();
int minx = bi.getMinX();
int miny = bi.getMinY();

int rgba1 = bi.getRGB(41, 1332);
Color mycolor1 = new Color(rgba1, true);
int red1 = mycolor1.getRed();
int green1 = mycolor1.getGreen();
int blue1 = mycolor1.getBlue();

System.out.println("mycolor1:" + mycolor1 + ",red:" + red1 + ",green:" + green1 + ",blue:" + blue1);

System.out.println("图片的宽度为:" + width + " 图片的高度为:" + height);

for (int i = minx; i < width; i++) {
for (int j = miny; j < height; j++) {
int rgba = bi.getRGB(i, j);
Color mycolor = new Color(rgba, true);
int red = mycolor.getRed();
int green = mycolor.getGreen();
int blue = mycolor.getBlue();

// System.out.println("mycolor:" + mycolor + ",red:" + red + ",green:" + green + ",blue:" + blue);

if (red >= 250 && green < 5 & blue < 5) {
redcount++;
}
if (red < 5 && green >= 250 && blue < 5) {
greencount++;
}
if (red < 100 && green < 100 && blue >= 100) {
bluecount++;
}
if (red > 250 && green > 250 && blue > 250) {
writecount++;
}
if (red < 5 && green < 5 && blue < 5) {
blackcount++;
}

count++;
}
}

System.out.println("这张图总像素点个数:" + count);

System.out.println("黑色像素点个数有:" + blackcount);

System.out.println("白色像素点个数有:" + writecount);

System.out.println("红色像素点个数有:" + redcount);

System.out.println("绿色色像素点个数有:" + greencount);

System.out.println("蓝色像素点个数有:" + bluecount);
} else {
System.out.println("未读取到图片");
}

}
}
文章作者: HibisciDai
文章链接: http://hibiscidai.com/2021/01/03/统计像素图像素个数/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 HibisciDai
好用、实惠、稳定的梯子,点击这里