WPF使用OpenCvSharp4
WPF使用OpenCvSharp4
- 创建项目
- 安装OpenCvSharp4
创建项目
安装OpenCvSharp4
在解决方案资源管理器中,右键单击项目名称,选择“管理 NuGet 包”。搜索并安装以下包:
- OpenCvSharp4
- OpenCvSharp4.Extensions
- OpenCvSharp4.runtime.win
- System.Management
using OpenCvSharp;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace OpenCvDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : System.Windows.Window
{
public MainWindow()
{
InitializeComponent();
OpencvTest();
}
private void OpencvTest()
{
string imgPath = "D:\\笔记\\python.jpg";
Mat srcImage = Cv2.ImRead(imgPath);
if (srcImage.Empty())
{
}
else
{
Mat hsvImage = new Mat();
Cv2.CvtColor(srcImage, hsvImage, ColorConversionCodes.BGR2HSV);
// 转为灰度图
Mat grayImage = new Mat();
Cv2.CvtColor(srcImage, grayImage, ColorConversionCodes.BGR2GRAY);
// 高斯模糊,减少噪声
Mat blurredImage = new Mat();
Cv2.GaussianBlur(grayImage, blurredImage, new OpenCvSharp.Size(5, 5), 1.5);
// Cannny算子进行边缘检测
Mat edges = new Mat();
Cv2.Canny(blurredImage, edges, 100, 200);
// 显示图片
Cv2.ImShow("原始图片", srcImage);
Cv2.ImShow("灰度图片", grayImage);
Cv2.ImShow("高斯模糊", blurredImage);
Cv2.ImShow("HSV图片", hsvImage);
Cv2.ImShow("边缘检测结果", edges);
Cv2.WaitKey(0);
Cv2.DestroyAllWindows();
}
}
}
}
运行结果: