WPF编程--OpenCvSharp图像拼接Demo

目录

1. 创建项目StitchImage

2. NuGet安装依赖   OpenCvSharp4   OpenCvSharp4.Extensions   OpenCvSharp4.runtime.win​编辑

 3. 创建Images文件夹用于存放待拼接的图片

4. 编写MainView.xmal的展示区域代码

5. 编辑MainView.xmal.cs的OpenCvSharp图像拼接代码

6. 运行程序


1. 创建项目StitchImage

2. NuGet安装依赖
   OpenCvSharp4
   OpenCvSharp4.Extensions
   OpenCvSharp4.runtime.winWPF编程--OpenCvSharp图像拼接Demo_第1张图片

 3. 创建Images文件夹用于存放待拼接的图片

 

4. 编写MainView.xmal的展示区域代码


    
        
    

5. 编辑MainView.xmal.cs的OpenCvSharp图像拼接代码

using OpenCvSharp;
using OpenCvSharp.Extensions;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;

namespace StitchImage
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            Width = SystemParameters.WorkArea.Width / 1.5;
            Height = SystemParameters.WorkArea.Height / 1.5;
            this.Loaded += Stitch_Image;
        }

        private void Stitch_Image(object sender, RoutedEventArgs e)
        {
            //1. 加载两张待拼接的图片
            string imageBasePath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
            string imageFullPath_1 = imageBasePath + "\\Images\\image1.jpg";
            string imageFullPath_2 = imageBasePath + "\\Images\\image2.jpg";
            Mat image1 = new Mat(imageFullPath_1, ImreadModes.Color);
            Mat image2 = new Mat(imageFullPath_2, ImreadModes.Color);
            //2. 完成图片拼接
            Mat[] images = new Mat[] { image1, image2 };
            Stitcher stitcher = Stitcher.Create(Stitcher.Mode.Scans);
            Mat pano = new Mat();
            stitcher.Stitch(images, pano);
            //3. 拼接后的图片展示
            var converted = Convert(BitmapConverter.ToBitmap(pano));
            imgViewport.Source = converted;
        }

        BitmapImage Convert(Bitmap src)
        {
            Bitmap ImageOriginalBase = new Bitmap(src);
            BitmapImage bitmapImage = new BitmapImage();
            using (MemoryStream ms = new MemoryStream())
            {
                ImageOriginalBase.Save(ms, ImageFormat.Png);
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = ms;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                bitmapImage.Freeze();
            }
            return bitmapImage;
        }
    }
}

6. 运行程序

你可能感兴趣的:(wpf,c#,opencv)