tmpOflearningOC.cpp
上传用户:wry1372
上传日期:2022-08-07
资源大小:1438k
文件大小:11k
源码类别:

图形图像处理

开发平台:

C/C++

  1. #include "cv.h"
  2. #include "highgui.h"
  3. #include "cxcore.h"
  4. #include "mmsystem.h"//导入声音头文件
  5. #pragma comment(lib,"winmm.lib")//导入声音头文件库
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <math.h>
  11. #include <float.h>
  12. #include <limits.h>
  13. #include <time.h>
  14. #include <ctype.h>
  15. int processImg(char* filename) {
  16. // Load the source image.
  17. IplImage *eye_image=NULL;
  18. IplImage *original_eye_image=NULL;
  19. IplImage *threshold_image=NULL;
  20. IplImage *ellipse_image=NULL;
  21. IplImage *scene_image=NULL;
  22.   //Make the eye image (in monochrome):
  23.   eye_image=cvCreateImageHeader(cvSize(640,480), 8, 1 );
  24.   eye_image->imageData=(char *)malloc(640*480);
  25.   //Make the eye image (in monochrome):
  26.   threshold_image = cvCloneImage(eye_image);
  27.     //Make the ellipse image (in RGB) :
  28.   ellipse_image=cvCreateImageHeader(cvSize(640,480), 8, 3 );
  29.   ellipse_image->imageData=(char *)malloc(640*480*3);
  30.      int i, j;
  31.   int *inliers_index;
  32.   CvSize ellipse_axis;
  33.   CvPoint gaze_point;
  34.   static int lost_frame_num = 0;
  35.     cvZero(ellipse_image);
  36.   //Make the ellipse image (in RGB) :
  37.   ellipse_image=cvCreateImageHeader(cvSize(640,480), 8, 3 );
  38.   ellipse_image->imageData=(char *)malloc(640*480*3);
  39. CvScalar White,Red,Green,Blue,Yellow;
  40.   //Init colors
  41.   White = CV_RGB(255,255,255);
  42.   Red = CV_RGB(255,0,0);
  43.   Green = CV_RGB(0,255,0);
  44.   Blue = CV_RGB(0,0,255);
  45.   Yellow = CV_RGB(255,255,0);
  46. original_eye_image = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
  47. eye_image = cvCloneImage(original_eye_image);
  48. cvSmooth(eye_image, eye_image, CV_GAUSSIAN, 5, 5);
  49. //Reduce_Line_Noise(eye_image);
  50. cvNamedWindow("win1", CV_WINDOW_AUTOSIZE);
  51. cvShowImage("win1", eye_image);
  52. //cvSetMouseCallback( "win1", on_mouse, 0 );
  53. //while(1){
  54. // if(makeGuess){
  55. // printf("DOn");
  56. // start_point.x=gX;
  57. // start_point.y=gY;
  58. // makeGuess=0;
  59. // break;
  60. // }
  61. // char c = cvWaitKey(33);
  62. //printf("what you press:%d",c);
  63. // if(c==27) {return 0;}
  64. //}
  65.   //corneal reflection
  66. remove_corneal_reflection(eye_image, threshold_image,
  67. (int)start_point.x, (int)start_point.y,
  68. cr_window_size,   (int)eye_image->height/10,
  69. corneal_reflection.x, corneal_reflection.y, corneal_reflection_r);
  70. printf("corneal reflection: (%d, %d)n", corneal_reflection.x, corneal_reflection.y);
  71. Draw_Cross(ellipse_image, corneal_reflection.x, corneal_reflection.y, 15, 15, Yellow);
  72. Draw_Cross(eye_image, corneal_reflection.x, corneal_reflection.y, 15, 15, Yellow);
  73.   //starburst pupil contour detection
  74.   starburst_pupil_contour_detection((UINT8*)eye_image->imageData, eye_image->width, eye_image->height,
  75.                                 edge_threshold, rays, min_feature_candidates);
  76.   inliers_num = 0;
  77.   inliers_index = pupil_fitting_inliers((UINT8*)eye_image->imageData, eye_image->width, eye_image->height, inliers_num);
  78.   ellipse_axis.width = (int)pupil_param[0];
  79.   ellipse_axis.height = (int)pupil_param[1];
  80.   pupil.x = (int)pupil_param[2];
  81.   pupil.y = (int)pupil_param[3];
  82.   Draw_Cross(ellipse_image, pupil.x, pupil.y, 15, 15, Red);
  83.   Draw_Cross(eye_image, pupil.x, pupil.y, 15, 15, Red);
  84.   //cvLine(eye_image, pupil, corneal_reflection, Red, 4, 8);
  85.   //cvLine(ellipse_image, pupil, corneal_reflection, Red, 4, 8);
  86. cvSaveImage("d:\eye.jpg", eye_image);
  87. cvSaveImage("d:\ellipse.jpg", ellipse_image);
  88. cvDestroyWindow("win1");
  89. cvNamedWindow("win2", CV_WINDOW_AUTOSIZE);
  90. cvShowImage("win2", eye_image);
  91. cvNamedWindow("ellipse_image", CV_WINDOW_AUTOSIZE);
  92. cvShowImage("ellipse_image", ellipse_image);
  93. //cvNamedWindow("win2", CV_WINDOW_AUTOSIZE);
  94. //cvShowImage("win2", original_eye_image);
  95. while(1){
  96. char c = cvWaitKey(33);
  97. //printf("what you press:%d",c);
  98. if(c==27) break;
  99. }
  100. return 0;
  101. }
  102. int example2_4(int argc, char** argv){
  103. char* imageFile = argv[1];
  104. IplImage* img = cvLoadImage(imageFile);
  105. smooth_image(img);
  106. return 0;
  107. }
  108. int loadAVIWithTrackbar( char * aviFile){
  109. cvNamedWindow("WithTrackBar", CV_WINDOW_AUTOSIZE);
  110. g_capture = cvCreateFileCapture( aviFile);
  111. int frames = (int) cvGetCaptureProperty(
  112. g_capture,
  113. CV_CAP_PROP_FRAME_COUNT
  114. );
  115. if( frames!=0){
  116.   cvCreateTrackbar(
  117. "Position",
  118. "WithTrackBar",
  119. &g_slider_position,
  120. frames,
  121. onTrackbarSlide
  122.   );
  123. }
  124. IplImage * frame;
  125. while(1){
  126. frame = cvQueryFrame(g_capture);
  127. if(frame==NULL) printf("frame is nulln");
  128. if(!frame)break;
  129. cvShowImage("WithTrackBar",frame);
  130. g_slider_position++;
  131. cvSetTrackbarPos("Position", "WithTrackBar", g_slider_position);
  132. //printf("press ESC to escapen");
  133. char c = cvWaitKey(33);
  134. //printf("what you press:%d",c);
  135. if(c==27) break;
  136. }
  137. cvReleaseCapture(&g_capture);
  138. cvDestroyWindow("WithTrackBar");
  139. return 0;
  140. }
  141. //example 2_4
  142. void smooth_image(IplImage* image){
  143. cvNamedWindow( "Smooth-in");
  144. cvNamedWindow( "Smooth-out");
  145. cvShowImage("Smooth-in", image);
  146. IplImage* out = cvCreateImage(
  147. cvGetSize(image),
  148. IPL_DEPTH_8U,
  149. 3
  150. );
  151. cvSmooth(image, out, CV_GAUSSIAN, 3,3);
  152. cvShowImage("Smooth-out", out);
  153. cvReleaseImage(&out);
  154. cvWaitKey(0);
  155. cvDestroyWindow("Smooth-in");
  156. cvDestroyWindow("Smooth-out");
  157. }
  158. int loadAVI(char * aviFile){
  159. cvNamedWindow("LoadAVI", CV_WINDOW_AUTOSIZE);
  160. CvCapture * capture = cvCreateFileCapture(aviFile);
  161. if(capture==NULL) printf("capture is nulln");
  162. IplImage * frame;
  163. while(1){
  164. frame = cvQueryFrame(capture);
  165. if(frame==NULL) printf("frame is nulln");
  166. if(!frame)break;
  167. cvShowImage("LoadAVI",frame);
  168. //printf("press ESC to escapen");
  169. char c = cvWaitKey(33);
  170. //printf("what you press:%d",c);
  171. if(c==27) break;
  172. }
  173. cvReleaseCapture(&capture);
  174. cvDestroyWindow("LoadAVI");
  175. return 0;
  176. }
  177. int loadCamera(int index){
  178. cvNamedWindow("LoadCamera", CV_WINDOW_AUTOSIZE);
  179. //CvCapture * capture = cvCreateCameraCapture(0);
  180. CvCapture * capture = cvCaptureFromCAM(index);
  181. double fps = cvGetCaptureProperty( capture, CV_CAP_PROP_FPS);
  182. //why this was 0.00
  183. printf("fps: %lfn", fps);
  184. if(capture==NULL) printf("capture is nulln");
  185. IplImage * frame;
  186. while(1){
  187. frame = cvQueryFrame(capture);
  188. if(frame==NULL) printf("frame is nulln");
  189. if(!frame)break;
  190. cvShowImage("LoadCamera",frame);
  191. //printf("press ESC to escapen");
  192. char c = cvWaitKey(33);
  193. //printf("what you press:%d",c);
  194. if(c==27) break;
  195. }
  196. cvReleaseCapture(&capture);
  197. cvDestroyWindow("LoadCamera");
  198. return 0;
  199. }
  200. //save the vedio from camera
  201. //unable to write in file
  202. int saveCameraFile(char * savedFile){
  203. int to_save = 0;
  204. int to_end = 0;
  205. cvNamedWindow("LoadCamera", CV_WINDOW_AUTOSIZE);
  206. //CvCapture * capture = cvCreateCameraCapture(0);
  207. CvCapture * capture = cvCaptureFromCAM(-1);
  208. if(capture==NULL) {
  209. printf("capture is nulln");
  210. return -1;
  211. }
  212. double fps = cvGetCaptureProperty( capture, CV_CAP_PROP_FPS);
  213. CvSize size = cvSize(
  214. (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),
  215. (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT)
  216. );
  217. CvVideoWriter* writer = cvCreateVideoWriter(
  218. "e:\t.avi",
  219. CV_FOURCC('D', 'I', 'V', 'X'),
  220. fps,
  221. size
  222. );
  223. IplImage * frame;
  224. int count = 0;
  225. char filePath[30] = {0};
  226. printf("Hot key:n"
  227. "tEse - quite the programn"
  228. "ts - to save the videon"
  229. "te - to end the videon"
  230. );
  231. while(1){
  232. frame = cvQueryFrame(capture);
  233. if(frame==NULL) printf("frame is nulln");
  234. if(!frame)break;
  235. cvShowImage("LoadCamera",frame);
  236. cvWriteFrame(writer, frame);
  237. //printf("press ESC to escapen");
  238. count++;
  239. sprintf(filePath, "%s\%06d.jpg", savedFile, count);
  240. //printf("file name: %sn", filePath);
  241. if(to_save){
  242. cvSaveImage(filePath, frame);
  243. }
  244. char c = cvWaitKey(33);
  245. if(c==27) break;
  246. switch( c){
  247. case 's': to_save=1;break;
  248. case 'e': to_end=1;to_save=0;break;
  249. }
  250. }
  251. cvReleaseCapture(&capture);
  252. cvDestroyWindow("LoadCamera");
  253. cvReleaseVideoWriter(&writer);
  254. return 0;
  255. }
  256. void stereocallback(IplImage* image1,IplImage* image2);
  257. void onMouse(int Event,int x,int y,int flags,void *param);
  258. int g_count = 0;
  259. char g_filePath[30] = {0};
  260. void syncTwoCamera(){
  261. int ncams = cvcamGetCamerasCount();
  262. printf("Cam number: %dn", ncams);
  263. if(ncams<2) return;
  264. int * cameraIndexes;
  265. int count = cvcamSelectCamera(&cameraIndexes);
  266. if(count <2) return;
  267. //printf("0: %dn", cameraIndexes[0]);
  268. //printf("1: %dn", cameraIndexes[1]);
  269. //for the first camera
  270. cvNamedWindow("Camera 0", CV_WINDOW_AUTOSIZE);
  271. HWND captureWindow0 = (HWND)cvGetWindowHandle("Camera 0");
  272.     cvcamSetProperty(cameraIndexes[0], CVCAM_PROP_ENABLE, CVCAMTRUE);
  273.     cvcamSetProperty(cameraIndexes[0], CVCAM_PROP_RENDER, CVCAMTRUE);
  274.     cvcamSetProperty(cameraIndexes[0], CVCAM_PROP_WINDOW, &captureWindow0);
  275. cvSetMouseCallback("Camera 0",onMouse,0);
  276. cvcamSetProperty(cameraIndexes[0], CVCAM_STEREO_CALLBACK,stereocallback);
  277. //CvCapture * capture_0 = cvCaptureFromCAM(cameraIndexes[0]);
  278. /*
  279. if(capture_0){
  280. double fps_0 = cvGetCaptureProperty( capture_0, CV_CAP_PROP_FPS);
  281. printf("fps of capture_0: %lfn",fps_0);
  282. }else printf("caputure_0 is nulln");
  283. */
  284. //for the second camera
  285. cvNamedWindow("Camera 1", CV_WINDOW_AUTOSIZE);
  286. HWND captureWindow1 = (HWND)cvGetWindowHandle("Camera 1");
  287.     cvcamSetProperty(cameraIndexes[1], CVCAM_PROP_ENABLE, CVCAMTRUE);
  288.     cvcamSetProperty(cameraIndexes[1], CVCAM_PROP_RENDER, CVCAMTRUE);
  289.     cvcamSetProperty(cameraIndexes[1], CVCAM_PROP_WINDOW, &captureWindow1);
  290. cvSetMouseCallback("Camera 1",onMouse,0);
  291. cvcamSetProperty(cameraIndexes[1], CVCAM_STEREO_CALLBACK,stereocallback);
  292. //CvCapture * capture_1 = cvCaptureFromCAM(cameraIndexes[1]);
  293. //让两个摄像头同步
  294.     cvcamInit();
  295.     cvcamStart();
  296.     cvWaitKey(0);
  297.     cvcamStop();
  298.     cvcamExit();
  299. //cvReleaseCapture(&capture_0);
  300. //cvReleaseCapture(&capture_1);
  301. cvDestroyWindow("Camera 0");
  302. cvDestroyWindow("Camera 1");
  303. }
  304. char * savedFile1 = "e:\savedImage\eye";
  305. char * savedFile2 = "e:\savedImage\scene";
  306. void stereocallback(IplImage* image1,IplImage* image2)//IplImage* image2
  307. {
  308. //创建窗口
  309. //cvNamedWindow("leftVideo", 1);
  310. //cvNamedWindow("rightVideo",1);
  311. //使窗口有序排列
  312. cvMoveWindow("Camera 0", 30, 0);
  313. cvMoveWindow("Camera 1", 360, 0);
  314. //
  315. //cvShowImage("Camera 0", image1);//cvShowImage("Camera 1", image2);
  316. if(g_toSave){
  317. g_count++;
  318. sprintf(g_filePath, "%s\%06d.jpg", savedFile1, g_count);
  319. cvSaveImage(g_filePath, image1);
  320. sprintf(g_filePath, "%s\%06d.jpg", savedFile2, g_count);
  321. cvSaveImage(g_filePath, image2);
  322. }
  323. cvWaitKey(60);
  324. }
  325. void onMouse(int Event,int x,int y,int flags,void *param)
  326. {
  327. if(Event==CV_EVENT_RBUTTONDOWN)
  328. {
  329. g_toSave = abs(g_toSave-1);
  330. }
  331. }