C#将灰度数据转换为彩色图像
我想使用C#将灰度数据转换为彩色图像。C#将灰度数据转换为彩色图像,c#,image,grayscale,colorize,C#,Image,Grayscale,Colorize,我想使用C#将灰度数据转换为彩色图像。 我尝试二维转换一维数据并显示位图,但我想显示彩色图像。来自.NET framework的内置图形和绘图功能不支持处理灰度图像 我做了同样的事情,将灰色图像显示为带有假颜色的热图。 就我而言,我使用了图书馆 这是我在项目中使用的一些示例代码: /* * -----------------------------------------------------------------------------------
我尝试二维转换一维数据并显示位图,但我想显示彩色图像。来自.NET framework的内置图形和绘图功能不支持处理灰度图像 我做了同样的事情,将灰色图像显示为带有假颜色的热图。
就我而言,我使用了图书馆
这是我在项目中使用的一些示例代码:
/*
* -----------------------------------------------------------------------------------
* 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
/*
* -----------------------------------------------------------------------------------
*步骤3:通过对灰色图像进行平滑处理来调整其大小。
*这使得图像数据更加平滑以便进一步处理
* -----------------------------------------------------------------------------------
* */
var width=Convert.ToInt32(this.Params[“width”]);
var smoothWidth=转换为32(宽度/150F);
grayShadeMatrix=grayShadeMatrix.Resize(宽度,转换为32(宽度*(浮点)ySize/(浮点)xSize),Emgu.CV.CvEnum.INTER.CV\u INTER\u线性);
grayShadeMatrix=grayShadeMatrix.SmoothBlur(smoothWidth,smoothWidth);
#端区
#区域步骤4:通过应用渐变颜色创建热图
/*
* -----------------------------------------------------------------------------------
*步骤4:使用每个点的值作为颜色的色调角度来创建热图
*这样可以非常快速地计算颜色。还应用了日志函数
*在值上,使较低的值也可见
* -----------------------------------------------------------------------------------
* */
this.maxhuevalueprovaluepoint=最大色调值/this.MaxValue;
this.maxhuevalueprocompiled=Math.Log(最大值,ScalaLogBase);
var grayShadeMatrixConverted=grayShadeMatrix.Convert(GetHueValue);
//创建hsv图像
var heatMapHsv=新图像(grayShadeMatrixConverted.Width,grayShadeMatrixConverted.Height,new Hsv());
heatMapHsv=heatMapHsv.Max(255);//默认情况下,将每个颜色通道设置为255(色调:255、sat:255、val:255)
heatMapHsv[0]=灰度阴影矩阵转换;//现在将色调通道设置为计算的色调值
//将hsv图像转换回rgb,以便正确显示
var heatMap=新图像(grayShadeMatrixConverted.Width,grayShadeMatrixConverted.Height,new Rgba());
CvInvoke.cvtcolor(heatMapHsv.Ptr、heatMap.Ptr、Emgu.CV.CvEnum.COLOR\u CONVERSION.CV\u HSV2RGB);
#端区
函数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);
}
//
///通过对值应用对数函数来计算色调值
///
///
///
专用字节GetHueValue(ushort f)
{
f=转换为16(f<1?0:f);
var hue=(双精度)MAX_hue_VALUE/Math.Log((双精度)UInt16.MaxValue,ScalaLogBase)*Math.Log(f,ScalaLogBase);
色调=色调==双色。负色不透明度?0:色调;
返回Convert.ToByte(色调);
}
注意:varibale grayShadeMatrix是一个灰度图像,只有一个颜色通道(灰度值)
这会产生如下图像(应用透明度,其中灰度图像的值为0):
这是一个相互测试的问题,已经有了一个可接受的答案。请重新表述这个问题,并展示你迄今为止所做的尝试。