shisushi
shisushi

Reputation: 225

Android - change the color of the pixel from photo

I would like to change all the color except black to white from the photo captured by the camera. However, the black color of the object changes with the light and white balance, it cannot be detected as black and then also changed to white

public void subColor(Mat src, String timeStamp, File mediaStorageDir) throws FileNotFoundException {
    Bitmap output = Bitmap.createBitmap(src.width(), src.height(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(src, output);

    for (int x = 0; x < output.getWidth(); x++)
        for (int y = 0; y < output.getHeight(); y++) {
            int pixel = output.getPixel(x, y);
            if (pixel != Color.BLACK)
                output.setPixel(x, y, Color.WHITE);
        }

    String mImageName= timeStamp + "_EDIT" + ".jpg";
    File mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
    FileOutputStream fos = new FileOutputStream(mediaFile);
    output.compress(Bitmap.CompressFormat.JPEG, 100, fos);
}

I would like to know if the color of pixel can compare with the range of black instead of Color.BLACK.

Thank you.

Upvotes: 4

Views: 210

Answers (2)

Vladimir Jovanović
Vladimir Jovanović

Reputation: 2267

Black doesn't have range. Black is just one. If you want to see if something is darker than some other color you can do something like this:

if(Color.red(myColor) < 20 &&
     Color.blue(myColor) < 20 && 
     Color.green(myColor) < 20){
    ...
 }

where color you want to compare it with is #202020. If you want to be sure that color is gray add Color.red(myColor) == Color.blue(myColor) && Color.green(myColor) == Color.blue(myColor)

Upvotes: 0

Ashlesha Sharma
Ashlesha Sharma

Reputation: 949

Hope following steps could help you: 1. compute RGB from pixel like:

int r = (pixel >> 16) & 0xff;  
int g = (pixel >>  8) & 0xff; 
int b = (pixel >>  0) & 0xff; 

2.convert the RGB color value to compute luminance by the following formula

Y = 0.2126*r + 0.7152*g + 0.0722*b
  1. check if it is a shade of Black or not like:

    if( Y < 128){ //can be taken as shade of black }else{ //can be taken as shade of white }

Upvotes: 1

Related Questions