Java 计数“;redish/蓝色/绿色……”;图像中的颜色
我正在研究一种能够计算蓝/红/黄/的数字的东西。。。图像中的像素。到目前为止,我已将此代码作为测试:Java 计数“;redish/蓝色/绿色……”;图像中的颜色,java,colors,count,Java,Colors,Count,我正在研究一种能够计算蓝/红/黄/的数字的东西。。。图像中的像素。到目前为止,我已将此代码作为测试: public class Main { /* Black: 0,0,0 White: 255, 255, 255 Red: 255, 0, 0 Orange: 255, 127, 0 Yellow: 255, 255, 0 Green: 0, 255, 0 Blue: 0, 0, 255 Indigo: 111, 0, 255 Vi
public class Main {
/*
Black: 0,0,0
White: 255, 255, 255
Red: 255, 0, 0
Orange: 255, 127, 0
Yellow: 255, 255, 0
Green: 0, 255, 0
Blue: 0, 0, 255
Indigo: 111, 0, 255
Violet: 143, 0, 255
*/
static int blackCount = 0;
static int whiteCount = 0;
static int redCount = 0;
static int orangeCount = 0;
static int yellowCount = 0;
static int greenCount = 0;
static int blueCount = 0;
static int indigoCount = 0;
static int violetCount = 0;
static int otherCount = 0;
static int totalCount = 0;
public static void main(String[] args) {
try {
String path = "src/colors.jpg";
BufferedImage image = ImageIO.read(new File(path));
int w = image.getWidth();
int h = image.getHeight();
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
Color c = new Color(image.getRGB(x, y));
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();
countColor(red, green, blue);
totalCount++;
}
}
printColors();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
private static void countColor(int red, int green, int blue) {
if (red == 0 && green == 0 && blue == 0) blackCount++;
else if (red == 255 && green == 255 && blue == 255) whiteCount++;
else if (red == 255 && green == 0 && blue == 0) redCount++;
else if (red == 255 && green == 127 && blue == 0) orangeCount++;
else if (red == 255 && green == 255 && blue == 0) yellowCount++;
else if (red == 0 && green == 255 && blue == 0) greenCount++;
else if (red == 0 && green == 0 && blue == 255) blueCount++;
else if (red == 111 && green == 0 && blue == 255) indigoCount++;
else if (red == 143 && green == 0 && blue == 255) violetCount++;
else otherCount++;
}
private static void printColors() {
System.out.println("Black: " + blackCount);
System.out.println("White: " + whiteCount);
System.out.println("Red: " + redCount);
System.out.println("Orange: " + orangeCount);
System.out.println("Yellow: " + yellowCount);
System.out.println("Green: " + greenCount);
System.out.println("Blue: " + blueCount);
System.out.println("Indigo: " + indigoCount);
System.out.println("Violet: " + violetCount);
System.out.println("Other: " + otherCount);
System.out.println("Total: " + totalCount);
}
公共类主{
/*
黑色:0,0,0
白色:255,255,255
红色:255,0,0
橙色:255127,0
黄色:255,255,0
绿色:0,255,0
蓝色:0,0,255
靛蓝:111,0255
紫罗兰色:1430255
*/
静态整数黑计数=0;
静态int白计数=0;
静态整数redCount=0;
静态int-orangeCount=0;
静态计数=0;
静态整数绿计数=0;
静态整数蓝计数=0;
静态int indigoCount=0;
静态整数violetCount=0;
静态int otherCount=0;
静态整数totalCount=0;
公共静态void main(字符串[]args){
试一试{
String path=“src/colors.jpg”;
buffereImage image=ImageIO.read(新文件(路径));
int w=image.getWidth();
int h=image.getHeight();
对于(int y=0;y
但您可能会注意到问题…在RGB中,颜色“红色”定义为(255,0,0)。因此,包含大量红色的图像可能返回“0”,因为图像中使用的颜色是(254,0,0)而不是(255,0,0)
因此,我实际上不仅要计算纯红色像素,还要计算所有的“redish”像素。我认为有一种比编写一个疯狂的长if(red=255),if(red=254)更简单的方法来实现这一点……结构? < P>我会试着确定三个常数中哪一个在你的颜色中更大。如果红色的量大于蓝色的量和绿色的量,你可能认为你的颜色是“rereDe”。 蓝色和绿色也是如此 但是,您可能需要考虑一些特殊情况:
- 当颜色为“浅灰色”时,即所有三个常量都相同时,你会怎么做 如果你考虑颜色(155, 154, 154)会发生什么?红色常数是最大的,但颜色仍然是灰色的。也许你应该说颜色是一个特定的颜色,当且仅当相关常数大于所有其他加上某个值时,
我确实相信颜色是一个非常有趣的话题,但是它确实很复杂,在决定如何编写代码之前,您应该获得关于这个主题的尽可能多的信息。与其检查红色、绿色或蓝色的特定值,不如检查范围 (你必须根据语法和计算来调整它——我不是Java程序员) 专用静态void countColor(整数红色、整数绿色、整数蓝色){ 如果(红色==0&&绿色==0&&蓝色==0)blackCount++; 如果(红色!=0&&green==red&&blue==red)白计数++; 如果(红色!=0&&绿色==0&&蓝色==0)红色计数++; 如果(红色!=0和绿色==(红色/2)和蓝色==0)orangeCount++; 如果(红色!=0和绿色==红色和蓝色==0)yellowCount++; 如果(红色==0和绿色==!=0和蓝色==0)绿色计数++; 如果(红色==0&&绿色==0&&蓝色==!=0)blueCount++; 如果(红色==(蓝色/3)和绿色==0和蓝色!=0)靛蓝计数++; 否则,如果(红色==(蓝色/0.66)和绿色==0和蓝色=!=)violetCount++; else-otherCount++; }
如果这样做有效,它将检查与红色、绿色、蓝色、白色、黑色等色调匹配的颜色。您可以将正在处理的像素转换为更方便的颜色模型,即 然后 private static void countColor(int red, int green, int blue) { if (red == 0 && green == 0 && blue == 0) blackCount++; else if (red !=0 && green == red && blue == red) whiteCount++; else if (red !=0 && green == 0 && blue == 0) redCount++; else if (red !=0 && green == (red/2) && blue == 0) orangeCount++; else if (red !=0 && green == red && blue == 0) yellowCount++; else if (red == 0 && green == !=0 && blue == 0) greenCount++; else if (red == 0 && green == 0 && blue == !=0) blueCount++; else if (red == (blue/3) && green == 0 && blue !=0) indigoCount++; else if (red == (blue/0.66) && green == 0 && blue == !=) violetCount++; else otherCount++; }