C#图像伪彩色处理方法
程序员文章站
2022-05-15 14:20:26
本文实例讲述了c#图像伪彩色处理方法。分享给大家供大家参考。具体如下:
//灰度图转伪彩色图像函数
public bitmap pgraytocolor(bit...
本文实例讲述了c#图像伪彩色处理方法。分享给大家供大家参考。具体如下:
//灰度图转伪彩色图像函数 public bitmap pgraytocolor(bitmap src) { try { bitmap a = new bitmap(src); rectangle rect = new rectangle(0, 0, a.width, a.height); system.drawing.imaging.bitmapdata bmpdata = a.lockbits(rect, system.drawing.imaging.imagelockmode.readwrite, system.drawing.imaging.pixelformat.format24bpprgb); int stride = bmpdata.stride; unsafe { byte* pin = (byte*)bmpdata.scan0.topointer(); byte* p; int r, g, b; int temp = 0; for (int y = 0; y < a.height; y++) { for (int x = 0; x < a.width; x++) { p = pin; b = p[0]; g = p[1]; r = p[2]; temp = (byte)(b * 0.114 + g * 0.587 + r * 0.299); if (temp >= 0 && temp <= 63) { p[2] = 0; p[1] = (byte)(254 - 4 * temp); p[0] = (byte)255; } if (temp >= 64 && temp <= 127) { p[2] = 0; p[1] = (byte)(4 * temp - 254); p[0] = (byte)(510 - 4 * temp); } if (temp >= 128 && temp <= 191) { p[2] = (byte)(4 * temp - 510); p[1] = (byte)(255); p[0] = (byte)0; } if (temp >= 192 && temp <= 255) { p[2] = (byte)255; p[1] = (byte)(1022 - 4 * temp); p[0] = (byte)0; } pin += 3; } pin += stride - a.width * 3; } } a.unlockbits(bmpdata); return a; } catch (exception e) { messagebox.show(e.message.tostring()); return null; } }
原图:
效果图:
反色图:
希望本文所述对大家的c#程序设计有所帮助。