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

时间:2013-12-09 10:10:27

标签: c# image grayscale colorize

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

1 个答案:

答案 0 :(得分:3)

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

我做了同样的事情,将灰色图像显示为带有假色的热图 就我而言,我使用了库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<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

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);
    }

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

这导致像这样的图像(应用透明度,其中greyimage的值为0): enter image description here

相关问题