OSDN Git Service

first
[psychlops/cpp.git] / win32gl / test / opencvtest.cpp
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
6 \r
7 //hoge\r
8 \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
20 \r
21 \r
22 void psychlops_main() {\r
23 \r
24 \r
25         ///+ 1 Declaration /////////////////////////////////////////////////////////////\r
26         //// 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
32 \r
33                 //declare local variables around here\r
34 \r
35         ///- 1 Declaration /////////////////////////////////////////////////////////////\r
36 \r
37         ///+ 2 Initialization //////////////////////////////////////////////////////////\r
38         //// 2 Initialization\r
39 \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
44 \r
45                 //cv2\r
46                 cv::VideoCapture cap(0);\r
47                 if(!cap.isOpened()) {\r
48                         std::cout << "Camera was not found." << std::endl;\r
49                         exit(20);\r
50                 }\r
51                 cv::Mat camera_mat, camera_mat_prev;\r
52                 // ensure the existance of the reference reImage\r
53         cap >> camera_mat;\r
54 \r
55                 Image img1, img2, camimg;\r
56 \r
57 \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
63 \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
67 \r
68 \r
69 \r
70                 cv::FastFeatureDetector detector(10);\r
71                 //cv::GoodFeaturesToTrackDetector detector(30, 0.05, 5, 3);\r
72 \r
73                 Figures::Movie movie;\r
74                 movie.source_area.set(500,500).centering();\r
75 \r
76         ///- 2 Initialization //////////////////////////////////////////////////////////\r
77 \r
78         ///+ 3 Drawing /////////////////////////////////////////////////////////////////\r
79         while(!Input::get(Keyboard::esc)) {\r
80                 cnvs.clear(DEFAULT_BG_COLOR);\r
81 \r
82                 // update camera input\r
83                 //camera_mat.copyTo(camera_mat_prev);\r
84         cap >> camera_mat;\r
85 /*\r
86                 // refresh data cache\r
87                 keypoints.clear();\r
88                 oldpoints.clear();\r
89                 newpoints.clear();\r
90 \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
94                 {\r
95                         oldpoints.push_back(itk->pt);\r
96                 }\r
97                 newpoints.reserve(oldpoints.capacity());\r
98 \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
102                                                                 , 3 // Max level\r
103                                                                 , cvTermCriteria (CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 30, 0.01)\r
104                                                                 , 0.5 // derivLambda\r
105                                                                 );\r
106 \r
107 \r
108 \r
109                 // draw\r
110 */\r
111         camimg.from(camera_mat);\r
112                 camimg.centering().draw();\r
113                 //FFT2::filterImage(camimg, img1, 10, 10, 1.0);\r
114 /*\r
115                 img_datum = camimg.getDatum();\r
116                 for(int i=0; i<oldpoints.size(); i++)\r
117                 {\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
121                 }\r
122 */\r
123 \r
124                 movie.captureOnce();\r
125                 cnvs.flip();\r
126                 CANVAS_FRAMENUM++;\r
127         }\r
128         ///- 3 Drawing /////////////////////////////////////////////////////////////////\r
129 \r
130 \r
131         movie.save("test.avi", 15);\r
132         //cv1\r
133     //cvReleaseCapture( &videoCapture );\r
134 }\r