OpenCvSharp里有自带的伪彩色,如
Cv2.ApplyColorMap(dst1, dst2, ColormapTypes.Winter);
但是自带的伪彩色常常不够用,就只能自定义自己想要的颜色,如下:
Cv2.LUT(src, table, b);
实现方式:
private byte[] GetTableByMax(int max)
{
byte[] table = new byte[256];
for (int i = 0; i < 256; i++)
{
table[i] = (byte)(i / 255.0f * max);
}
return table;
}
private Mat CustomColorMap(Mat src, Color color)
{
Mat dst = new Mat();
Mat b = new Mat();
Mat g = new Mat();
Mat r = new Mat();
byte[] table = GetTableByMax(color.B);
Cv2.LUT(src, table, b);
table = GetTableByMax(color.G);
Cv2.LUT(src, table, g);
table = GetTableByMax(color.R);
Cv2.LUT(src, table, r);
Cv2.Merge(new Mat[3] { b, g, r }, dst);
return dst;
}
调用:
Mat src = new Mat("C:\\1.tif", ImreadModes.Grayscale);//原始图片
Mat dst = new Mat(src.Size(), OpenCvSharp.MatType.CV_8UC3);//伪彩图片
dst = CustomColorMap(src, Color.Red);