【问题标题】:Average color of bottom 30% of an image图像底部 30% 的平均颜色
【发布时间】:2013-06-04 07:14:24
【问题描述】:

我正在尝试编写一种方法来获取图像底部 30% 的平均颜色。我试图通过单独检查每个像素,获取它们的颜色,将它们求和并将结果除以我检查的像素数量来做到这一点。我的代码是:

int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
int pixel;
int pixelSumRed = 0;
int pixelSumBlue = 0;
int pixelSumGreen = 0;
for (int i = 0; i < 100; i++) {
    for (int j = 70; j < 100; j++) {
        pixel = image.getPixel((int) Math.round((i/100)*imageWidth), (int) Math.round((j/100)*imageHeight));
        pixelSumRed += Color.red(pixel);
        pixelSumBlue += Color.blue(pixel);
        pixelSumGreen += Color.green(pixel);
        Log.d("Checks", "Pixel " + i + ", " + j + " red: " + Color.red(pixel) + ", green: " + Color.green(pixel) + ", blue: " + Color.blue(pixel));
        }
    }

averagePixelRed = pixelSumRed / 3000;
averagePixelBlue = pixelSumBlue / 3000;
averagePixelGreen = pixelSumGreen / 3000;

现在,我注意到每个像素都打印了相同的颜色,因此对于一个图像,每个像素的 RGB = 210、44、70,而对于另一个图像,我将获得 RGB = 12、0、90对于每个像素。显然出了点问题,但我找不到它是什么。希望大家帮帮我。

【问题讨论】:

    标签: android image colors pixel


    【解决方案1】:

    整数除法的奇迹!当您将 i 或 j 除以 100 时,它以整数除法完成,并且向下舍入 - 每次您都会得到 0。您可以重新订购它:

    (i * imageWidth) / 100
    

    【讨论】:

      猜你喜欢
      • 2012-07-28
      • 2015-06-05
      • 1970-01-01
      • 2019-09-17
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      • 2014-02-15
      • 2011-02-02
      相关资源
      最近更新 更多