【问题标题】:C# Convert Grayscale data to colored image [closed]C# 将灰度数据转换为彩色图像 [关闭]
【发布时间】:2013-12-26 09:38:54
【问题描述】:

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

【问题讨论】:

  • 这是一个有趣的问题,已经有了一个可以接受的答案。请重新表述问题并展示您迄今为止所做的尝试。

标签: c# image grayscale colorize


【解决方案1】:

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

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

这会产生这样的图像(应用透明度,灰度图像的值为 0):

【讨论】:

    猜你喜欢
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多