C# 图像追踪

C# OpenCvShar4.windows 图像追踪

C# 图像追踪

NuGet 包管理器 安装 OpenCvSharp4.Windows

using bmpTo;
using DlibDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using static pubVar;
using static CsAPI;
using Rectangle = DlibDotNet.Rectangle;
using OpenCvSharp;
using System.Diagnostics;

class pubThr
    {

    private void subsA(object sender, DoWorkEventArgs e)
    {
        while (bl == true)
        {
            var tracker = new CorrelationTracker();
            int init = 0;
            Mat temp = null;

            using (var win = new ImageWindow())
            {
                win.SetPos(1000, 0);
                //选择追踪对象
                while (!win.IsClosed())
                {

                    CsAPI.Rect fx = new CsAPI.Rect();
                    GetWindowRect(hwnd_main, out fx);//h为窗口句柄
                    int width = fx.Right - fx.Left;                        //窗口的宽度
                    int height = fx.Bottom - fx.Top;                   //窗口的高度

                    GetCursorPos(ref defPnt);

                    int xx = defPnt.X- fx.Left;
                    int yy = defPnt.Y- fx.Top;
                  

                    bitmap = windowprint.GetWindow(hwnd_main);

                    temp = BitmapConverter.ToMat(bitmap);//BitmapToMat(bitmap);
                    System.Threading.Thread.Sleep(10);

                    //释放资源 
                    //bitmap.Dispose();
                    //bitmap = null;
                    //g.Dispose();
                    //GC.Collect();


                    if (temp == null)
                    {
                        Debug.WriteLine("图像获取错误!");
                        return;
                    }

                    var array = new byte[temp.Width * temp.Height * temp.ElemSize()];
                    Marshal.Copy(temp.Data, array, 0, array.Length);
                    using (var cimg = Dlib.LoadImageData<BgrPixel>(array, (uint)temp.Height, (uint)temp.Width, (uint)(temp.Width * temp.ElemSize())))
                    {

                        init++;
                        if (init > 1)
                        {

                            if (t_start == 1)
                            {
                                Debug.WriteLine("开始追踪目标!");
                                t_start = 0;
                                blwindow = true;
                                //确定 追踪 位置
                                var rect2 = DRectangle.CenteredRect(xx, yy, 100, 100);
                                //开始追踪
                                tracker.StartTrack(cimg, rect2);
                                win.SetImage(cimg);
                                win.ClearOverlay();
                                win.AddOverlay(rect2);
                                break;
                            }
                        }

                        var rect1 = DRectangle.CenteredRect(xx, yy, 100, 100);

                        //显示图片
                        win.SetImage(cimg);
                        win.ClearOverlay();
                        //显示框
                        win.AddOverlay(rect1);

                    }


                }
                if (win.IsClosed()) blwindow = false;
                //选择追踪对象
                while (blwindow==true)
                {
                    bitmap = windowprint.GetWindow(hwnd_main);
                    temp = BitmapConverter.ToMat(bitmap);//BitmapToMat(bitmap);
                    //System.Threading.Thread.Sleep(100);


                    if (temp == null)
                    {
                        Debug.WriteLine("图像获取错误!");
                        return;
                    }

                    var array = new byte[temp.Width * temp.Height * temp.ElemSize()];
                    Marshal.Copy(temp.Data, array, 0, array.Length);
                    using (var cimg = Dlib.LoadImageData<BgrPixel>(array, (uint)temp.Height, (uint)temp.Width, (uint)(temp.Width * temp.ElemSize())))
                    {
                        //更新追踪图像
                        tracker.Update(cimg);

                        win.SetImage(cimg);
                        win.ClearOverlay();

                        //获得追踪到的目标位置
                        DRectangle rect2 = tracker.GetPosition();
                        win.AddOverlay(rect2);

                     
                        System.Diagnostics.Debug.WriteLine("OBJ RECT:" + (int)rect2.Left + " " + (int)rect2.Top + " " + (int)rect2.Width + " " + (int)rect2.Height);

                        System.Threading.Thread.Sleep(10);
                    }
                }

            }

        }
    }

    public void thr_start()
    {
        if (objsub.IsBusy==false)
        {
        objsub.DoWork -= new DoWorkEventHandler(subsA);
        objsub.DoWork += new DoWorkEventHandler(subsA);
        objsub.WorkerSupportsCancellation = true;
        objsub.WorkerReportsProgress = true;
        objsub.RunWorkerAsync();
        }
    }

    }

F2 获取窗口

F3 鼠标指向窗口定位图像

F4 重新加载

需要释放内存

源码下载地址 C#OpenCvShar4.windows图像追踪tracking.rar-C#文档类资源-CSDN下载

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值