原文:Win8Metro(C#)数字图像处理--2.7图像伪彩色
图像伪彩色函数
[函数名称]
图像伪彩色函数PseudoColorProcess(WriteableBitmap
src)
[算法说明]
伪彩色是为改善视觉效果,利用计算机图像增强技术对图像的灰度赋予的不同假色彩,即,将一张灰度图转化为彩色图。主要原理是把灰度图像的各个不同灰度级按照线性或非线性的映射函数变换成为不同的彩色空间。
本文采用基于RGB颜色空间的伪彩色映射算法。过程如下:
[函数代码]
///<summary>
///
Pseudo color process.
///</summary>
///<param
name="src">Source image.</param>
///<returns></returns>
publicstaticWriteableBitmap
PseudoColorProcess(WriteableBitmap src)////7伪彩色处理
{
if(src!=null
)
{
int
w = src.PixelWidth;
int
h = src.PixelHeight;
WriteableBitmap
pseudoImage =newWriteableBitmap(w,
h);
byte[]
temp = src.PixelBuffer.ToArray();
int
tGray = 0;
for
(int i = 0; i < temp.Length; i += 4)
{
tGray = (int)(temp[i]
* 0.114 + temp[i + 1] * 0.587 + temp[i + 2] * 0.299);
if
(tGray >= 0 && tGray <= 63)
{
temp[i] = (byte)255;
temp[i + 1] = (byte)(254
- 4 * tGray);
temp[i + 2] = 0;
}
if
(tGray >= 64 && tGray <= 127)
{
temp[i] = (byte)(510
- 4 * tGray);
temp[i + 1] = (byte)(4
* tGray - 254);
temp[i + 2] = (byte)0;
}
if
(tGray >= 128 && tGray <= 191)
{
temp[i] = (byte)0;
temp[i + 1] = (byte)255;
temp[i + 2] = (byte)(4
* tGray - 510);
}
if
(tGray >= 192 && tGray <= 255)
{
temp[i] = (byte)0;
temp[i + 1] = (byte)(1022
- 4 * tGray);
temp[i + 2] = (byte)255;
}
tGray = 0;
}
Stream
sTemp = pseudoImage.PixelBuffer.AsStream();
sTemp.Seek(0,SeekOrigin.Begin);
sTemp.Write(temp, 0, w * 4 * h);
return
pseudoImage;
}
else
{
returnnull;
}
}