英文:
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<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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论