? ? 以下是一些C#中使用OpenCV的常用函数例子:
1. 加载图像:
using OpenCvSharp;
Mat image = Cv2.ImRead("path_to_your_image.jpg", ImreadModes.Color);
2. 显示图像:
Cv2.NamedWindow("Image Window", WindowFlags.Normal);
Cv2.ImShow("Image Window", image);
Cv2.WaitKey(0);
Cv2.DestroyAllWindows();
3. 图像转换(例如RGB转灰度):
Mat grayImage = new Mat();
Cv2.CvtColor(image, grayImage, ColorConversionCodes.BGR2GRAY);
4. 通道分离:
Mat[] channels = new Mat[3];
Cv2.Split(image, channels);
Mat blueChannel = channels[0]; // BGR order, so the first channel is Blue
5. 位操作(例如求非):
Mat bitwiseNotImage = new Mat();
Cv2.BitwiseNot(image, bitwiseNotImage);
6. 计算图像的直方图:
int[] histSize = { 256 };
float[] ranges = { 0, 256 };
MatND histogram = new MatND();
Cv2.CalcHist(new Mat[] { grayImage }, new int[] { 0 }, null, histogram, new int[] { 1 }, histSize, ranges);
7. 裁剪图像:
Rect roi = new Rect(new Point(100, 100), new Size(200, 200));
Mat croppedImage = new Mat(image, roi);
8. 缩放图像:
Mat resizedImage = new Mat();
Cv2.Resize(image, resizedImage, new Size(800, 600));
9. 保存图像:
Cv2.ImWrite("output_image.jpg", image);
以上就是一些C#中使用OpenCV的常用函数例子,根据具体需求可以选择合适的功能进行图像处理。请注意,这些代码示例假设你已经正确安装并引用了OpenCvSharp库。