C#将灰度数据转换为彩色图像

我想使用C#将灰度数据转换为彩色图像。 我尝试2D并转换1D数据并显示位图,但我想显示彩色图像。

.NET框架中的内置图形和绘图function不支持使用灰度图像。

我做了同样的事情,将灰色图像显示为带有假色的热图。
在我的例子中,我使用了Emgu.CV库
这是我用于项目的一些示例代码:

/* * ----------------------------------------------------------------------------------- * Step 3: Resize the gray image by using smoothing on it. * This makes the image-data more smooth for further processing * ----------------------------------------------------------------------------------- * */ var width = Convert.ToInt32(this.Params["Width"]); var smoothWidth = Convert.ToInt32(width / 150F); grayShadeMatrix = grayShadeMatrix.Resize(width, Convert.ToInt32(width * (float)ySize / (float)xSize), Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); grayShadeMatrix = grayShadeMatrix.SmoothBlur(smoothWidth, smoothWidth); #endregion #region Step 4: Create HeatMap by applying gradient color /* * ----------------------------------------------------------------------------------- * Step 4: Create the heatmap by using the value of the every point as hue-angle for the color * This way the color can be calculated very quickly. Also applies a log-function * on the value, to make the lower values visible too * ----------------------------------------------------------------------------------- * */ this.MaxHueValuePerValuePoint = MAX_HUE_VALUE / this.MaxValue; this.MaxHueValuePreCompiled = Math.Log(MAX_HUE_VALUE, ScalaLogBase); var grayShadeMatrixConverted = grayShadeMatrix.Convert(GetHueValue); // Create the hsv image var heatMapHsv = new Image(grayShadeMatrixConverted.Width, grayShadeMatrixConverted.Height, new Hsv()); heatMapHsv = heatMapHsv.Max(255); // Set each color-channel to 255 by default (hue: 255, sat: 255, val: 255) heatMapHsv[0] = grayShadeMatrixConverted; // Now set the hue channel to the calculated hue values // Convert hsv image back to rgb, for correct display var heatMap = new Image(grayShadeMatrixConverted.Width, grayShadeMatrixConverted.Height, new Rgba()); CvInvoke.cvCvtColor(heatMapHsv.Ptr, heatMap.Ptr, Emgu.CV.CvEnum.COLOR_CONVERSION.CV_HSV2RGB); #endregion 

函数GetHueValue:

  ///  /// Calculates the hue value by applying a logarithmic function to the values ///  ///  ///  private byte GetHueValue(ushort f) { f = Convert.ToUInt16(f < 1 ? 0 : f); var hue = (double)MAX_HUE_VALUE / Math.Log((double)UInt16.MaxValue, ScalaLogBase) * Math.Log(f, ScalaLogBase); hue = hue == Double.NegativeInfinity ? 0 : hue; return Convert.ToByte(hue); } 

注意: varibale grayShadeMatrix是一个只有一个颜色通道(灰度值)的灰度图像。

这会产生这样的图像(应用透明度,其中greyimage的值为0): 在此处输入图像描述