Java 计算图像的颜色数

Java 计算图像的颜色数,java,image,colors,Java,Image,Colors,我有三种不同的图像(jpeg或bmp)。 我试图根据每个图像的颜色数量来预测每个图像的复杂性。 我怎样才能使Java成为可能? 多谢各位 更新: 这些代码不起作用。。输出显示1312种颜色,即使它只有纯红色和白色 import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.util.ArrayList; import javax.imageio.ImageIO; public cl

我有三种不同的图像(jpeg或bmp)。 我试图根据每个图像的颜色数量来预测每个图像的复杂性。 我怎样才能使Java成为可能? 多谢各位

更新: 这些代码不起作用。。输出显示1312种颜色,即使它只有纯红色和白色

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;

import javax.imageio.ImageIO;

public class clutters {
    public static void main(String[] args) throws IOException {

        ArrayList<Color> colors = new ArrayList<Color>();

        BufferedImage image = ImageIO.read(new File("1L.jpg"));    
        int w = image.getWidth();
        int h = image.getHeight();
        for(int y = 0; y < h; y++) {
            for(int x = 0; x < w; x++) {
                int pixel = image.getRGB(x, y);     
                int red   = (pixel & 0x00ff0000) >> 16;
                int green = (pixel & 0x0000ff00) >> 8;
                int blue  =  pixel & 0x000000ff;                    
                Color color = new Color(red,green,blue);     

                //add the first color on array
                if(colors.size()==0)                
                    colors.add(color);          
                //check for redudancy
                else {
                    if(!(colors.contains(color)))
                        colors.add(color);
                }
            }
        }
system.out.printly("There are "+colors.size()+"colors");
    }
}
import java.awt.*;
导入java.awt.image.buffereImage;
导入java.io.*;
导入java.util.ArrayList;
导入javax.imageio.imageio;
公共类杂波{
公共静态void main(字符串[]args)引发IOException{
ArrayList colors=新的ArrayList();
buffereImage image=ImageIO.read(新文件(“1L.jpg”);
int w=image.getWidth();
int h=image.getHeight();
对于(int y=0;y>16;
int绿色=(像素&0x0000ff00)>>8;
int蓝色=像素&0x000000ff;
颜色=新颜色(红色、绿色、蓝色);
//在数组中添加第一种颜色
如果(colors.size()==0)
颜色。添加(颜色);
//检查是否有重复
否则{
如果(!(colors.contains(color)))
颜色。添加(颜色);
}
}
}
system.out.printly(“有”+colors.size()+颜色”);
}
}


具有可能有用的getRGB方法。但正如你在这节课上看到的。计算颜色不是一件小事,因为有各种颜色编码,也有alpha通道需要处理。

代码基本正确,但太复杂了。您可以简单地使用
Set
并将
int
值添加到其中,因为现有值将被忽略。您也不需要计算每种颜色的RGB值,因为
getRGB
返回的
int
值本身是唯一的:

BufferedImage bi=ImageIO.read(...);
bi.getColorModel().getRGB(...);
Set<Integer> colors = new HashSet<Integer>();
    BufferedImage image = ImageIO.read(new File("test.png"));    
    int w = image.getWidth();
    int h = image.getHeight();
    for(int y = 0; y < h; y++) {
        for(int x = 0; x < w; x++) {
            int pixel = image.getRGB(x, y);     
            colors.add(pixel);
        }
    }
    System.out.println("There are "+colors.size()+" colors");
Set colors=new HashSet();
BuffereImage image=ImageIO.read(新文件(“test.png”);
int w=image.getWidth();
int h=image.getHeight();
对于(int y=0;y
你得到的“奇怪”颜色数是由于图像压缩(在你的例子中是JPEG)和其他原因,比如图像编辑软件的抗锯齿。即使只绘制红色和白色,结果图像的边缘上这两个值之间也可能包含大量颜色


这意味着代码将返回特定图像中使用的颜色的实际计数。您可能还想了解不同的图像文件格式以及无损和有损压缩算法。

灰度图像(仅256色)是否天生比多达65536色的彩色图像复杂?您试图构建的是直方图。