1 //Psychlops Code Template
\r
2 // Please visit following web site to get sample codes.
\r
3 // http://psychlops.sourceforge.jp/ja/?StartCode
\r
4 // CodeDresser at following address is also available to view the code.
\r
5 // http://visitope.org/Tools/codedresser.html
\r
9 ///+ 0 Setup Psychlops Circumstances
\r
10 //// 0 Setup Psychlops Circumstances
\r
11 #include <opencv2/imgproc/imgproc.hpp>
\r
12 #include <opencv2/video/tracking.hpp>
\r
13 #include <opencv2/features2d/features2d.hpp>
\r
14 #include <opencv2/highgui/highgui.hpp>
\r
15 #include <psychlops.h>
\r
16 //#include "psychlops_ext_cv1.h"
\r
17 using namespace Psychlops;
\r
18 // Psychlops Win32 1.6.0 / 20120220
\r
19 ///- 0 Setup Psychlops Circumstances
\r
22 void psychlops_main() {
\r
25 ///+ 1 Declaration /////////////////////////////////////////////////////////////
\r
27 // declare default window and variables for its parameters
\r
28 Canvas cnvs(Canvas::window, Display::secondary);
\r
29 double CANVAS_FRAMENUM;
\r
30 int CANVAS_REFRESHRATE;
\r
31 Psychlops::Color DEFAULT_BG_COLOR;
\r
33 //declare local variables around here
\r
35 ///- 1 Declaration /////////////////////////////////////////////////////////////
\r
37 ///+ 2 Initialization //////////////////////////////////////////////////////////
\r
38 //// 2 Initialization
\r
40 // Set initial values for local variables
\r
41 CANVAS_REFRESHRATE = cnvs.getRefreshRate();
\r
42 CANVAS_FRAMENUM = 0;
\r
43 DEFAULT_BG_COLOR.set(127.0/255.0,127.0/255.0,127.0/255.0,1.0); // default background color is 127/255 mid-gray
\r
46 cv::VideoCapture cap(0);
\r
47 if(!cap.isOpened()) {
\r
48 std::cout << "Camera was not found." << std::endl;
\r
51 cv::Mat camera_mat, camera_mat_prev;
\r
52 // ensure the existance of the reference reImage
\r
55 Image img1, img2, camimg;
\r
58 std::vector<cv::KeyPoint> keypoints;
\r
59 std::vector<cv::Point2f> oldpoints;
\r
60 std::vector<cv::Point2f> newpoints;
\r
61 std::vector<uchar> status;
\r
62 std::vector<float> errors;
\r
64 Psychlops::Figures::Arrow motion_vector_arrow;
\r
65 motion_vector_arrow.stroke.set(Color(1.0, 0.0, 0.0, 0.5), 1, Stroke::SOLID);
\r
66 Psychlops::Point img_datum;
\r
70 cv::FastFeatureDetector detector(10);
\r
71 //cv::GoodFeaturesToTrackDetector detector(30, 0.05, 5, 3);
\r
73 Figures::Movie movie;
\r
74 movie.source_area.set(500,500).centering();
\r
76 ///- 2 Initialization //////////////////////////////////////////////////////////
\r
78 ///+ 3 Drawing /////////////////////////////////////////////////////////////////
\r
79 while(!Input::get(Keyboard::esc)) {
\r
80 cnvs.clear(DEFAULT_BG_COLOR);
\r
82 // update camera input
\r
83 //camera_mat.copyTo(camera_mat_prev);
\r
86 // refresh data cache
\r
91 // detect good points
\r
92 detector.detect(camera_mat_prev, keypoints);
\r
93 for(std::vector<cv::KeyPoint>::iterator itk = keypoints.begin(); itk != keypoints.end(); ++itk)
\r
95 oldpoints.push_back(itk->pt);
\r
97 newpoints.reserve(oldpoints.capacity());
\r
99 // calc optical flow
\r
100 cv::calcOpticalFlowPyrLK(camera_mat_prev, camera_mat, oldpoints, newpoints, status, errors
\r
101 , cv::Size(15,15) // maximum search box
\r
103 , cvTermCriteria (CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 30, 0.01)
\r
104 , 0.5 // derivLambda
\r
111 camimg.from(camera_mat);
\r
112 camimg.centering().draw();
\r
113 //FFT2::filterImage(camimg, img1, 10, 10, 1.0);
\r
115 img_datum = camimg.getDatum();
\r
116 for(int i=0; i<oldpoints.size(); i++)
\r
118 motion_vector_arrow.set(oldpoints[i].x, oldpoints[i].y, newpoints[i].x, newpoints[i].y);
\r
119 motion_vector_arrow.shift(img_datum.x, img_datum.y);
\r
120 motion_vector_arrow.draw();
\r
123 //movie.captureOnce();
\r
127 ///- 3 Drawing /////////////////////////////////////////////////////////////////
\r
129 //movie.save("test.avi", 15);
\r
131 //cvReleaseCapture( &videoCapture );
\r