Menu

Showing posts with label OpenCV. Show all posts
Showing posts with label OpenCV. Show all posts

Saturday, June 8, 2013

[OpenCV] วิธีการบันทึกไฟล์วิดีโอ
[OpenCV] How to Write/Save Video File

ขั้นแรก นิยามตัวแปรที่ใช้เขียนไฟล์วิดีโอตามด้านล่างนี้
First, define the video writer as follow
CvVideoWriter* writer = cvCreateVideoWriter(filepath,fourccfpsframe_size, iscolor);

โดยที่
filepath เป็นที่อยู่ของวิดีโอที่ต้องการจะบันทึก ในที่นี้ใช้นามสกุล avi
fourcc เป็น codec ของวิดีโอที่จะบันทึก ในที่นี้ใช้ CV_FOURCC('M','J','P','G') 
where
filepath is the path of output video file, in this case, file extension is avi
fourcc is the codec of video, in this case, CV_FOURCC('M','J','P','G') is used

ขั้นที่สอง เขียนเฟรมแต่ละเฟรมไปยังวิดีโอที่บันทึก
Second, write frame to output video as follow
cvWriteFrame(writer, frame);

ขั้นสุดท้าย ปลดปล่อยหน่วยความจำสำหรับตัวแปรที่ใช้เขียนไฟล์วิดีโอ
Last, release the video writer 
cvReleaseVideoWriter(&writer);

Friday, June 7, 2013

[OpenCV] วิธีการตรวจจับใบหน้า โดยใช้ Haar Cascade Classifier
[OpenCV] How to Detect Face using Haar Cascade Classifier

สมมติว่า img เป็นภาพสีสำหรับตรวจจับใบหน้า
Assume that img is color image for face detection

  1. const char* cascade_name = "haarcascade_frontalface_alt.xml";
  2. static CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name );
  3. static CvMemStorage* storage = cvCreateMemStorage(0);

  4. CvSeq* detectFace(IplImage* img){
  5.      
  6.      static IplImage* gray=0;
  7.      if(gray && (gray->width!=img->width || gray->height!= img->height)){
  8.           cvReleaseImage(&gray);
  9.           gray=0;
  10.      }
  11.      if(!gray)
  12.           gray=cvCreateImage(cvGetSize(img),8,1);

  13.      cvCvtColor(img,gray,CV_BGR2GRAY);
  14.      cvClearMemStorage( storage );
  15.      CvSeq* faces = cvHaarDetectObjects( gray, cascade, storage, 1.1, 3, CV_HAAR_DO_CANNY_PRUNING, cvSize(100, 100) );
  16.      return faces;
  17. }

[OpenCV] วิธีเล่นวิดีโอจากไฟล์
[OpenCV] How to Play Video From File

  1. CvCapture* capture=cvCreateFileCapture("E:/Wildlife.wmv");
  2. IplImage* img;
  3. char c;

  4. double frame_width=cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH );
  5. double frame_height=cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT  );
  6. double fps=cvGetCaptureProperty(capture,CV_CAP_PROP_FPS  );
  7. printf("%f %f %f",frame_width, frame_height,fps);

  8. cvNamedWindow("test",1);
  9. img=cvQueryFrame(capture);
  10. while(img){
  11. cvShowImage("test",img);
  12. c=cvWaitKey(30);
  13. if(c==27)
  14. break;
  15. img=cvQueryFrame(capture);
  16. }
  17. cvReleaseCapture(&capture);

[OpenCV] วิธีการดึงภาพจากกล้อง
[OpenCV] How to Capture Image From Camera

  1. CvCapture* capture=cvCreateCameraCapture(0);
  2. IplImage* img;
  3. char c;

  4. cvNamedWindow("test",1);
  5. while(true){
  6.   img=cvQueryFrame(capture);
  7.   cvShowImage("test",img);
  8.   c=cvWaitKey(30);
  9.   if(c==27)
  10.     break;
  11. }
  12. cvReleaseCapture(&capture);

Friday, April 12, 2013

[OpenCV] วิธีการใช้ OpenCV บน Ubutu
[OpenCV] How to Use OpenCV on Ubuntu

สร้างไฟล์ใหม่ชื่อ test.cpp แล้วใส่โค้ดที่ใช้ OpenCV ตามด้านล่าง
Create a program using OpenCV by creating new text file and inserting the code as below then saving as test.cpp
  1. #include<stdio.h>
  2. #include<opencv/cv.h>
  3. #include<opencv/highgui.h>

  4. int main(){
  5.   IplImage* img=cvLoadImage("test.jpg",1);
  6. cvNamedWindow("Test",1);
  7. cvShowImage("Test",img);
  8. cvWaitKey();
  9. return 0;
  10. }

Monday, April 1, 2013

[OpenCV] การติดตั้ง OpenCV บน Ubuntu
[OpenCV] OpenCV Installation on Ubuntu

สมมติว่า แฟ้ม OpenCV ถูกสร้างอยู่ในแฟ้ม Home เรียบร้อยแล้ว
Assume that OpenCV folder is created in Home folder

ขั้นตอนแรก ดาวน์โหลด source code ของ OpenCV มาไว้ในเครื่อง โดยมีอยู่สองทางเลือก
First, get OpenCV source code. There are two options.

- ดาวน์โหลดเวอร์ชั่นที่เสถียรล่าสุดของ OpenCV
- Get the Latest Stable OpenCV Version

Wednesday, March 13, 2013

[OpenCV] การติดตั้งแพ็คเกจที่จำเป็นสำหรับการติดตั้ง OpenCV บน Ubuntu
[OpenCV] Required Packages Installation for OpenCV on Ubuntu

ก่อนจะทำการติดตั้ง OpenCV บน Ubuntu จำเป็นจะต้องลงแพ็คเกจที่จำเป็นเสียก่อน (ตาม เอกสาร OpenCV)
There are some required packages for OpenCV installation on Ubuntu. (see OpenCV document)

โพสต์นี้จะอธิบายการติดตั้งแต่ละแพ็คเกจ แต่ก่อนอื่น คุณจำเป็นจะต้องรู้เกี่ยวกับ Terminal และ Ubuntu Software Center เสียก่อน (อ่าน วิธีการเรียกใช้ Terminal บน Ubuntu และ วิธีการใช้ Ubuntu Software Center)
The installation of each package is described in this post. Before starting, you need to know about Terminal and Ubuntu Software Center (see How to run Terminal on Ubuntu and How to use Ubuntu Software Center).

Monday, December 3, 2012

Sunday, November 25, 2012

[OpenCV] OpenCV คืออะไร?
[OpenCV] What is OpenCV?

OpenCV ย่อมาจาก Open Source Computer Vision ซึ่งเป็นไลบรารี่ที่รวบรวมฟังก์ชั่นต่างๆสำหรับการประมวลผลภาพและคอมพิวเตอร์วิทัศนศาสตร์เอาไว้เป็นจำนวนมาก ไลบรารี่นี้อยู่ภายใต้ใบอนุญาต BSD ซึ่งเราสามารถใช้ได้ฟรีทั้งทางด้านการศึกษาและทางการค้า นอกจากนั้น OpenCV ยังมีอินเตอร์เฟสที่หลากหลายรองรับการพัฒนาโปรแกรมบนภาษาโปรแกรมต่างๆ เช่น C/C++, Python, Java เป็นต้น และ OpenCV ยังสามารถรันได้ทั้งบน Window, Linux, Android, และ Mac

OpenCV (Open Source Computer Vision) is a library of programming functions for Image Processing and Computer Vision. It is released under the BSD license that is free for both academic and commercial use. It has C/C++, Python and Java interfaces running on Window, Linux, Android, and Mac.