如何将彩色位图图像转换为灰度图像? [英] how can i convert a colored bitmap image to grayscale image?

查看:76
本文介绍了如何将彩色位图图像转换为灰度图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何将彩色的位图图像转换为灰度图像?

how can i convert a colored bitmap image to grayscale image?

推荐答案

Google是您的朋友:很好,经常拜访他.与在这里发布问题相比,他可以更快地回答问题.

从主题行粘贴使用将彩色位图图像转换为灰度图像"进行快速搜索,获得了3/4的匹配.最受好评的是有关如何在c#...中准确执行操作的教程.

http://www.switchonthecode.com/tutorials/csharp-tutorial-将彩色图像转换为灰度 [ ^ ]

学习使用Google:比在这里发布问题要快得多.
Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A quick search using "convert a colored bitmap image to grayscale image" pasted from your subject line gave 3/4 million hits. The top hit is a tutorial on exactly how to do it in c#...

http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale[^]

Learn to use Google: it can be a lot quicker than posting a question here.


使用颜色矩阵并将其设置为图像属性.

灰度的颜色矩阵是

Use the color matrix and set it to the image attributes.

The color matrix for gray scale is

//Gilles Khouzams colour corrected grayscale shear
        ColorMatrix cm = new ColorMatrix(new float[][]{   new float[]{0.3f,0.3f,0.3f,0,0},
                                  new float[]{0.59f,0.59f,0.59f,0,0},
                                  new float[]{0.11f,0.11f,0.11f,0,0},
                                  new float[]{0,0,0,1,0,0},
                                  new float[]{0,0,0,0,1,0},
                                  new float[]{0,0,0,0,0,1}});



请参阅

http://www.bobpowell.net/grayscale.htm [



refer

http://www.bobpowell.net/grayscale.htm[^]

About 582,000 results (0.14 seconds) results in google.


首先,您必须将图像加载到内存中.如您所知,周围有很多不同的格式.根据原始图像的格式,您可能需要一个外部库来加载它.

加载后,必须遍历图像的像素并将其转换为灰度.灰度表示颜色的所有成分(R,G和B)具有相同的值.这样,您可以获得从黑色(R = 0,G = 0,B = 0)到白色(R = 255,G = 255,B = 255)的256种颜色.

对于每个像素,您必须找到亮度相似的灰度颜色值,并用它替换像素的颜色值.我只是尝试计算原始R,G和B的平均值,然后将此值用于灰度像素的所有三个分量:

R2 = G2 = B2 =(R1 + G1 + B1)/3

修改每个像素后,只需简单地再次保存图像即可.抱歉,但不知道图像的格式以及加载图像所用的内容,因此很难更准确地解释.
First, you must load the image into memory. As you know, there are quite a few different formats around. Depending on which format the original image is, you may need an external library to load it.

Once you have loaded it, you must go through the pixels of the image and convert them to grayscale. Grayscale means, that all components (R, G and B) of the colors have the same value. This way you get 256 colors from black (R = 0, G = 0, B = 0) to white (R = 255, G = 255, B = 255).

For each pixel you must find a grayscale color value with similar brightness and replace the pixel''s color value with it. I would simply try to calculate the average of the original R, G and B and use this value for all three components of the grayscale pixel:

R2 = G2 = B2 = (R1 + G1 + B1) / 3

After modifying each pixel, you simply must save the image again. Sorry, but without knowing the image''s format and what you use to load it, it''s hard to explain more precisely.


这篇关于如何将彩色位图图像转换为灰度图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆