C# Преобразуване на данни от сивата скала в цветно изображение [затворено]

Искам да конвертирам данни в сивата скала в цветно изображение с помощта на C#. Опитвам 2D и конвертирам 1D данни и показвам растерно изображение, но искам да покажа цветно изображение.


person flanker_42    schedule 09.12.2013    source източник
comment
Това е интересен проблем, който вече има приемлив отговор. Моля, преформулирайте въпроса и покажете какво сте опитвали досега.   -  person Larry    schedule 09.12.2013


Отговори (1)


Вградената функция за графики и чертежи от .NET framework не поддържа работа с Grayscale изображения.

Направих същото, за да покажа сиво изображение като топлинна карта с фалшиви цветове.
В моя случай използвах библиотеката 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): въведете описание на изображението тук

person RononDex    schedule 09.12.2013