รวบรวมวิธีลง OpenCV 3.1 บน Mac OS X (10.11)

Tutorial ที่นำมารวบรวมนี้ ต้นทางใช้กับ OS X 10.9 พร้อม OpenCV 3.0 แต่วิธีการ install ยังเหมือนเดิมใน OS X 10.11 พร้อม OpenCV 3.1 ครับ

สิ่งที่ต้องใช้

  1. CMake for Mac
  2. OpenCV 3.1 (download from opencv.org)
  3. Xcode (download from Mac App Store – Free)

วิธีการ Setup OpenCV
http://blogs.wcode.org/2014/10/howto-install-build-and-use-opencv-macosx-10-10/

วิธีการ Setup ให้ใช้ OpenCV กับ Xcode ได้
http://blogs.wcode.org/2014/11/howto-setup-xcode-6-1-to-work-with-opencv-libraries/

จากนั้น ให้ทดสอบรันโปรแกรมดู

#include "iostream"
#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

int main() {
 Mat frame, prev_frame, diff, gray, bw, struct_element;
 vector<vector<Point> > contours;
 vector<Vec4i> hierarchy;
 RNG rng(12345);
 bool motion = false, tracking = false;
 
 // 0 is the ID of the built-in laptop camera, change if you want to use other camera
 VideoCapture cap(0);
 
 //check if the file was opened properly
 if(!cap.isOpened())
 {
 cout << "Capture could not be opened successfully" << endl;
 return -1; }
 namedWindow("Video");
 
 // Play the video in a loop till it ends
 while(char(waitKey(1)) != 'q' && cap.isOpened()) {
 cap >> frame;
 
 if (char(waitKey(1)) == 'm') {
 cout << "toggled motion" << endl;
 motion = ~motion;
 tracking = false;
 }
 
 if (char(waitKey(1)) == 't') {
 cout << "toggled tracking" << endl;
 tracking = ~tracking;
 motion = false;
 }
 
 if (motion) {
 absdiff(frame, prev_frame, diff);
 imshow("Video", diff);
 }
 else if (tracking) {
 absdiff(frame, prev_frame, diff);
 cvtColor(diff, diff, CV_RGB2GRAY);
 threshold(diff, bw, 50, 255, THRESH_BINARY);
 struct_element = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1) );
 erode(bw, bw, struct_element);
 dilate(bw, bw, struct_element);
 findContours( bw, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
 
 vector<vector<Point> > contours_poly( contours.size() );
 vector<Rect> boundRect( contours.size() );
 vector<Point2f>center( contours.size() );
 vector<float>radius( contours.size() );
 
 int csize, csize2;
 Point tl, br;
 csize = contours.size();
 
 if (csize >= 2) {
 csize2 = contours[csize-1].size();
 tl = contours[0][0];
 br = contours[csize-1][csize2-1];
 
 Scalar color(rand()*255, rand()*255, rand()*255);
 rectangle(frame, tl, br, color);
 }
 
 /*for( size_t i = 0; i < contours.size(); i++ ) {
 approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
 boundRect[i] = boundingRect( Mat(contours_poly[i]) );
 minEnclosingCircle( contours_poly[i], center[i], radius[i] );
 }
 
 Mat drawing = Mat::zeros( bw.size(), CV_8UC3 );
 
 for( size_t i = 0; i< contours.size(); i++ ) {
 Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
 drawContours( drawing, contours_poly, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point() );
 rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
 circle( drawing, center[i], (int)radius[i], color, 2, 8, 0 );
 }*/
 
 imshow("Video", frame);
 }
 else {
 imshow("Video", frame);
 }
 
 prev_frame = frame.clone();
 
 //cout << motion << endl;
 }
 
 return 0;
}

Screen Shot 2559-08-21 at 11.50.36 PM

 

จบแล้ว 🙂

Published by

Pargorn Puttapirat

Master student at Department of Computer Science and Technology, Xi'an Jiaotong University, China Currently working on Biomedical Text Mining Alumni of Department of Biomedical Engineering, Srinakharinwirot University, Thailand

Leave a Reply