繁体   English   中英

如何获得像素的颜色?

[英]How to get the color of a pixel?

我必须使用Android Studio和OpenCV获得图像中最主要(最常见)的颜色,因此我试图遍历图像中的每个像素并获得其颜色。 然后,我计算图像中每种颜色(红色,绿色,蓝色,黄色,其他)的像素数量,并获得最大颜色(像素最多的像素)并打印其第一个字母(r-红色,g-绿色,y-黄色,b-蓝色,x-其他)。

我的代码可以运行,但是不会产生所需的输出。 这是我的方法:

            int color = imageBitmap.getPixel(j, i);

            if(color == Color.RED){
                red++;
            }else if(color == Color.GREEN){
                green++;
            }else if(color == Color.YELLOW){
                yellow++;
            }else if(color == Color.BLUE){
                blue++;
            }else{
                other++;
            }

另外,我已经尝试过(首先将RGB图像转换为HSV),但它也不会产生所需的输出:

            if(hue > 340 || hue < 20){
                red++;
            }
            else if(hue > 20 && hue < 45){
                orange++;
            }
            else if(hue > 45 && hue < 70){
                yellow++;
            }
            else if(hue > 90 && hue < 140){
                green++;
            }
            else{
                other++;
            }

有什么想法可以解决这个问题吗?

解决方案:我终于做到了,这是我的解决方案(感谢Shiva kumar):

    int red = 0;
    int green = 0;
    int yellow = 0;
    int orange = 0;
    int other = 0;

    Bitmap newBitmap = Bitmap.createScaledBitmap(imageBitmap, 1, 1, true);
    final int color = newBitmap.getPixel(0, 0);
    newBitmap.recycle();

    int redValue = Color.red(color);
    int greenValue = Color.green(color);
    int blueValue = Color.blue(color);

    float[] hsv = new float[3];
    Color.RGBToHSV(redValue, greenValue, blueValue, hsv);

    float hue = hsv[0];
    float saturation = hsv[1];
    float value = hsv[2];

    if(hue > 340 || hue < 20){
        red++;
    }
    else if(hue > 20 && hue < 45){
        orange++;
    }
    else if(hue > 45 && hue < 70){
        yellow++;
    }
    else if(hue > 90 && hue < 140){
        green++;
    }
    else{
        other++;
    }

int像素= bitmap.getPixel(x,y);

int redValue = Color.red(pixel);

int blueValue = Color.blue(pixel);

int greenValue = Color.green(pixel);

使用该rgb值尝试获得颜色!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM