将图像转换为灰度
有没有办法将图像转换为每像素 16 位灰度格式,而不是将每个 r、g 和 b 分量设置为亮度。我目前有一个 bmp 文件。
Bitmap c = new Bitmap("filename");
我想要一个位图 d,即 c 的灰度版本。我确实看到一个包含 System.Drawing.Imaging.PixelFormat 的构造函数,但我不明白如何使用它。我是图像处理和相关 C# 库的新手,但对 C# 本身有一定的经验。
任何帮助、在线资源参考、提示或建议将不胜感激。
编辑:d 是 c 的灰度版本。
Is there a way to convert an image to grayscale 16 bits per pixel format, rather than setting each of the r,g and b components to luminance. I currently have a bmp from file.
Bitmap c = new Bitmap("filename");
I want a Bitmap d, that is grayscale version of c. I do see a constructor that includes System.Drawing.Imaging.PixelFormat, but I don't understand how to use that. I'm new to Image Processing and the relevant C# libraries, but have a moderate experience with C# itself.
Any help, reference to an online source, hint or suggestion will be appreciated.
EDIT: d is the grayscale version of c.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是如何做到这一点
编辑:
从switchonthecode 请点击链接进行完整分析:
Here is how to do this
EDIT: To convert to grayscale
Faster Version from switchonthecode follow link for full analysis:
这样它也保留了 Alpha 通道。
享受。
This way it also keeps the alpha channel.
Enjoy.
ToolStripRenderer
类中有一个名为CreateDisabledImage
的静态方法。它的用法很简单:
它使用与接受的答案中的矩阵稍有不同的矩阵,并另外将其乘以透明度值 0.7,因此效果与灰度略有不同,但如果您只想获得图像变灰,这是最简单也是最好的解决方案。
There's a static method in
ToolStripRenderer
class, namedCreateDisabledImage
.Its usage is as simple as:
It uses a little bit different matrix than the one in the accepted answer and additionally multiplies it by a transparency of value 0.7, so the effect is slightly different than just grayscale, but if you want to just get your image grayed, it's the simplest and best solution.
下面的代码是最简单的解决方案:
The code below is the simplest solution:
上述示例均未创建 8 位 (8bpp) 位图图像。有些软件,例如图像处理,仅支持8bpp。不幸的是,MS .NET 库没有解决方案。 PixelFormat.Format8bppIndexed 格式看起来很有前途,但经过多次尝试后我无法让它工作。
要创建真正的 8 位位图文件,您需要创建正确的标头。最终我找到了用于创建 8 位位图 (BMP) 的 灰度库 解决方案文件。代码非常简单:
这个项目的代码远非漂亮,但它可以工作,有一个易于修复的小问题。作者将图像分辨率硬编码为10x10。图像处理程序不喜欢这样。修复方法是打开 GrayBMP_File.cs(是的,我知道,文件命名很时髦)并将第 50 行和第 51 行替换为下面的代码。该示例将分辨率设置为 200x200,但您应该将其更改为正确的数字。
None of the examples above create 8-bit (8bpp) bitmap images. Some software, such as image processing, only supports 8bpp. Unfortunately the MS .NET libraries do not have a solution. The PixelFormat.Format8bppIndexed format looks promising but after a lot of attempts I couldn't get it working.
To create a true 8-bit bitmap file you need to create the proper headers. Ultimately I found the Grayscale library solution for creating 8-bit bitmap (BMP) files. The code is very simple:
The code for this project is far from pretty but it works, with one little simple-to-fix problem. The author hard-coded the image resolution to 10x10. Image processing programs do not like this. The fix is open GrayBMP_File.cs (yeah, funky file naming, I know) and replace lines 50 and 51 with the code below. The example sets the resolution to 200x200 but you should change it to the proper number.
在此总结几项:
有一些逐像素选项虽然简单,但速度不快。
@Luis 的评论链接到:(已存档)https://web.archive.org/web/20110827032809/http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image - 到灰度 非常棒。
他介绍了三个不同的选项,并给出了每个选项的时间安排。
To summarize a few items here:
There are some pixel-by-pixel options that, while being simple just aren't fast.
@Luis' comment linking to: (archived) https://web.archive.org/web/20110827032809/http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale is superb.
He runs through three different options and includes timings for each.
我认为在这种情况下这是一个好方法:
享受......
I think it's a good way in this case:
Enjoy...