在Emgu.CV中使用C#自定义Mat上的颜色映射

huangapple go评论67阅读模式
英文:

Customized colormap on Mat in Emgu.CV in C#

问题

我正在使用CvInvoke.ApplyColorMap将颜色映射应用到我的灰度矩阵(srcMat,50x50,8位无符号整数,单通道)。但似乎只有"ColorMapType Enumeration"中的内置颜色映射。我可以使用自定义颜色映射吗?

示例代码:

Mat dstMat = new Mat(50, 50, Emgu.CV.CvEnum.DepthType.Cv8U, 3);
CvInvoke.ApplyColorMap(srcMat, dstMat, Emgu.CV.CvEnum.ColorMapType.Jet);

在示例代码中,我正在使用内置的颜色映射"Jet"。有人可以建议如何使用自定义颜色映射吗?

英文:

I am using CvInvoke.ApplyColorMap to assign a colormap on my grayscale mat (srcMat, 50 by 50, 8-bit unsigned integer, single channel). But it seems there are only built in colormaps in "ColorMapType Enumeration". Can I have customized colormap to assign?

Example codes:

Mat dstMat = new Mat(50, 50, Emgu.CV.CvEnum.DepthType.Cv8U, 3);
CvInvoke.ApplyColorMap(srcMat, dstMat , Emgu.CV.CvEnum.ColorMapType.Jet);

In the example codes, I am using built-in colormap "Jet". Can someone advise how to use customized colormap?

答案1

得分: 0

你需要创建一个256x1的矩阵,使用自定义的颜色映射,颜色从绿色到红色渐变。以下是代码部分的翻译:

Image<Bgr, byte> ApplyCustomColorMap(Image<Gray, byte> imin)
{
    Bgr col0 = new Bgr(Color.Green);
    Bgr col1 = new Bgr(Color.Red);

    Matrix<byte> mtB = new Matrix<byte>(256, 1);
    Matrix<byte> mtG = new Matrix<byte>(256, 1);
    Matrix<byte> mtR = new Matrix<byte>(256, 1);

    for (int i = 0; i < 256; i++)
    {
        double p = (double)i / 256.0;
        double q = 1 - p;

        mtB.Data[i, 0] = (byte)(q * (double)col0.Blue + p * (double)col1.Blue);
        mtG.Data[i, 0] = (byte)(q * (double)col0.Green + p * (double)col1.Green);
        mtR.Data[i, 0] = (byte)(q * (double)col0.Red + p * (double)col1.Red);
    }

    VectorOfMat vct = new VectorOfMat();
    vct.Push(mtB);
    vct.Push(mtG);
    vct.Push(mtR);

    Mat mm2 = new Mat(256, 1, Emgu.CV.CvEnum.DepthType.Cv8U, 3);
    CvInvoke.Merge(vct, mm2);

    var colorMappedImage = new Image<Bgr, byte>(img_colormap.Size);
    CvInvoke.ApplyColorMap(imin, colorMappedImage, mm2);
    return colorMappedImage;
}
英文:

You have to create a 256x1 mat with your custom colormap, this one goes from green to red:

Image&lt;Bgr, byte&gt; ApplyCustomColorMap(Image&lt;Gray, byte&gt; imin)
{


Bgr col0 = new Bgr(Color.Green);
Bgr col1 = new Bgr(Color.Red);

Matrix&lt;byte&gt; mtB = new Matrix&lt;byte&gt;(256, 1);
Matrix&lt;byte&gt; mtG = new Matrix&lt;byte&gt;(256, 1);
Matrix&lt;byte&gt; mtR = new Matrix&lt;byte&gt;(256, 1);

for (int i = 0; i &lt; 256; i++)
{
    double p = (double)i / 256.0;
    double q = 1 - p;

    mtB.Data[i, 0] = (byte)(q * (double)col0.Blue + p * (double)col1.Blue);
    mtG.Data[i, 0] = (byte)(q * (double)col0.Green + p * (double)col1.Green);
    mtR.Data[i, 0] = (byte)(q * (double)col0.Red + p * (double)col1.Red);
}
VectorOfMat vct = new VectorOfMat();
vct.Push(mtB);
vct.Push(mtG);
vct.Push(mtR);
Mat mm2 = new Mat(256, 1, Emgu.CV.CvEnum.DepthType.Cv8U, 3);
CvInvoke.Merge(vct, mm2);

var colorMappedImage = new Image&lt;Bgr, byte&gt;(img_colormap.Size);
CvInvoke.ApplyColorMap(imin, colorMappedImage, mm2);
return colorMappedImage;

}

huangapple
  • 本文由 发表于 2023年2月14日 06:45:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441890.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定