[英]How can I properly return ArrayList<Object> from recursive method without repeated values?
[英]How can I do a method that return the Color that is more times repeated? all values of RGB that the image has
是这样的:
公共颜色colorMoreTimesRepeated(){
我不知道如何制作一个可以计算不同颜色的变量,然后将重复多次的变量返回给我。
我的想法是计算图像的所有颜色并给出重复多次的颜色,我尝试使用for的* 2旅程,当重复任何颜色时,它开始计数,最后返回一个颜色。更多重复。
count=0;
Color moreRepeated = null;
for(int i=0;i< high;i++){
for(int j=0;j<wide;j++){ *
在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");
调整Bjen的答案,以实际计算它们:
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);
}
}
您可以执行以下操作-在地图中找到最高的Count,然后从RGB返回颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.