gpt4 book ai didi

c# - 什么是好的 TRUE 黑白颜色矩阵?

转载 作者:可可西里 更新时间:2023-11-01 07:51:25 26 4
gpt4 key购买 nike

我想将图像从彩色转换为黑白(即没有灰度,只有黑白)。有没有人有一个很好的颜色矩阵来实现这个?

最佳答案

我终于找到了解决问题的方法:

  1. 使用众所周知的颜色矩阵将图像转换为灰度。
  2. 使用 ImageAttributes 类的 SetThreshold 方法来设置将黑色与白色分开的阈值。

这是 C# 代码:

using (Graphics gr = Graphics.FromImage(SourceImage)) // SourceImage is a Bitmap object
{
var gray_matrix = new float[][] {
new float[] { 0.299f, 0.299f, 0.299f, 0, 0 },
new float[] { 0.587f, 0.587f, 0.587f, 0, 0 },
new float[] { 0.114f, 0.114f, 0.114f, 0, 0 },
new float[] { 0, 0, 0, 1, 0 },
new float[] { 0, 0, 0, 0, 1 }
};

var ia = new System.Drawing.Imaging.ImageAttributes();
ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(gray_matrix));
ia.SetThreshold(0.8); // Change this threshold as needed
var rc = new Rectangle(0, 0, SourceImage.Width, SourceImage.Height);
gr.DrawImage(SourceImage, rc, 0, 0, SourceImage.Width, SourceImage.Height, GraphicsUnit.Pixel, ia);
}

我已经对这段代码进行了基准测试,它比逐像素操作快大约 40 倍。

关于c# - 什么是好的 TRUE 黑白颜色矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2746103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com