目录
c# opencvsharp tracker 目标追踪
效果
项目
代码
using opencvsharp;
using opencvsharp.extensions;
using opencvsharp.tracking;
using system;
using system.drawing;
using system.reflection;
using system.windows.forms;
namespace c__opencvsharp_tracker_目标追踪
{
public partial class form1 : form
{
public form1()
{
initializecomponent();
}
string filename = "";
bool play = false;
tracker tracker;
videocapture capture;
bool m_mousedown = false;
bool m_mousemove = false;
system.drawing.point startpoint = new system.drawing.point();
system.drawing.point endpoint = new system.drawing.point();
mat currentframe = new mat();
rect roi = new rect();
private void form1_load(object sender, eventargs e)
{
combobox1.selectedindex = 0;
toolstripstatuslabel1.text = "请打开视频文件";
label2.text = "";
}
private void button1_click(object sender, eventargs e)
{
openfiledialog ofd = new openfiledialog();
ofd.filter = "video files (*.avi)|*.avi|mp4 files (*.mp4)|*.mp4";
ofd.restoredirectory = true;
ofd.checkfileexists = true;
if (ofd.showdialog() == dialogresult.ok)
{
filename = ofd.filename;
toolstripstatuslabel1.text = filename;
capture = new videocapture(filename);
if (!capture.isopened())
{
toolstripstatuslabel1.text = " 打开视频文件失败";
return;
}
capture.read(currentframe);
if (!currentframe.empty())
{
picturebox1.image = bitmapconverter.tobitmap(currentframe);
timer1.interval = (int)(1000.0 / capture.fps);
timer1.enabled = true;
m_mousemove = false;
m_mousedown = false;
picturebox2.image = null;
}
}
}
private void button2_click(object sender, eventargs e)
{
play = true;
if (picturebox2.image != null)
{
switch (combobox1.selectedindex)
{
case 0:
default:
tracker = trackercsrt.create();
break;
case 1:
tracker = trackergoturn.create();
break;
case 2:
tracker = trackerkcf.create();
break;
case 3:
tracker = trackermil.create();
break;
}
tracker.init(currentframe, roi);
}
}
private void button3_click(object sender, eventargs e)
{
play = false;
}
private void timer1_tick(object sender, eventargs e)
{
if (play)
{
capture.read(currentframe);
if (currentframe.empty())
{
play = false;
picturebox1.image = null;
picturebox2.image = null;
timer1.enabled = false;
return;
}
if (picturebox2.image != null && tracker != null)
{
tracker.update(currentframe, ref roi);
cv2.rectangle(currentframe, roi, scalar.red);
label2.text = string.format("roi:({0},{1})-({2},{3})", roi.x, roi.y, roi.x + roi.width, roi.y + roi.height);
}
picturebox1.image = bitmapconverter.tobitmap(currentframe);
}
}
private void picturebox1_paint(object sender, painteventargs e)
{
if (!m_mousedown || !m_mousemove)
return;
graphics g = e.graphics;
pen p = new pen(color.blue, 2);
rectangle rect = new rectangle(startpoint.x, startpoint.y, (endpoint.x - startpoint.x), (endpoint.y - startpoint.y));
g.drawrectangle(p, rect);
}
private void picturebox1_mousemove(object sender, mouseeventargs e)
{
if (picturebox1.image == null)
return;
if (!m_mousedown) return;
m_mousemove = true;
endpoint = e.location;
picturebox1.invalidate();
}
private void picturebox1_mouseup(object sender, mouseeventargs e)
{
if (!m_mousedown || !m_mousemove)
return;
m_mousedown = false;
m_mousemove = false;
system.drawing.point image_startpoint = convertcooridinate(startpoint);
system.drawing.point image_endpoint = convertcooridinate(endpoint);
if (image_startpoint.x < 0)
image_startpoint.x = 0;
if (image_startpoint.y < 0)
image_startpoint.y = 0;
if (image_endpoint.x < 0)
image_endpoint.x = 0;
if (image_endpoint.y < 0)
image_endpoint.y = 0;
if (image_startpoint.x > currentframe.cols)
image_startpoint.x = currentframe.cols;
if (image_startpoint.y > currentframe.rows)
image_startpoint.y = currentframe.rows;
if (image_endpoint.x > currentframe.cols)
image_endpoint.x = currentframe.cols;
if (image_endpoint.y > currentframe.rows)
image_endpoint.y = currentframe.rows;
label2.text = string.format("roi:({0},{1})-({2},{3})", image_startpoint.x, image_startpoint.y, image_endpoint.x, image_endpoint.y);
int w = (image_endpoint.x - image_startpoint.x);
int h = (image_endpoint.y - image_startpoint.y);
if (w > 10 && h > 10)
{
roi = new rect(image_startpoint.x, image_startpoint.y, w, h);
mat roi_mat = currentframe[roi];
picturebox2.image = bitmapconverter.tobitmap(roi_mat);
}
}
private void picturebox1_mousedown(object sender, mouseeventargs e)
{
if (picturebox1.image == null)
return;
play = false;
m_mousedown = true;
startpoint = e.location;
}
private system.drawing.point convertcooridinate(system.drawing.point point)
{
system.reflection.propertyinfo rectangleproperty = this.picturebox1.gettype().getproperty("imagerectangle", bindingflags.instance | bindingflags.nonpublic);
rectangle picturebox = (rectangle)rectangleproperty.getvalue(this.picturebox1, null);
int zoomedwidth = picturebox.width;
int zoomedheight = picturebox.height;
int imagewidth = picturebox1.image.width;
int imageheight = picturebox1.image.height;
double zoomratex = (double)(zoomedwidth) / (double)(imagewidth);
double zoomratey = (double)(zoomedheight) / (double)(imageheight);
int black_left_width = (zoomedwidth == this.picturebox1.width) ? 0 : (this.picturebox1.width - zoomedwidth) / 2;
int black_top_height = (zoomedheight == this.picturebox1.height) ? 0 : (this.picturebox1.height - zoomedheight) / 2;
int zoomedx = point.x - black_left_width;
int zoomedy = point.y - black_top_height;
system.drawing.point outpoint = new system.drawing.point();
outpoint.x = (int)((double)zoomedx / zoomratex);
outpoint.y = (int)((double)zoomedy / zoomratey);
return outpoint;
}
}
}
using opencvsharp;
using opencvsharp.extensions;
using opencvsharp.tracking;
using system;
using system.drawing;
using system.reflection;
using system.windows.forms;
namespace c__opencvsharp_tracker_目标追踪
{
public partial class form1 : form
{
public form1()
{
initializecomponent();
}
string filename = "";
bool play = false;
tracker tracker;
videocapture capture;
bool m_mousedown = false;
bool m_mousemove = false;
system.drawing.point startpoint = new system.drawing.point();
system.drawing.point endpoint = new system.drawing.point();
mat currentframe = new mat();
rect roi = new rect();
private void form1_load(object sender, eventargs e)
{
combobox1.selectedindex = 0;
toolstripstatuslabel1.text = "请打开视频文件";
label2.text = "";
}
private void button1_click(object sender, eventargs e)
{
openfiledialog ofd = new openfiledialog();
ofd.filter = "video files (*.avi)|*.avi|mp4 files (*.mp4)|*.mp4";
ofd.restoredirectory = true;
ofd.checkfileexists = true;
if (ofd.showdialog() == dialogresult.ok)
{
filename = ofd.filename;
toolstripstatuslabel1.text = filename;
capture = new videocapture(filename);
if (!capture.isopened())
{
toolstripstatuslabel1.text = " 打开视频文件失败";
return;
}
capture.read(currentframe);
if (!currentframe.empty())
{
picturebox1.image = bitmapconverter.tobitmap(currentframe);
timer1.interval = (int)(1000.0 / capture.fps);
timer1.enabled = true;
m_mousemove = false;
m_mousedown = false;
picturebox2.image = null;
}
}
}
private void button2_click(object sender, eventargs e)
{
play = true;
if (picturebox2.image != null)
{
switch (combobox1.selectedindex)
{
case 0:
default:
tracker = trackercsrt.create();
break;
case 1:
tracker = trackergoturn.create();
break;
case 2:
tracker = trackerkcf.create();
break;
case 3:
tracker = trackermil.create();
break;
}
tracker.init(currentframe, roi);
}
}
private void button3_click(object sender, eventargs e)
{
play = false;
}
private void timer1_tick(object sender, eventargs e)
{
if (play)
{
capture.read(currentframe);
if (currentframe.empty())
{
play = false;
picturebox1.image = null;
picturebox2.image = null;
timer1.enabled = false;
return;
}
if (picturebox2.image != null && tracker != null)
{
tracker.update(currentframe, ref roi);
cv2.rectangle(currentframe, roi, scalar.red);
label2.text = string.format("roi:({0},{1})-({2},{3})", roi.x, roi.y, roi.x + roi.width, roi.y + roi.height);
}
picturebox1.image = bitmapconverter.tobitmap(currentframe);
}
}
private void picturebox1_paint(object sender, painteventargs e)
{
if (!m_mousedown || !m_mousemove)
return;
graphics g = e.graphics;
pen p = new pen(color.blue, 2);
rectangle rect = new rectangle(startpoint.x, startpoint.y, (endpoint.x - startpoint.x), (endpoint.y - startpoint.y));
g.drawrectangle(p, rect);
}
private void picturebox1_mousemove(object sender, mouseeventargs e)
{
if (picturebox1.image == null)
return;
if (!m_mousedown) return;
m_mousemove = true;
endpoint = e.location;
picturebox1.invalidate();
}
private void picturebox1_mouseup(object sender, mouseeventargs e)
{
if (!m_mousedown || !m_mousemove)
return;
m_mousedown = false;
m_mousemove = false;
system.drawing.point image_startpoint = convertcooridinate(startpoint);
system.drawing.point image_endpoint = convertcooridinate(endpoint);
if (image_startpoint.x < 0)
image_startpoint.x = 0;
if (image_startpoint.y < 0)
image_startpoint.y = 0;
if (image_endpoint.x < 0)
image_endpoint.x = 0;
if (image_endpoint.y < 0)
image_endpoint.y = 0;
if (image_startpoint.x > currentframe.cols)
image_startpoint.x = currentframe.cols;
if (image_startpoint.y > currentframe.rows)
image_startpoint.y = currentframe.rows;
if (image_endpoint.x > currentframe.cols)
image_endpoint.x = currentframe.cols;
if (image_endpoint.y > currentframe.rows)
image_endpoint.y = currentframe.rows;
label2.text = string.format("roi:({0},{1})-({2},{3})", image_startpoint.x, image_startpoint.y, image_endpoint.x, image_endpoint.y);
int w = (image_endpoint.x - image_startpoint.x);
int h = (image_endpoint.y - image_startpoint.y);
if (w > 10 && h > 10)
{
roi = new rect(image_startpoint.x, image_startpoint.y, w, h);
mat roi_mat = currentframe[roi];
picturebox2.image = bitmapconverter.tobitmap(roi_mat);
}
}
private void picturebox1_mousedown(object sender, mouseeventargs e)
{
if (picturebox1.image == null)
return;
play = false;
m_mousedown = true;
startpoint = e.location;
}
private system.drawing.point convertcooridinate(system.drawing.point point)
{
system.reflection.propertyinfo rectangleproperty = this.picturebox1.gettype().getproperty("imagerectangle", bindingflags.instance | bindingflags.nonpublic);
rectangle picturebox = (rectangle)rectangleproperty.getvalue(this.picturebox1, null);
int zoomedwidth = picturebox.width;
int zoomedheight = picturebox.height;
int imagewidth = picturebox1.image.width;
int imageheight = picturebox1.image.height;
double zoomratex = (double)(zoomedwidth) / (double)(imagewidth);
double zoomratey = (double)(zoomedheight) / (double)(imageheight);
int black_left_width = (zoomedwidth == this.picturebox1.width) ? 0 : (this.picturebox1.width - zoomedwidth) / 2;
int black_top_height = (zoomedheight == this.picturebox1.height) ? 0 : (this.picturebox1.height - zoomedheight) / 2;
int zoomedx = point.x - black_left_width;
int zoomedy = point.y - black_top_height;
system.drawing.point outpoint = new system.drawing.point();
outpoint.x = (int)((double)zoomedx / zoomratex);
outpoint.y = (int)((double)zoomedy / zoomratey);
return outpoint;
}
}
}
下载
发表评论