111,097
社区成员




csharp
private Bitmap srcBitmap = null;
public TImageColorspace(Bitmap tempBitmap)
{
srcBitmap = tempBitmap;
}
public byte[]RGBValue()
{
try
{
int w = srcBitmap.Width;
int h = srcBitmap.Height;
byte[] imageData = new byte[w * h * 3];
Bitmap dstBitmap = new Bitmap(srcBitmap.Width, srcBitmap.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Imaging.BitmapData srcData = srcBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
unsafe
{
byte* pIn = (byte*)srcData.Scan0.ToPointer();
byte* p;
int stride = srcData.Stride;
int r, g, b;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
p = pIn;
r = p[2];
g = p[1];
b = p[0];
imageData[x * 3 + y * w * 3] = (byte)b;
imageData[x * 3 + 1 + y * w * 3] = (byte)g;
imageData[x * 3 + 2 + y * w * 3] = (byte)r;
pIn += 3;
}
pIn += srcData.Stride - w * 3;
}
srcBitmap.UnlockBits(srcData);
return imageData;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
return null;
}
}
public double[] HISValue()
{
try
{
int w = srcBitmap.Width;
int h = srcBitmap.Height;
double[] imageData = new double[w * h * 3];
Bitmap dstBitmap = new Bitmap(srcBitmap.Width, srcBitmap.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Imaging.BitmapData srcData = srcBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
unsafe
{
byte* pIn = (byte*)srcData.Scan0.ToPointer();
byte* p;
int stride = srcData.Stride;
double r, g, b;
double hV = 0, degree = 0, iV = 0, sV = 0;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
p = pIn;
r = (double)p[2] / 255.0;
g = (double)p[1] / 255.0;
b = (double)p[0] / 255.0;
degree = Math.Acos(0.5 * ((r - g) + (r - b)) / Math.Sqrt((r - g) * (r - g) + (r - b) * (g - b) + 0.0000001)) / (2 * Math.PI);
hV = (b <= g) ? degree : 1.0 - degree;
iV = (double)(r + g + b) / 3.0;
sV = 1.0 - 3.0 * (double)Math.Min(r, Math.Min(g, b)) / (double)(r + g + b + 0.00000001);
imageData[x * 3 + y * w * 3] = hV;
imageData[x * 3 + 1 + y * w * 3] = iV;
imageData[x * 3 + 2 + y * w * 3] = sV;
pIn += 3;
}
pIn += srcData.Stride - w * 3;
}
srcBitmap.UnlockBits(srcData);
return imageData;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
return null;
}
}
public static double[] RGBtoHIS(byte[] rgbValue, int w, int h)
{
double[] hisValue = new double[w * h * 3];
double r = 0, g = 0, b = 0;
double hV = 0, degree = 0, iV = 0, sV = 0;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
b = rgbValue[x * 3 + y * w * 3] / 255.0;
g = rgbValue[x * 3 + 1 + y * w * 3] / 255.0;
r = rgbValue[x * 3 + 2 + y * w * 3] / 255.0;
degree = Math.Acos(0.5 * ((r - g) + (r - b)) / Math.Sqrt((r - g) * (r - g) + (r - b) * (g - b) + 0.0000001)) / (2 * Math.PI);
hV = (b <= g) ? degree : 1.0 - degree;
iV = (double)(r + g + b) / 3.0;
sV = 1.0 - 3.0 * (double)Math.Min(r, Math.Min(g, b)) / (double)(r + g + b + 0.00000001);
hisValue[x * 3 + y * w * 3] = hV;
hisValue[x * 3 + 1 + y * w * 3] = iV;
hisValue[x * 3 + 2 + y * w * 3] = sV;
}
}
return hisValue;
}
public static byte[] HIStoRGB(double[] hisValue, int w, int h)
{
byte[] rgbValue = new byte[w * h * 3];
double hV = 0, iV = 0, sV = 0;
double r = 0, g = 0, b = 0;
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++)
{
hV = hisValue[i * 3 + j * w * 3];
iV = hisValue[i * 3 + 1 + j * w * 3];
sV = hisValue[i * 3 + 2 + j * w * 3];
hV = hV * 2 * Math.PI;
if (hV >= 0 && hV < 2*Math .PI/3)
{
r = (double)(iV * (1.0 + (sV * Math.Cos(hV) / Math.Cos(Math .PI/3-hV))));
b = (double)(iV * (1.0 - sV));
g = (double)(3.0 * iV - r - b);
}
else if (hV >= 2*Math.PI/3 && hV < 4*Math .PI/3)
{
g = (double)(iV * (1.0 + sV * Math.Cos(hV-2*Math .PI/3) / Math.Cos(Math.PI-hV)));
r = (double)(iV * (1.0 - sV));
b = (double)(3.0 * iV - r - g);
}
else
{
g = (double)(iV * (1.0 - sV));
b = (double)(iV * (1.0 + sV * Math.Cos(hV-4*Math .PI/3) / Math.Cos(5*Math.PI/3-hV)));
r = (double)(3.0 * iV - g - b);
}
rgbValue[i * 3 + j * w * 3] = (byte)(Math .Min(255,b * 255.0));
rgbValue[i * 3 + 1 + j * w * 3] = (byte)(Math .Min(255,g * 255.0));
rgbValue[i * 3 + 2 + j * w * 3] = (byte)(Math .Min(255,r * 255.0));
}
}
return rgbValue;
}