C# - Opencv应用(2) 之矩阵Mat使用[矩阵创建、图像显示、像素读取与赋值]
- 矩阵创建
- 图像显示与保存
- 像素读取与赋值
- 新建sample02项目,配置opencv4相关包,新建
.cs
进行测试
1.矩阵创建
//创建空白矩阵
var dst = new Mat()//创建并赋值
var src = new Mat(new Size(128, 128), MatType.CV_8U, Scalar.All(125));//图像拷贝
Mat dst = src.Clone();
图像显示与保存
private static void test_0()
{//全黑var mat = new Mat(new Size(600, 600), MatType.CV_8UC3, new Scalar(0, 0, 0));Cv2.NamedWindow("black", 0);Cv2.ImShow("black", mat);// 全白mat = new Mat(new Size(600, 600), MatType.CV_8UC3, new Scalar(255, 255, 255));Cv2.NamedWindow("white", 0);Cv2.ImShow("white", mat);Cv2.ImWrite("white.jpg", mat);
}
像素读取与赋值
- 两种常用的图像遍历方式
private void GetSet(){using var mat = new Mat(ImagePath.Lenna, ImreadModes.Color);for (int y = 0; y < mat.Height; y++){for (int x = 0; x < mat.Width; x++){Vec3b color = mat.Get<Vec3b>(y, x);Vec3b newColor = new Vec3b(color.Item2, color.Item1, color.Item0);mat.Set<Vec3b>(y, x, newColor);}}Cv2.ImShow("Slow", mat);Cv2.WaitKey(0);Cv2.DestroyAllWindows();}/// <summary>/// Reasonably fast/// </summary>private void GenericIndexer(){using var mat = new Mat(ImagePath.Lenna, ImreadModes.Color);var indexer = mat.GetGenericIndexer<Vec3b>();for (int y = 0; y < mat.Height; y++){for (int x = 0; x < mat.Width; x++){Vec3b color = indexer[y, x];Vec3b newColor = new Vec3b(color.Item2, color.Item1, color.Item0);indexer[y, x] = newColor;}}Cv2.ImShow("GenericIndexer", mat);Cv2.WaitKey(0);Cv2.DestroyAllWindows();}
- 灰度图操作
private static void test_1(){var img = new Mat(new Size(128, 128), MatType.CV_8U, 1);var pixel = img.GetGenericIndexer<int>();for (var y = 0; y < img.Height; y++){for (var x = 0; x < img.Width; x++){pixel[y, x] = x + y;}}Cv2.NamedWindow("单通道",0);Cv2.ImShow("单通道", img);Cv2.WaitKey(0);
}
- 三通道图操作
private static void test_2()
{//三通道using (var src = new Mat(new Size(128, 128), MatType.CV_8UC3, new Scalar(20, 129, 250)))using (var dst = new Mat()){for (var y = 0; y < src.Height; y++){for (var x = 0; x < src.Width; x++){var color = src.Get<Vec3b>(y, x);var temp = color.Item0;color.Item0 = color.Item2; // B <- Rcolor.Item2 = temp; // R <- Bsrc.Set(y, x, color);}}src.CopyTo(dst);Mat dat = dst.Clone();Cv2.NamedWindow("dst", 0);Cv2.ImShow("dst", dst);Cv2.WaitKey(0);}
}
- 完整代码
using OpenCvSharp;namespace OpenCVSharpSample01
{class Program{static void Main(string[] args){test_0();test_1();test_2();}private static void test_0(){var mat = new Mat(new Size(600, 600), MatType.CV_8UC3, new Scalar(0, 0, 0));Cv2.NamedWindow("black", 0);Cv2.ImShow("black", mat);// 全白mat = new Mat(new Size(600, 600), MatType.CV_8UC3, new Scalar(255, 255, 255));Cv2.NamedWindow("white", 0);Cv2.ImShow("white", mat);Cv2.ImWrite("white.jpg", mat);}private static void test_1(){var img = new Mat(new Size(128, 128), MatType.CV_8U, 1);var pixel = img.GetGenericIndexer<int>();for (var y = 0; y < img.Height; y++){for (var x = 0; x < img.Width; x++){pixel[y, x] = x + y;}}Cv2.NamedWindow("单通道",0);Cv2.ImShow("单通道", img);Cv2.WaitKey(0);}private static void test_2(){//三通道using (var src = new Mat(new Size(128, 128), MatType.CV_8U, Scalar.All(125)))using (var dst = new Mat()){for (var y = 0; y < src.Height; y++){for (var x = 0; x < src.Width; x++){var color = src.Get<Vec3b>(y, x);var temp = color.Item0;color.Item0 = color.Item2; // B <- Rcolor.Item2 = temp; // R <- Bsrc.Set(y, x, color);}}src.CopyTo(dst);Mat dat = dst.Clone();Cv2.NamedWindow("dst", 0);Cv2.ImShow("dst", dst);Cv2.WaitKey(0);}}}
}