首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >每个像素的强度为0..255?

每个像素的强度为0..255?
EN

Stack Overflow用户
提问于 2011-07-06 20:49:14
回答 2查看 2.5K关注 0票数 3

我有黑白图片- RGB 565,200x50。因为我可以计算出每个像素的强度0..255?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-06 21:23:22

您可以尝试使用具有一个亮度和两个色度分量的颜色模型对其进行转换。亮度分量表示亮度,而两个色度分量表示颜色。你可能想看看http://en.wikipedia.org/wiki/YUV

否则:如果我是正确的,从灰色到黑色的白色在RGB格式中具有相同的值,每个通道具有相同的位数(例如,从(0,0,0)到(255,255,255))。假设这是真的,你可以只取其中一个通道来表示强度,因为你可以从中确定其他值。不能保证这是否有效。

编辑:我写了一个演示上述想法的代码片段。我使用的是RGB888,但在删除断言并按照注释中的说明修改像素的最大强度后,它也应该适用于RGB565。请注意,每个像素只有2^5个不同的强度级别。因此,您可能希望使用平均强度的缩放版本。

我使用来自http://www.smashingmagazine.com/2008/06/09/beautiful-black-and-white-photography/的图片对其进行了测试。我希望它能帮你把这个移植到android上。

代码语言:javascript
运行
复制
// 2^5 for RGB 565
private static final int MAX_INTENSITY = (int) Math.pow(2, 8) - 1;

public static int calculateIntensityAverage(final BufferedImage image) {
    long intensitySum = 0;
    final int[] colors = image.getRGB(0, 0, image.getWidth(),
            image.getHeight(), null, 0, image.getWidth());
    for (int i = 0; i < colors.length; i++) {
        intensitySum += intensityLevel(colors[i]);
    }
    final int intensityAverage = (int) (intensitySum / colors.length);
    return intensityAverage;
}

public static int intensityLevel(final int color) {
    // also see Color#getRed(), #getBlue() and #getGreen()
    final int red = (color >> 16) & 0xFF;
    final int blue = (color >> 0) & 0xFF;
    final int green = (color >> 8) & 0xFF;
    assert red == blue && green == blue; // doesn't hold for green in RGB 565
    return MAX_INTENSITY - blue;
}
票数 0
EN

Stack Overflow用户

发布于 2011-07-12 22:04:40

我就是这个意思,谢谢。妈妈,这是谁能帮上忙。我得到每个像素的强度0..255,然后得到平均值。

代码语言:javascript
运行
复制
     Bitmap cropped = Bitmap.createBitmap(myImage, 503, 270,myImage.getWidth() - 955,            myImage.getHeight() - 550);



     Bitmap cropped2 = Bitmap.createBitmap(cropped, 0, 0,cropped.getWidth() , cropped.getHeight() / 2 );



 final double GS_RED = 0.35;

 final double GS_GREEN = 0.55;

 final double GS_BLUE = 0.1;

 int  R, G, B;
 int result = 0;
 int g = 0;
 int ff;
     for(int x = 0; x < cropped2.getWidth(); x++) 
    {   
        int ff_y = 0;      
         for(int y = 0; y < cropped2.getHeight(); y++) 
        {             
             Pixel = cropped.getPixel(x, y);      

             R = Color.red(Pixel);

             G = Color.green(Pixel);

             B = Color.blue(Pixel);        

             ff = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B) ;
             ff_y += ff;

        }
        result += ff_y;
        g = result / (cropped2.getWidth()*cropped2.getHeight());

    }
   Toast.makeText(this, "00" +  g, Toast.LENGTH_LONG).show();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6596623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档