0

I've an image and its pixels are in grid blocks. each pixel occupy 20x20px block of a grid . here is the image

enter image description here

I want to read color of each block of that grid. Here is the code which i tried.

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.abc);
    for(int y=0; y<bmp.getHeight(); y=y+20){
      for(int x=0; x<bmp.getWidth(); x=x+20){  
        pixelColor = bmp.getPixel(x,y);
    }
      }

The problem is now that, colors which are being read are of very slight difference and in result it is picking too many colors. For example in black color case it picks almost 10 black colors which slightly varies from each other. Please help me to pick all unique colors. any help would be much appreciated. Thank you

Tashen Jazbi
  • 1,068
  • 1
  • 16
  • 41

2 Answers2

1

I've finally figured out myself by using Palette class in android

I've used its nested class Palette.Swatch to get all color swatches in the image. here is how i did this

ArrayList<Integer> color_vib = new ArrayList<Integer>();
Bitmap bitmap = BitmapFactory.decodeResource( getResources(), R.drawable.abc );
Palette.from( bitmap ).generate( new Palette.PaletteAsyncListener() {
  @Override
     public void onGenerated( Palette palette ) {
            //work with the palette here
             for( Palette.Swatch swatch : palette.getSwatches() ) {
                    color_vib.add( swatch.getRgb() );
                }
        }
    });

Now I've all unique colors of any blocky pixelated image :)

Tashen Jazbi
  • 1,068
  • 1
  • 16
  • 41
0

This is an issue with device density. You should put different size image for different density devices. Since your image read more than the actual width & height. That cause the problem to overload your loop to more than the actual pixels.

Have a look https://developer.android.com/guide/practices/screens_support.html#qualifiers to see different density devices to put various drawable. This will need if you need to display it in UI.

Otherwise if you don't want image to display it in UI. Put image in drawable-nodpi folder & get height,width it will return correct results.

See this Question & am referred from this Solution

UPDATED:

    ImageView imgTop = (ImageView) findViewById(R.id.imgTop);
    ImageView imgBtm = (ImageView) findViewById(R.id.imgBtm);

    Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.pixel);

    Bitmap output = Bitmap.createBitmap(bmp1.getWidth(),bmp1.getHeight(), Bitmap.Config.ARGB_8888);

    int count = 0;
    int[] pixelColor = new int[bmp1.getHeight() * bmp1.getHeight()];
    for(int y=0; y<bmp1.getHeight(); y++){
        for(int x=0; x<bmp1.getWidth(); x++){
           pixelColor[count] = bmp1.getPixel(x,y);

            if(Color.red(pixelColor[count]) <= 30 && Color.green(pixelColor[count]) <= 30 && Color.blue(pixelColor[count]) <= 30)
            {
                pixelColor[count] = Color.BLACK;
            }
            else
            {
               //pixelColor[count] contain other colours..
            }
            output.setPixel(x,y,pixelColor[count]);
            count++;
        }
    }

    imgTop.setImageBitmap(bmp1);
    imgBtm.setImageBitmap(output);

OUTPUT

enter image description here

Mohamed Mohaideen AH
  • 2,527
  • 1
  • 16
  • 24
  • thank you for your answer, it is very much appreciated :) .Dear the problem is not with extra pixels , the problem is with extra colors of same image. For example if i take only a black color image, my code will show a lot of colors which are black and only slight different from others in terms of RBG value. I want only that black prominent color . – Tashen Jazbi Jan 17 '18 at 10:36
  • @TashenJazbi Your color code look like black pixel is slightly different that is what you got with different colour codes. i checked the first 4 diagonal pixel even its different. – Mohamed Mohaideen AH Jan 17 '18 at 14:19
  • yes it is different but all slight variation in black color must be read as black. – Tashen Jazbi Jan 18 '18 at 06:50
  • Slight variation to black cannot read as black but we can do ourself to read each pixel and change to black colour if its close to black. Check my updated answer. – Mohamed Mohaideen AH Jan 18 '18 at 08:14
  • Thank you brother, you code will read only black color and in picture there are other colors too. There could be such colors where we can not use "Color.othercolor " condition because they do not exist in Color class. – Tashen Jazbi Jan 18 '18 at 09:21
  • You can get other colours of image in else condition of if loop. And do whatever you need. – Mohamed Mohaideen AH Jan 18 '18 at 09:44
  • dude, if else condition will appear where we'll know actual color. In that case we don't know the major color, even after finding major color manually we don't know whether that color will be in Color class as text or not. – Tashen Jazbi Jan 18 '18 at 12:54
  • You need something all slightly different colors combined to major value of colour. Am i right? Then if you find manually other color you can use `Color.rgb(20, 20, 20);` method to change to unique. Otherwise explain your expected output. – Mohamed Mohaideen AH Jan 18 '18 at 13:03
  • Thanks dude, I've got all unique colors by using Palette class of android. thank you for your participation, it is very much appreciated :) – Tashen Jazbi Jan 18 '18 at 13:08