当前位置: 首页 > news >正文

代码片段 - C#实现多张图片根据重叠部分拼接图像

安装Nuget包

  • Emgu.CV
  • Emgu.CV.runtime.windows
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Features2D;
using Emgu.CV.Stitching;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using System.Diagnostics;
namespace Tools
{
    public class ImageStitcher
    {
        public static void StitchImages(string[] imagePaths, string stitchedImagePath)
        {
            Image<Bgr, byte>[] sourceImages = new Image<Bgr, byte>[imagePaths.Length];
            for (int i = 0; i < sourceImages.Length; i++)
            {
                sourceImages[i] = new Image<Bgr, byte>(imagePaths[i]);
            }
            try
            {
                //only use GPU if you have build the native binary from code and enabled "NON_FREE"
                using (Stitcher stitcher = new Stitcher())
                using (Emgu.CV.Features2D.AKAZE finder = new Emgu.CV.Features2D.AKAZE())
                using (Emgu.CV.Stitching.WarperCreator warper = new SphericalWarper())
                {
                    stitcher.SetFeaturesFinder(finder);
                    stitcher.SetWarper(warper);
                    using (VectorOfMat vm = new VectorOfMat())
                    {
                        Mat result = new Mat();
                        vm.Push(sourceImages);

                        Stopwatch watch = Stopwatch.StartNew();

                        Stitcher.Status stitchStatus = stitcher.Stitch(vm, result);
                        watch.Stop();

                        if (stitchStatus == Stitcher.Status.Ok)
                        {
                            result.Save(stitchedImagePath);
                            Console.WriteLine(string.Format("Stitched in {0} milliseconds.", watch.ElapsedMilliseconds));
                        }
                        else
                        {
                            Console.WriteLine(string.Format("Stiching Error: {0}", stitchStatus));
                        }
                    }
                }
            }
            finally
            {
                foreach (Image<Bgr, Byte> img in sourceImages)
                {
                    img.Dispose();
                }
            }
        }
    }
}

  • imagePaths:待拼接的图片路径列表
  • stitchedImagePath:拼接好的图片保存路径

相关文章:

  • SpringBoot的简单介绍
  • Vue3实战八、路由权限和动态路由
  • 使用 Lua 脚本高效查询 Redis 键的内存占用
  • 【go】slice的浅拷贝和深拷贝
  • Python设计模式:适配模式
  • 2025 年江苏保安员职业资格考试经验分享​
  • starrocks split函数和trino split函数差异性
  • 【正点原子】如何设置 ATK-DLMP135 开发板 eth0 的开机默认 IP 地址
  • Windows RAC 集群故障:表决盘 “失踪” 致无法启动的排查实践
  • Docker部署Jenkins服务
  • ComfyUI模型下载与路径问题解决
  • IAGCN:登上《Nature》的深度学习可解释性情感分析模型突破
  • Appium的学习总结-Inspector参数设置和界面使用(5)
  • 【图像处理基石】什么是ISP色彩管理?
  • Android OpenCV 人脸识别 识别人脸框 识别人脸控件自定义
  • python小整数池和字符串贮存
  • Threejs实现天空盒效果
  • 前端单页应用性能优化全指南:从加载提速到极致体验
  • 《AI大模型趣味实战》智能财务助手系统配套说明:数据报表与指标手册
  • flink cdc的source数据流如何配置事件时间,如何设置时间语义,分配时间戳并生成水位线
  • 杭州的服装网站建设/建站企业网站
  • 网站开发与设计实训心得/保定seo推广公司
  • 帮客户做网站的公司/系统优化软件排行榜
  • 城市建设最好的网站/如何做网站优化seo
  • 网站源码下载教程/关键词查询工具软件
  • 做陶瓷公司网站/百度手机快速排名点击软件