问题
I want to convert grayscale data to colored image using C#. I try 2D and convert 1D data and show bitmap, but I want to show Colored image.
回答1:
The built in graphics and drawings function from the .NET framework do not support working with Grayscale images.
I did the same thing to display a gray image as a heatmap with false colors.
In my case I used the library Emgu.CV
This is some example code I used for my project:
/*
* -----------------------------------------------------------------------------------
* 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<byte>(GetHueValue);
// Create the hsv image
var heatMapHsv = new Image<Hsv, byte>(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<Rgba, byte>(grayShadeMatrixConverted.Width, grayShadeMatrixConverted.Height, new Rgba());
CvInvoke.cvCvtColor(heatMapHsv.Ptr, heatMap.Ptr, Emgu.CV.CvEnum.COLOR_CONVERSION.CV_HSV2RGB);
#endregion
The function GetHueValue:
/// <summary>
/// Calculates the hue value by applying a logarithmic function to the values
/// </summary>
/// <param name="f"></param>
/// <returns></returns>
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);
}
Note: that the varibale grayShadeMatrix is a greyscale image with just one color-channel (grey value).
This results in images like this (with applied transparency where the greyimage had a value of 0):
来源:https://stackoverflow.com/questions/20467907/c-sharp-convert-grayscale-data-to-colored-image