using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
//using Microsoft.VisualBasic;
using System.Linq;
using System;
using System.Collections;
using System.Xml.Linq;
using System.Windows.Forms;
//using Microsoft.VisualBasic.CompilerServices;
namespace WindowsApplication1
{
public partial class Form1
{
public Form1()
{
InitializeComponent();
//Added to support default instance behavour in C#
if (defaultInstance == null)
defaultInstance = this;
}
#region Default Instance
private static Form1 defaultInstance;
/// <summary>
/// Added by the VB.Net to C# Converter to support default instance behavour in C#
/// </summary>
public static Form1 Default
{
get
{
if (defaultInstance == null)
{
defaultInstance = new Form1();
defaultInstance.FormClosed += new FormClosedEventHandler(defaultInstance_FormClosed);
}
return defaultInstance;
}
}
static void defaultInstance_FormClosed(object sender, FormClosedEventArgs e)
{
defaultInstance = null;
}
#endregion
public class cvCalcOpticalFlowPyrLK
{
public cvCalcOpticalFlowPyrLK()
{
flags = Emgu.CV.CvEnum.LKFLOW_TYPE.DEFAULT;
}
private WindowsApplication1.Capture captureCV;
private bool captureInProgress;
const int radius = 5;
bool defineCorners = false;
PointF[][] puntiA = new PointF[1][];
PointF[][] puntiB = new PointF[1][];
PointF[][] puntiTemp;
PointF[][] puntiBtemp = new PointF[1][];
bool add_remove_pt = false;
int count = 0;
Image.CV.Image.CV.Image<CV.Structure.Bgr, byte> frame;
Image<WindowsApplication1.Gray, byte> grayA;
Image<WindowsApplication1.Gray, byte> grayB;
Image.Image<CV.Structure.Gray, byte> pyrBufferA;
Image<CV.Structure.Gray, byte> pyrBufferB;
Image<CV.Structure.Gray, byte> imgTemp;
CvEnum.LKFLOW_TYPE.LKFLOW_TYPE flags;
byte[] status;
float[] errors;
private void Button1_Click(System.Object sender, System.EventArgs e)
{
if (captureCV == null)
{
try
{
captureCV = new Emgu.CV.Capture();
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
if (captureCV != null)
{
if (captureInProgress)
{
this.Button1.Text = "Start Capture";
Application.Idle -= new System.EventHandler(new EventHandler(this.ProcessFrame));
}
else
{
this.Button1.Text = "Stop";
Application.Idle += new System.EventHandler(new EventHandler(this.ProcessFrame));
// set form and Imagebox size
Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> frame = captureCV.QueryFrame;
grayA = frame.Convert<Emgu.CV.Structure.Gray, byte>();
pyrBufferA = new Emgu.CV.Image<Emgu.CV.Structure.Gray, byte>(frame.Width + 8, frame.Height / 3);
pyrBufferB = new Emgu.CV.Image<Emgu.CV.Structure.Gray, byte>(frame.Width + 8, frame.Height / 3);
// dimensiona i riquadri
ImageBox1.Width = System.Convert.ToInt32(frame.Width);
ImageBox1.Height = System.Convert.ToInt32(frame.Height);
this.Width = frame.Width + 30;
this.Height = frame.Height + 100;
}
captureInProgress = System.Convert.ToBoolean(!captureInProgress);
}
}
// add points with GoodFeaturesToTrack
private void Button2_Click(System.Object sender, System.EventArgs e)
{
defineCorners = true;
}
// add one point where the user clicks
public void ImageBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
bool removedpoint = false;
double radiussquare = System.Convert.ToDouble(Math.Pow(radius, 2));
//Dim puntitemp(0)() As PointF
if (count > 0)
{
int k = 0;
for (int i = 0; i <= count - 1; i++)
{
double dx = e.X - puntiB(0)[i].X;
double dy = e.Y - puntiB(0)[i].Y;
if (dx * dx + dy * dy >= radiussquare)
{
puntiA(0)[k] = puntiA(0)[i];
k++;
}
else
{
removedpoint = true;
}
}
count = k;
}
if (!removedpoint)
{
puntiBtemp(0) = new PointF[] { new PointF(e.X, e.Y) };
add_remove_pt = true;
}
}
}
private void Button3_Click(System.Object sender, System.EventArgs e)
{
count = 0;
}
//Dim times(1000) As Double
//Dim ii As Integer
private void ProcessFrame(object sender, EventArgs arg)
{
frame = captureCV.QueryFrame;
grayB = frame.Convert<Emgu.CV.Structure.Gray, byte>();
if (defineCorners)
{
count = 500;
puntiB = grayB.GoodFeaturesToTrack(count, 0.01, 10, 3);
grayB.FindCornerSubPix(puntiB, new System.Drawing.Size(10, 10), new System.Drawing.Size(-1, -1), new Emgu.CV.Structure.MCvTermCriteria(20, 0.03));
count = puntiB(0).Length;
defineCorners = false;
}
else if (count > 0)
{
//Dim start_time As Date = Now
// my overridden function with the "count" parameter added
Emgu.CV.OpticalFlow.PyrLK(grayA, grayB, pyrBufferA, pyrBufferB, count, puntiA(0), new Size(10, 10), 3, CvTermCriteria(20, 0.03M), flags, puntiB(0), status, errors);
flags = Emgu.CV.CvEnum.LKFLOW_TYPE.CV_LKFLOW_PYR_A_READY;
//' counts seconds from start_time declaration
//Dim stop_time As Date = Now
//Dim elapsed_time As TimeSpan = stop_time.Subtract(start_time)
//Dim timestamp As Double = elapsed_time.TotalMilliseconds / 1000
cSharp-code.rar_flow csharp_optical flow_them
版权申诉
116 浏览量
2022-09-20
18:29:49
上传
评论
收藏 2KB RAR 举报
刘良运
- 粉丝: 69
- 资源: 1万+
最新资源
- 甘晴void:一位多才多艺的编程新星.zip
- 基于C++的App图标资源库设计源码 - libicon
- 基于Java的日记本应用程序设计源码 - Diary
- 基于C#的.NET模板引擎设计源码 - jntemplate
- 基于51单片机+AC24C04+LCD1602显示的电子密码锁程序源代码及电路仿真.zip
- 基于C++的图形共享内存轻量级设计源码 - graphic_surface_lite
- 深入解析指令调度与延迟分支.zip
- 基于STC15F104E系列单片机的EEPROM应用程序测试例程KEIL工程源码.zip
- 基于STC15F104E系列单片机的串口通讯应用程序测试例程KEIL工程源码.zip
- java-leetcode题解之第844题比较含退格的字符串.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈