0

could you help me, you can tell me what I'm wrong with, my code has 2 for's that run through a whole 157x127 pixel image, from which I get black and white through RGB; in turn I have 2 counters of those two colors.

As I understand it, if the 157x127 measurement gives 19,939 pixels, of which there is black and white; but adding the counters gives me less (14218 for white and 5643 for black). Where am I wrong or is my logic wrong?

Add the image:

enter image description here

The application does not close itself

Code below:

BitmapDrawable drawable = (BitmapDrawable) imgviewREXMorphologEX.getDrawable();
Bitmap bitmap = drawable.getBitmap();

int Width = bitmap.getWidth();
int Height = bitmap.getHeight();

int contadorB = 0;
int contadorN = 0;

for (int x = 0; x <= Width - 1; x++) {
    int xi = x;
    for (int y = 0; y <= Height - 1; y++) {
        int yi = y;

        int coordenada = bitmap.getPixel(xi, yi);// pixel
        double r = Color.red(coordenada);
        double g = Color.green(coordenada);
        double b = Color.blue(coordenada);

        if (r == 255 && g == 255 && b == 255) {
            contadorB++;
        } else if (r == 0 && g == 0 && b == 0) {
            contadorN++;
        }
        txtviewcolor3.setText(contadorB + " - " + contadorN + " - " + (contadorB + contadorN) + " - " + (Width * Height));

    }
}

2 Answers2

0

Add an else clause to your if statements, and see if there are actually some other colours in the image?

Dave Atkinson
  • 46
  • 1
  • 5
0

Your image is not just black or white!

See here an amplified section (800%, bottom right):

enter image description here

There are some pixels that are gray (brighter and darker ones). Depending on purpose, some limits can help (e.g. r < MIN && g < MIN && b < 10) or using the Formula to determine brightness of RGB color compared to some limit(s)