Thông báo

Collapse
No announcement yet.

tracker theo vật bằng webcam, dùng thư viện emguCV

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • tracker theo vật bằng webcam, dùng thư viện emguCV

    mình bị lỗi này mong các bạn giúp.
    Đoạn code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    using Emgu.CV;
    using Emgu.CV.CvEnum;
    using Emgu.CV.Structure;
    using Emgu.CV.UI;

    namespace Tracker
    {
    public partial class Form1 : Form
    {
    //member varible
    Capture capwebcam = null;
    bool blnCapturingInProcess = false;
    public Mat imgOriginal1;
    Image<Bgr, Byte> imgOriginal;

    Image<Gray, Byte> imgProcessed;

    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    try {
    Capture capwebcam = new Capture(); //create a camera captue
    // capwebcam = new Capture(Emgu.CV.CvEnum.CaptureType.DShow); ; // associate capture object to the defaut webcam
    } catch (NullReferenceException except) { // catch error if unsucessful
    //txtXY.Text = except.Message; // show error object message in text box
    MessageBox.Show(except.Message);
    return;
    }
    // wwhen we have a good capture object
    Application.Idle += processFrameAndUpdateGUI; // add process image function to application;s list of tasks
    blnCapturingInProcess = true;
    }
    private void Form1_FormClose(object sender, FormClosedEventArgs e)
    {
    if (capwebcam != null)
    {
    capwebcam.Dispose();
    }
    }
    /////////////
    void processFrameAndUpdateGUI(object sender, EventArgs arg)
    {
    imgOriginal1 = capwebcam.QueryFrame(); // get the next frame from the webcam
    imgOriginal = imgOriginal1.ToImage<Bgr, byte>();

    if (imgOriginal == null) return; // if we did not get a frame
    imgProcessed = imgOriginal.InRange(new Bgr(0, 0, 175), // min filter value if color is greater than or equal to this
    new Bgr(100, 100, 256)); // max filter value if color less than or equal to this
    imgProcessed = imgProcessed.SmoothGaussian(9); // we call SmoothGaussian with only one param, that being the x ang y size of the filter window
    CircleF[] circles = imgProcessed.HoughCircles(new Gray(100), //canny threshold
    new Gray(50), // accumulator threshold
    2, // size of image/this param = "accumulator resoltion
    imgProcessed.Height / 4, // min distance in pixel between the centers of detected circles
    10, // min radius of detected circles
    400)[0]; // max radius of detected circle, get circle from the first channel
    foreach (CircleF circle in circles)
    {
    if (txtXY.Text != "") txtXY.AppendText(Environment.NewLine); // if we are not on the first line in the text box, insert the new line char
    txtXY.AppendText("ball position = x" + circle.Center.X.ToString().PadLeft(4) + // x position of center point of circle
    ", y = " + circle.Center.Y.ToString().PadLeft(4) + // y position of center point of circle
    " radius = " + circle.Radius.ToString("###.000").PadLeft(7)); // radius circle
    txtXY.ScrollToCaret(); //move the text box croll bar down to the line

    // draw small green cirlce at the center of the detected object
    // to do this , we will call opencv1.x function, this is nessesary
    // we will draw radius 3, even though the size of the dectected circle will be much bigger
    // the CVInvoke object can be used to make opencv1.x function calls

    CvInvoke.Circle(imgOriginal, //draw on the original image
    new Point((int)circle.Center.X, (int)circle.Center.Y), // center point of circle
    3, // radius of circle
    new MCvScalar(0, 255, 0), // draw pure green
    -1, // thickness of the circle, -1 indicates to fill the circle
    LineType.AntiAlias, // use AntiAlias to smooth the pixels
    0); // no shift

    // draw red circle around the detected oject
    imgOriginal.Draw(circle,
    new Bgr(Color.Red),
    3); //thickness of the circle pixels
    }
    ibOriginal.Image = imgOriginal;
    ibProcess.Image = imgProcessed;
    }

    private void btPauseandResume_Click(object sender, EventArgs e)
    {
    if (blnCapturingInProcess == true)
    {
    Application.Idle -= processFrameAndUpdateGUI;
    blnCapturingInProcess = false;
    btPauseandResume.Text = "resume";
    }
    else
    {
    Application.Idle += processFrameAndUpdateGUI;
    blnCapturingInProcess = true;
    btPauseandResume.Text = "pause";
    }
    }

    private void btStart_Click(object sender, EventArgs e)
    {
    processFrameAndUpdateGUI(null ,null);
    }

    }
    }Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	35.6 KB
ID:	1423039

Về tác giả

Collapse

Thang_Hoang Tìm hiểu thêm về Thang_Hoang

Bài viết mới nhất

Collapse

Đang tải...
X