【问题标题】:How to display histogram of RGB values of an image?如何显示图像的 RGB 值的直方图?
【发布时间】:2013-08-08 16:56:32
【问题描述】:

我的应用程序捕获图像并对其应用过滤器以修改图像 RGB 值。

修改后,我希望在图像本身的顶部显示每种颜色(红、绿、蓝)的直方图。

我已经知道如何获取 RGB 值并且我已经知道如何获取位图,只是不知道如何绘制它们。

RGB 值代码:

    int[] pixels = new int[width*height];
    int index = 0;
    image.getPixels(pixels, 0, width, 0, 0, width, height);
    Bitmap returnBitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            A = (pixels[index] >> 24) & 0xFF;
            R = (pixels[index] >> 16) & 0xFF;
            G = (pixels[index] >> 8) & 0xFF;
            B = pixels[index] & 0xFF;
                            ++index;

                     }
            }

【问题讨论】:

    标签: java android image graph histogram


    【解决方案1】:

    我们做过类似的事情。我们得到了图像的位图:

    Bitmap bmp = BitmapFactory.decodeResource(<youImageView>.getResources(), R.drawable.some_drawable);
    

    然后我们遍历每个像素并使用以下代码来获取像素的颜色:

    int color = bmp.getPixel(i, j);
    int[] rgbValues = new int[]{
                    (color >> 16) & 0xff, //red
                    (color >>  8) & 0xff, //green
                    (color      ) & 0xff  //blue
                };
    

    编辑:
    我刚刚读到你也可以通过使用这个 insead 来获得不透明度:

    int color = bmp.getPixel(i, j);
    int[] rgbValues = new int[]{
                    (color >> 24) & 0xff, //alpha
                    (color >> 16) & 0xff, //red
                    (color >>  8) & 0xff, //green
                    (color      ) & 0xff  //blue
                };
    

    如果您已经有了这些值,我建议您 androidplot 创建图表。有一些示例使其易于使用。我没有使用条形图,但折线图效果很好。 Here 是用于 androidplot 的 BarCharts 示例。
    我只想总结不同的值,然后(如果你愿意的话)对其进行规范化。


    要最终显示图表,您可以将布局创建为 FrameLayout,然后this 可能会帮助您处理 z 顺序。您现在唯一需要做的就是显示/隐藏包含您的图表的布局部分。 (View.setVisibility)

    【讨论】:

    • 嗨,看我更新的问题,我已经知道如何获取 RGB 值,只是不知道如何根据捕获的图像绘制它们
    • 嘿,请看我编辑的答案版本。希望这会有所帮助。
    猜你喜欢
    • 2013-01-18
    • 1970-01-01
    • 2012-04-22
    • 2018-04-04
    • 2016-11-10
    • 2015-04-15
    • 2012-01-02
    • 2013-03-09
    相关资源
    最近更新 更多