如何在Android上应用将图像从彩色转灰度的算法?

发布于 2024-09-30 08:14:46 字数 586 浏览 0 评论 0原文

我正在尝试使用以下算法之一将 RGB 图像转换为灰度图像:

亮度方法对最突出和最不突出的颜色进行平均: (最大(R,G,B)+最小(R,G,B))/ 2。

平均方法只是对值进行平均:(R + G + B) / 3。

光度的公式为0.21 R + 0.71 G + 0.07 B。

但我得到了非常奇怪的结果!我知道还有其他方法可以实现这一目标,但是可以这样做吗?

这是代码:

for(int i = 0 ; i < eWidth*eHeight;i++){
        int R = (pixels[i] >> 16) ;     //bitwise shifting
        int G = (pixels[i] >> 8) ;
        int B = pixels[i] ;
        int gray = (R + G + B )/ 3 ;
        pixels[i] = (gray << 16) | (gray << 8) | gray   ;
}

I am trying to use one of these algorithms to convert a RGB image to grayscale:

The lightness method averages the most prominent and least prominent colors:
(max(R, G, B) + min(R, G, B)) / 2.

The average method simply averages the values: (R + G + B) / 3.

The formula for luminosity is 0.21 R + 0.71 G + 0.07 B.

But I get very weird results! I know there are other ways to acheive this but is it possible to do this way?

Here is the code:

for(int i = 0 ; i < eWidth*eHeight;i++){
        int R = (pixels[i] >> 16) ;     //bitwise shifting
        int G = (pixels[i] >> 8) ;
        int B = pixels[i] ;
        int gray = (R + G + B )/ 3 ;
        pixels[i] = (gray << 16) | (gray << 8) | gray   ;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

饮湿 2024-10-07 08:14:46

您需要去掉不属于您所获得的组件的部分,特别是当班次中存在任何符号扩展时。

    int R = (q[i] >> 16) & 0xff ;     //bitwise shifting 
    int G = (q[i] >> 8) & 0xff ; 
    int B = q[i] & 0xff ; 

You need to strip off the bits that aren't part of the component you're getting, especially if there's any sign extension going on in the shifts.

    int R = (q[i] >> 16) & 0xff ;     //bitwise shifting 
    int G = (q[i] >> 8) & 0xff ; 
    int B = q[i] & 0xff ; 
甜味拾荒者 2024-10-07 08:14:46

你所做的对我来说看起来不错..

我曾经用java以几乎相同的方式这样做过。
获取 RGB 0-255 颜色值的平均值,得到灰度,它看起来很像你的。

public int getGray(int row, int col) throws Exception
{
     checkInImage(row,col);
     int[] rgb = this.getRGB(row,col);
     return  (int) (rgb[0]+rgb[1]+rgb[2])/3;
}

What you made looks allright to me..

I once did this, in java, in much the same way.
Getting the average of the 0-255 color values of RGB, to get grayscale, and it looks alot like yours.

public int getGray(int row, int col) throws Exception
{
     checkInImage(row,col);
     int[] rgb = this.getRGB(row,col);
     return  (int) (rgb[0]+rgb[1]+rgb[2])/3;
}
纸短情长 2024-10-07 08:14:46

我知道你不是要求锄头来编码这个,而是要求算法?

根据http://www.dfanning.com/ip_tips/color2gray,没有“正确”的算法。 html

他们使用

Y = 0.3*R + 0.59*G + 0.11*B

I understand you are not asking for hoe to code this, but for algorithm?

There is no "correct" algorithm as per http://www.dfanning.com/ip_tips/color2gray.html

They use

Y = 0.3*R + 0.59*G + 0.11*B
饮惑 2024-10-07 08:14:46

您当然可以在 Java 中修改每个像素,但效率非常低。如果可以的话,我会使用 ColorMatrix。有关详细信息,请参阅 Android 文档: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/ColorMatrixSample.html

您可以将矩阵的饱和度设置为 0 以使其灰度。

如果你真的想用 Java 来做,你可以按照你做的方式来做,但是你需要先屏蔽掉每个元素,即 apply & 。 0xff 到它。

You can certainly modify each pixel in Java, but that's very inefficient. If you have the option, I would use a ColorMatrix. See the Android documentation for details: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/ColorMatrixSample.html

You could set the matrix' saturation to 0 to make it grayscale.

IF you really want to do it in Java, you can do it the way you did it, but you'll need to mask out each element first, i.e. apply & 0xff to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文