Chủ Nhật, 20 tháng 4, 2014

Auto capture frame from Video using AForge and C#

1. Bài toán: Thiết kế 1 chương trình đọc dữ liệu từ webcam (hoặc camera số), dùng cho việc ghi nhận các khung hình thành file ảnh. Mỗi ảnh cách nhau 1 giây. File ghi ra ổ đĩa D, tên file là ảnh kèm với cảnh, kèm với số thứ tự hình. Ví dụ: anh11.jpg, anh12.jpg, anh21.jpg, anh22.jpg. Trong đó chỉ số đầu chỉ số thứ tự cảnh, chỉ số thứ hai chỉ số thứ tự ảnh. Các giá trị cảnh và số lượng ảnh cần chụp trong cảnh có thể cho trực tiếp trên form.
(Ghi chú: Bài này giao cho 1 member viết, nhưng member đó không làm được, mình phải tự làm)


2. Phương pháp thiết kế:
2.1. Trong Project có sử dụng các công cụ của C#: 
- Tinmer: dùng cho việc điều khiển xung nhịp đồng hồ, mỗi xử lý cách nhau 1000ms.
- TableLayoutPanel: Chia màn hình 2 cột 1 dòng. Ô thứ nhất chứa Video quay được từ webcam, ô thứ hai chứa khung hình chụp được theo nhịp kim đồng hồ (có chứa số thứ tự ảnh).
- PictureBox, được kéo thả vào 2 ô của bảng. Tất nhiên cần điều chỉnh tham số Zoom và "Doc in parent container" để nó thay đổi kích thước khi form thay đổi kích thước.
- Command thứ nhất (Start) để bắt đầu thu nhận dữ liệu video từ webcam
- Command thứ hai (Auto Capture) để bắt đầu thu nhận các khung hình.
- TextBox thứ nhất (Scenes) chứa số hiệu cảnh (bắt đầu là 1).
- TextBox thứ hai (Amount of Frames) chứa số frame cần chụp của cảnh. (mặc định là 10).

2.2. Các thư viện tham khảo trong AForge
Cần phải Add các thư viện AFore, AFore.Video, AFore.Video.DirectShow vào trong mục tham khảo của Project thì mới sử dụng được các hàm của những thư viện này trong khi coding.



(Tiếp tục cập nhật)

3. Coding:

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;

//Khai bao sử dụng các thư viện của AForge
using AForge;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Drawing.Imaging;
using System.IO;

namespace MyVideo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //Khai bao bien toan cuc
        private FilterInfoCollection CaptureDevice;
        private VideoCaptureDevice FinalFrame;

        // Các biến sử dụng cho Auto Caputer
        Bitmap video, video2;
        Graphics g;
        bool OnOff = false;
        int thoigiandemnguoc; //Số lượng ảnh cần chụp của 1 cảnh, mỗi ảnh cách nhau 1 giây
        int scenes=1;  //Số thứ tự cảnh chụp

        private void Form1_Load(object sender, EventArgs e)
        {
            //Lấy các thiết bị Webcam, máy quay đang gắn vào máy tính làm dữ liệu cho combobox
            textBox1.Text = "10";
            textBox2.Text = "1";
            CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo Device in CaptureDevice)
            {
                comboBox1.Items.Add(Device.Name);
            }
            comboBox1.SelectedIndex = 0;
            FinalFrame = new VideoCaptureDevice();
        }

        //Khi nút lệnh Start được bấm
        private void button1_Click(object sender, EventArgs e)
        {
            FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString);
            FinalFrame.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);
            FinalFrame.Start();
        }

        private void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
            video = (Bitmap)eventArgs.Frame.Clone();
            Bitmap video2 = (Bitmap) eventArgs.Frame.Clone();
            if (OnOff==true)
            {
                g = Graphics.FromImage(video2);
                g.DrawString(thoigiandemnguoc.ToString(), new Font("Arial", 40), new SolidBrush(Color.Red), new PointF(2, 2));
                g.Dispose();
                pictureBox2.Image = video2;
            
            }
      }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            FinalFrame.Stop();
        }

    
       
        private void timer1_Tick(object sender, EventArgs e)
        {
            thoigiandemnguoc--;

            if (thoigiandemnguoc==0)
            {
                timer1.Enabled = false;
                OnOff = false;
                MessageBox.Show("Đã lưu đủ số ảnh");
                scenes=scenes+1;
                textBox2.Text = scenes.ToString();
               // FinalFrame.Stop();
            }
            pictureBox1.Image = video;
            
          //  pictureBox2.Image = video;
            pictureBox2.Image.Save(@"D:\anh" + scenes.ToString() + thoigiandemnguoc.ToString() + ".jpg", ImageFormat.Jpeg);
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox1 != null)
            {
                timer1.Enabled = true;
                OnOff = true;
                thoigiandemnguoc = int.Parse(textBox1.Text);
            }
            
        }
    }
}


4. Download Project: 



Không có nhận xét nào:

Đăng nhận xét