如何将RGB图像更改为HSI [英] How to change RGB Image to HSI
本文介绍了如何将RGB图像更改为HSI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了问题,需要您的代码帮助。我需要将RGB图像更改为HSI图像:
错误:值太大而且太小而不是Int32?
请帮帮我,谢谢大家!
我的代码如下:
I have a problem, and need your kind help for my code. I need to change RGB Image to HSI Image:
Error: Value either too large and too small then Int32?
please help me ,thanks all!
My code is as:
private void rGBToHSIToolStripMenuItem_Click(object sender, EventArgs e) {
Bitmap org = (Bitmap)pictureBox1.Image.Clone(); //ghi anh da open gan vao bien org, ghi vao bo nho
Bitmap output1 = new Bitmap(org.Width, org.Height);
//dua ra anh ouput voi kich thuoc tuong tu anh da nhap la org
for (Byte y = 0; y < output1.Height; y++)
//thuc hien lap cho y chay tu o den chieu cao anh
{
for (Byte x = 0; x < output1.Width; x++)
//thuc hien lap cho x chay tu 0 den chieu rong
{
Color c = org.GetPixel(x, y);
Double h=0;
Byte r = Convert.ToByte(c.R/(c.R + c.G + c.B));
Byte g = Convert.ToByte(c.G / (c.R + c.G + c.B));
Byte b = Convert.ToByte(c.B / (c.R + c.G + c.B));
Byte s = Convert.ToByte(1 - 3 * Math.Min(r, Math.Min(g, b)));
Byte k = Convert.ToByte((c.R + c.G + c.B) / (3 * 255));
if (b<=g)
{
h = Convert.ToDouble((Math.Round((1 / Math.Cos((0.5 * (2 * r - g - b)) / (Math.Sqrt(((r - g) * (r - g) + (r - b) * (g - b)))))))));
}
else
{
h = Convert.ToDouble((Math.Round((2 * 3.14 - 1 / Math.Cos((0.5 * (2 * r - g - b)) / (Math.Sqrt(((r - g) * (r - g) + (r - b) * (g - b)))))))));
}
Byte s1 = Convert.ToByte((s * 100));
Byte k1 = Convert.ToByte((k * 255));
Byte h1 = Convert.ToByte(((h * 180 / 3.14)));
output1.SetPixel(x, y, Color.FromArgb(h1, s1, k1));
}
推荐答案
对于这种类型的图像处理,你真的应该使用不安全的代码(指针) )在你的嵌套循环中。在此搜索页面上查看有关CodeProject的三篇文章:[ ^ ]。
您可以在这里尝试RGB< => HSL算法:[ ^ ]。
您还可以使用API调用:
For image processing of this type you really should be using "unsafe" code (pointers) in your nested loop. See the three articles on CodeProject on this search page: [^].
You might try the RGB<=>HSL algorithm here: [^].
There is an API call you can use also:
[DllImport("shlwapi.dll")]
static extern void ColorRGBToHLS(int RGB, ref int H, ref int L, ref int S);
但是,请参阅此处的示例代码对其输出的重要修改:[ ^ ]。
您是否知道.NET提供RGB到HSL转换功能?示例:
But, see the sample code here for an important modification of its output:[^].
Are you aware that .NET supplies RGB to HSL conversion functions ? Example:
private struct hslColorValues
{
private float H;
private float S;
private float B;
public hslColorValues(float hue, float saturation, float brightness)
{
H = hue;
S = saturation;
B = brightness;
}
}
private hslColorValues rgbToHsl(string colorName)
{
Color color = Color.FromName(colorName);
return new hslColorValues(color.GetHue(), color.GetSaturation(), color.GetBrightness());
}
这篇关于如何将RGB图像更改为HSI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文