Group Blog
 
<<
กรกฏาคม 2554
 12
3456789
10111213141516
17181920212223
24252627282930
31 
 
31 กรกฏาคม 2554
 
All Blogs
 
Emgu OpenCV on C# -- Webcam ตอนที่ 1/2

บทความนี้จะแนะนำการใช้ C# ร่วมกับ Emgu OpenCV เพื่อทำการติดต่อกล้อง หากท่านผู้อ่านยังมองภาพรวมของการใช้งาน C# ร่วมกับ Emgu OpenCV ไม่ออก หรือไม่มีพื้นฐานการใช้งาน C# แบบ Window Application ผมขอแนะนำให้อ่านที่นี่ก่อน

1. เริ่มจากการออกแบบรูปร่างหน้าตาของ GUI โดยการนำ pictureBox, button และ timer มาวางบนฟอร์ม จากนั้นกำหนดตำแหน่งและขนาดตามความต้องการ ดังนี้


2. ทำการกำหนด Time Interval ของ timer1 ให้มีค่าเท่ากับ 50mS (หรือค่าอื่นๆ ตามความต้องการ)


3. ให้ดับเบิลคลิกที่ button1 ตัว IDE จะสร้างฟังก์ชั่นของ button1 ให้โดยอัตโนมัติดังนี้

private void button2_Click(object sender, EventArgs e){  

}


4. กลับไปที่หน้าต่างออกแบบ GUI (Form1.cs [design]) อีกครั้ง ให้ดับเบิลคลิกที่ button1 ตัว IDE จะสร้างฟังก์ชั่นของ button2 ให้โดยอัตโนมัติเช่นกัน ดังนี้

private void button2_Click(object sender, EventArgs e){  

}

5. ต่อไปให้ทำกับ timer1 เหมือนกับที่ได้ทำไปในส่วนของ button1 และ button2 จะได้ฟังก์ชั่นของ timer1 ดังนี้

private void timer1_Tick(object sender, EventArgs e){  

}

6. ต่อไปทำการ Add reference เข้ามาใน Project ขั้นตอนนี้ทำอย่างไรให้ศึกษาที่นี่


7. เริ่มเขียนโปรแกรม เริ่มจากทำการบอกตัว C# ว่าเราต้องการใช้ความสามารถของ Emgu โดยการเขียนโปรแกรมส่วนนี้ไว้ด้านบน (ให้วางต่อจาก using  xxx ที่มีอยู่ก่อนหน้า)

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

8. ทำการประกาศตัวแปรสำหรับใช้ในการอ้างอิงถึงกล้อง

namespace Camera{  
public partial class Form1 : Form{
Emgu.CV.Capture capture; // <---- เพิ่มบรรทัดนี้ลงไป (บรรทัดอื่นๆ คือส่วนที่ IDE สร้างไว้ให้ก่อนหน้า)
public Form1(){
InitializeComponent();
}

9. เขียนโปรแกรมในส้วนของฟังก์ชัน Form1_Load

private void Form1_Load(object sender, EventArgs e){  
capture = new Emgu.CV.Capture();
if (capture == null){ // หากไม่สามารถเชื่มต่อกล้องได้ให้ทำการ...
MessageBox.Show("Cannot connect to the device"); // แสดงข้อความให้ทราบ และ...
Application.Exit(); // ออกจากโปรแกรม
}
button1_Click(null, null); // หากเชื่มต่อกล้องได้ให้เรียกฟังก์ชั่น button1_Click
}

10.เขียนโปรแกรมในส่วนของ button1_Click, button2_Click และ timer1_Tick ดังนี้

private void button1_Click(object sender, EventArgs e){  
timer1.Start(); // ให้ timer1 ทำงาน
button1.Enabled = false;
button2.Enabled = true;
}

private void button2_Click(object sender, EventArgs e){
timer1.Stop(); // ให้ timer1 หยุดทำงาน
button1.Enabled = true;
button2.Enabled = false;
}

private void timer1_Tick(object sender, EventArgs e){
ProcessFrame(); // เมื่อ timer1 นับเวลาครบตามที่กำหนด (50mS) จะเรียกฟังก์ชั่นนี้
}

11. สร้างฟังก์ชั่นใหม่ชื่อ ProcessFrame() มีรายละอียดดังนี้

private void ProcessFrame(){  
Emgu.CV.Image<Bgr, Byte> cameraImage = capture.QueryFrame(); // อ่านข้อมูลภาพจากกล้องมา 1 เฟรม
if (cameraImage != null) // ถ้าอ่านข้อมูลภาพได้ให้...
pictureBox1.Image = cameraImage.ToBitmap(); แสดงผลภาพที่ pictureBox1
else{ // ถ้าอ่านข้อมูลภาพไม่ได้ให้...
button2_Click(null, null); // เรียก button2_Click() และ
MessageBox.Show("Cannot capture an image"); // แสดงผลข้อความให้ทราบ ตามด้วย...
Application.Exit(); // ออกจากโปรแกรม
}
}

12. สุดท้าย ให้ทำการ Build และ Run ก็จะเห็นภาพหล่อๆ สวยๆ ปรากฎขึ้นมาให้เห็น


โปรแกรมแบบสมบูรณ์ และ Project File สามารถดาวน์โหลดได้จากที่นี่





Create Date : 31 กรกฎาคม 2554
Last Update : 31 กรกฎาคม 2554 11:04:26 น. 0 comments
Counter : 5486 Pageviews.

TheInsight
Location :


[ดู Profile ทั้งหมด]

ฝากข้อความหลังไมค์
Rss Feed
Smember
ผู้ติดตามบล็อก : 3 คน [?]




Links
 

 Pantip.com | PantipMarket.com | Pantown.com | © 2004 BlogGang.com allrights reserved.