【问题标题】:Android - change the color of the pixel from photoAndroid - 从照片中更改像素的颜色
【发布时间】:2016-04-18 16:32:59
【问题描述】:

我想将相机拍摄的照片中除黑色以外的所有颜色更改为白色。但是物体的黑色随着光线和白平衡的变化而变化,不能被检测为黑色,然后也变成了白色

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);
}

我想知道像素的颜色是否可以与黑色而不是Color.BLACK的范围进行比较。

谢谢。

【问题讨论】:

  • 注意:获取int pixel = output.getPixel(x, y)的像素应该来自源位图而不是输出,即正确:int pixel = src.getPixel(x, y)

标签: android image-processing


【解决方案1】:

希望以下步骤可以帮助您: 1. 从像素计算 RGB,如:

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

2.通过以下公式将RGB颜色值转换为亮度计算

Y = 0.2126*r + 0.7152*g + 0.0722*b
  1. 检查它是否是黑色的阴影:

    如果(Y

【讨论】:

  • 请您解释一下您是如何获得这些硬编码十进制值的或指向描述它的此类项目的链接?
【解决方案2】:

黑方没有射程。黑色只是其中之一。如果您想查看某些颜色是否比其他颜色深,您可以执行以下操作:

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

您要与之比较的颜色是#202020。 如果您想确保颜色为灰色,请添加Color.red(myColor) == Color.blue(myColor) &amp;&amp; Color.green(myColor) == Color.blue(myColor)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 2017-09-26
    相关资源
    最近更新 更多