简体   繁体   English

在C#.NET和Windows窗体中将照片转换为灰度

[英]Photo to grayscale in C#.NET and Windows Forms

How do I make this into a grayscale? 如何将其变成灰度?

After much googling it seems the best I can do is change the tint of the picture (in the code bellow, it's a green tint). 经过大量的谷歌搜索之后,似乎我能做的最好的就是改变图片的色彩(在下面的代码中,这是绿色的色彩)。 How can I do it? 我该怎么做?

byte[] redGreenBlueVal = new byte[numBytes];

for (int i = 0; i < redGreenBlueVal .Length; i += 4)
{
    redGreenBlueVal [i + 0] = (byte)(.114 * redGreenBlueVal [i + 0]); --> blue
    redGreenBlueVal [i + 1] = (byte)(.587 * redGreenBlueVal [i + 1]); --> green
    redGreenBlueVal [i + 2] = (byte)(.299 * redGreenBlueVal [i + 2]); --> red
}

In effect you're adjusting HSB so this should get you a better grayscale image: 30% RED 59% GREEN 11% BLUE 实际上,您正在调整HSB,这样可以为您提供更好的灰度图像:30%红色59%绿色11%蓝色

byte[] redGreenBlueVal = new byte[numBytes];

for (int i = 0; i < redGreenBlueVal .Length; i += 4)
{
      gray = (byte)(.11 * redGreenBlueVal [i + 0]);
      gray += (byte)(.59 * redGreenBlueVal [i + 1]); 
      gray += (byte)(.3 * redGreenBlueVal [i + 2]);

      redGreenBlueVal [i + 0] = gray;
      redGreenBlueVal [i + 1] = gray;
      redGreenBlueVal [i + 2] = gray;

}

You could try setting each pixels colour channels to the average value for that pixel. 您可以尝试将每个像素的颜色通道设置为该像素的平均值。

ie

for( int i = 0; i < redGreenBlueVal.Length; += 4 )
{
    int average = (redGreenBlueVal[i + 0] + redGreenBlueVal[i + 1] + redGreenBlueVal[i + 2])/3;
    redGreenBlueVal[i + 0] = average;
    redGreenBlueVal[i + 1] = average;
    redGreenBlueVal[i + 2] = average;
}

To get a grayscale image, the basic idea is to have all the 3 channels hold the same value. 为了获得灰度图像,基本思想是使所有3个通道保持相同的值。 There are many ways to do this, the simplest ones are: 有很多方法可以做到这一点,最简单的方法是:

  • use only one of the channels (it will look like when you select individual channels in photoshop) 仅使用其中一个通道(当您在photoshop中选择单个通道时,它看起来像)
  • calculate an average of the 3 channels 计算三个通道的平均值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#.NET-如何从现有的Windows窗体控件继承 - C#.NET - How to inherit from an existing windows forms control 如何重新调整图像C#.net Windows窗体的大小 - How To Re-size An Image C#.net Windows Forms C#.net,Windows 手机 - C#.net, Windows phone c#.net 2005中的窗体上未显示控件 - controls not shown on forms in c#.net 2005 C#.NET-Type.GetType(“ System.Windows.Forms.Form”)返回null - C#.NET - Type.GetType(“System.Windows.Forms.Form”) returns null Windows 通过ADFS C#.NET认证后出现认证框 Web Forms App - Windows authentication box appears after authenticating through ADFS C#.NET Web Forms App 加速在Windows窗体(c#.net)应用程序中从磁盘加载图像 - Speed up loading an image from disk in a windows forms (c#.net) app 使用 c#.net 可以使用控制台应用程序打开 Excel 工作簿,但不能使用 Windows 窗体应用程序打开 - Using c#.net can open excel workbook with console app but not with a windows forms app 如何在C#.net(2010)Windows窗体应用程序中获取TreeView控件中的所有复选框值? - how to get the All the Checkbox values in TreeView control in C#.net(2010) Windows Forms Application? 如何将文本框值传递给Windows窗体应用程序的c#.net中的方法 - how to pass textbox values to a method in c#.net for windows forms application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM