0

次のようなものです:

public Color colorMoreTimesRepeated() {

異なる色を数えて、何度も繰り返される色を返す変数を作成する方法がわかりません。

アイデアは、画像のすべての色を数え、より多く繰り返される色を与えることです。私は *2 の旅を for で使用してみました。任意の色が繰り返されると、カウントが開始され、最後にさらに繰り返します。

    count=0;
    Color moreRepeated = null;
    for(int i=0;i< high;i++){
    for(int j=0;j<wide;j++){ *
4

2 に答える 2

0

これに対する優れた回答が Stackflow にあります。以下のコードとディスカッションへのリンクを投稿しました。

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");

https://stackoverflow.com/a/5253698/2353014

于 2013-05-05T23:55:52.387 に答える
0

Adapting Bjen's answer, to actually count them:

Map<Integer,Integer> rgbCounts = new HashMap<Color,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);
        // count it;
        Integer count = rgbCounts.get( pixel);
        rgbCounts.put( pixel, (count != null) ? count+1 : 1);
    }
}

For you to do -- find the highest Count in the map, and return the Color from RGB.

于 2013-05-06T01:33:38.433 に答える