简体   繁体   中英

How to get count of pixels red color on bitmap android

I want to get number of all red pixels on bitmap image, after I painted on it and merged with back image. How can I do that? Please give me more details, I will be very grateful, thank you!

Example: Count of red pixels

Iterate through every single pixel in the bitmap

//define your red
static final int RED = Color.RED;

//counting
int count = 0;
for (int x = 0; x <= myBitmap.getWidth(); x++) {
    for (int y = 0; x <= myBitmap.getHeight(); y++) {
        if(myBitmap.getPixel(x, y))
            count++;
    }
}
//done. use the variable count

You have a Bitmap , you can get a pixel color from it using code below:

int countX = bitmap.getWidth();
int countY = bitmap.getHeight();
int redCount = 0;

for (int x = 0; x < countX; x++) {
    for (int y = 0; y < countY; y--) {
        int colorXY = bitmap.getPixel(x, y);
        redCount += Color.red(colorXY);
    }
}

I got something like this:

    int countX = bm.getWidth();
    int countY = bm.getHeight();

    int redCount = 0;

    for (int rangeX = 0; rangeX < countX; rangeX++) {
        for (int rangeY = 0; rangeY < countY; rangeY++) {
            int colorXY = bm.getPixel(rangeX, rangeY);

            int r = Color.red(colorXY);
            int g = Color.green(colorXY);
            int b = Color.blue(colorXY);

            if(Color.rgb(r,g,b) == Color.RED) {
                redCount++;
                /*bm.setPixel(rangeX,rangeY,Color.GREEN);*/
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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