笔记:Mat.Set<T>方法修改图像中的颜色,T的类型不能是Scalar,应为Vec3b,否则颜色修改失败
C#代码:
Mat labs = new Mat();
int counts = Cv2.ConnectedComponents(otsu,labs,PixelConnectivity.Connectivity4);
List<Vec3b> colors = new List<Vec3b>();
colors.Add(new Vec3b(0, 0, 0));
Random random = new Random();
for(int i = 0;i < counts; i++)
{
Vec3b scl = new Vec3b((byte)random.Next(0,256), (byte)random.Next(0, 256), (byte)random.Next(0, 256));
colors.Add(scl);
}
Mat dst = new Mat(otsu.Size(), MatType.CV_8UC3);
for(int r = 0; r < dst.Rows; r++)
{
for(int c = 0; c < dst.Cols; c++)
{
int lab = labs.At<int>(r, c);
dst.Set<Vec3b>(r, c, colors[lab]);
}
}
效果图(OpenCV中0表示没有数据,255表示有数据):