我怎样才能获得的图像的平均颜色

我怎样才能获得的图像的平均颜色

本文介绍了我怎样才能获得的图像的平均颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够拍摄图像,并找出什么是平均颜色。这意味着如果图像是半黑半白,我会得到介于两者之间......灰色一些树荫。它可能是最常见的单一颜色或中位数。不限中等就可以了。

我怎么能做到这一点的机器人。

解决方案

 位图位= someFunctionReturningABitmap();
长redBucket = 0;
长greenBucket = 0;
长blueBucket = 0;
长pixelCount = 0;

对于(INT Y = 0; Y< bitmap.getHeight(); Y ++)
{
    为(中间体X = 0 X  - 其中; bitmap.getWidth(); X ++)
    {
        颜色为c = bitmap.getPixel(X,Y);

        pixelCount ++;
        redBucket + = Color.red(C);
        greenBucket + = Color.green(C);
        blueBucket + = Color.blue(C);
        //做阿尔法事?
    }
}

颜色averageColor = Color.rgb(redBucket / pixelCount,
                                greenBucket / pixelCount,
                                blueBucket / pixelCount);
 

I want to be able to take an image and find out what is the average colour. meaning if the image is half black half white, I would get something in between... some shade of gray. It could be the most frequent single colour or median. Any average will do.

How can I do this in android.

解决方案
Bitmap bitmap = someFunctionReturningABitmap();
long redBucket = 0;
long greenBucket = 0;
long blueBucket = 0;
long pixelCount = 0;

for (int y = 0; y < bitmap.getHeight(); y++)
{
    for (int x = 0; x < bitmap.getWidth(); x++)
    {
        Color c = bitmap.getPixel(x, y);

        pixelCount++;
        redBucket += Color.red(c);
        greenBucket += Color.green(c);
        blueBucket += Color.blue(c);
        // does alpha matter?
    }
}

Color averageColor = Color.rgb(redBucket / pixelCount,
                                greenBucket / pixelCount,
                                blueBucket / pixelCount);

这篇关于我怎样才能获得的图像的平均颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 05:42