for (int i = 0; i < bmp.Width; i++) for (int j = 0l j < bmp.Height; j++) Color = bmp.getpixel(i, j)
/// <summary> /// RGB转GRAY时用的3值字典 /// </summary> class RgbGrayData { static double[] _R = new double[256]; static double[] _G = new double[256]; static double[] _B = new double[256]; static RgbGrayData _Init = new RgbGrayData(); private RgbGrayData() { for (int i = 0; i < 256; i++) _R[i] = i * 0.212671; for (int i = 0; i < 256; i++) _G[i] = i * 0.715160; for (int i = 0; i < 256; i++) _B[i] = i * 0.072169; } public static double[] R { get { return _R; } } public static double[] G { get { return _G; } } public static double[] B { get { return _B; } }
} ////////////////////////////////////////////////////////////// private unsafe void Rgb2Gray(int offsetRGB, byte* bytRGB, int wRGB, int hRGB, int offsetGray, byte[] bytGray) { int dst = 0; for (int i = 0; i < hRGB; i++) { for (int j = 0; j < wRGB; j++) { // RGB在内存中存放顺序是高位R低位B bytGray[dst++] = (byte)(RgbGrayData.B[*(bytRGB++)] + RgbGrayData.G[*(bytRGB++)] + RgbGrayData.R[*(bytRGB++)]); } // 跳过图像数据每行未用空间的字节,length = stride - width * bytePerPixel bytRGB += offsetRGB; dst += offsetGray; } } private byte[] GetGrayData(Image imgOrg) { int strideRGB = ((imgOrg.Width * 3 + 3) / 4) * 4; int offsetRGB = strideRGB - imgOrg.Width * 3; byte[] rgbData = new byte[strideRGB * imgOrg.Height]; int strideGray = ((imgOrg.Width + 3) / 4) * 4; int offsetGray = strideGray - imgOrg.Width; byte[] grayData = new byte[strideGray * imgOrg.Height]; using (Bitmap bitmap = new Bitmap(imgOrg)) { BitmapData dt = bitmap.LockBits(new Rectangle(0, 0, imgOrg.Width, imgOrg.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); unsafe { byte* ptr = (byte*)(dt.Scan0); Rgb2Gray(offsetRGB, ptr, imgOrg.Width, imgOrg.Height, offsetGray, grayData); } bitmap.UnlockBits(dt); } return grayData; }我的一个彩色转黑白的,拿去吧!嫌麻烦还有一个办法:请用opencv 平时来得少还是啥原因,这两天总看到应该发c/c++区的帖子发到.net这里来,这是说.net已经普及到可以替代他们了吗!
for (int j = 0l j < bmp.Height; j++)
Color = bmp.getpixel(i, j)
/// <summary>
/// RGB转GRAY时用的3值字典
/// </summary>
class RgbGrayData
{
static double[] _R = new double[256];
static double[] _G = new double[256];
static double[] _B = new double[256]; static RgbGrayData _Init = new RgbGrayData(); private RgbGrayData()
{
for (int i = 0; i < 256; i++)
_R[i] = i * 0.212671;
for (int i = 0; i < 256; i++)
_G[i] = i * 0.715160;
for (int i = 0; i < 256; i++)
_B[i] = i * 0.072169;
} public static double[] R
{
get { return _R; }
}
public static double[] G
{
get { return _G; }
}
public static double[] B
{
get { return _B; }
}
}
//////////////////////////////////////////////////////////////
private unsafe void Rgb2Gray(int offsetRGB, byte* bytRGB, int wRGB, int hRGB, int offsetGray, byte[] bytGray)
{
int dst = 0;
for (int i = 0; i < hRGB; i++)
{
for (int j = 0; j < wRGB; j++)
{
// RGB在内存中存放顺序是高位R低位B
bytGray[dst++] = (byte)(RgbGrayData.B[*(bytRGB++)] + RgbGrayData.G[*(bytRGB++)] + RgbGrayData.R[*(bytRGB++)]);
}
// 跳过图像数据每行未用空间的字节,length = stride - width * bytePerPixel
bytRGB += offsetRGB;
dst += offsetGray;
}
}
private byte[] GetGrayData(Image imgOrg)
{
int strideRGB = ((imgOrg.Width * 3 + 3) / 4) * 4;
int offsetRGB = strideRGB - imgOrg.Width * 3;
byte[] rgbData = new byte[strideRGB * imgOrg.Height]; int strideGray = ((imgOrg.Width + 3) / 4) * 4;
int offsetGray = strideGray - imgOrg.Width;
byte[] grayData = new byte[strideGray * imgOrg.Height]; using (Bitmap bitmap = new Bitmap(imgOrg))
{
BitmapData dt = bitmap.LockBits(new Rectangle(0, 0, imgOrg.Width, imgOrg.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
unsafe
{
byte* ptr = (byte*)(dt.Scan0);
Rgb2Gray(offsetRGB, ptr, imgOrg.Width, imgOrg.Height, offsetGray, grayData);
}
bitmap.UnlockBits(dt);
} return grayData;
}我的一个彩色转黑白的,拿去吧!嫌麻烦还有一个办法:请用opencv
平时来得少还是啥原因,这两天总看到应该发c/c++区的帖子发到.net这里来,这是说.net已经普及到可以替代他们了吗!