tmpOflearningOC.cpp
资源名称:EyeDetect.zip [点击查看]
上传用户:wry1372
上传日期:2022-08-07
资源大小:1438k
文件大小:11k
源码类别:
图形图像处理
开发平台:
C/C++
- #include "cv.h"
- #include "highgui.h"
- #include "cxcore.h"
- #include "mmsystem.h"//导入声音头文件
- #pragma comment(lib,"winmm.lib")//导入声音头文件库
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <assert.h>
- #include <math.h>
- #include <float.h>
- #include <limits.h>
- #include <time.h>
- #include <ctype.h>
- int processImg(char* filename) {
- // Load the source image.
- IplImage *eye_image=NULL;
- IplImage *original_eye_image=NULL;
- IplImage *threshold_image=NULL;
- IplImage *ellipse_image=NULL;
- IplImage *scene_image=NULL;
- //Make the eye image (in monochrome):
- eye_image=cvCreateImageHeader(cvSize(640,480), 8, 1 );
- eye_image->imageData=(char *)malloc(640*480);
- //Make the eye image (in monochrome):
- threshold_image = cvCloneImage(eye_image);
- //Make the ellipse image (in RGB) :
- ellipse_image=cvCreateImageHeader(cvSize(640,480), 8, 3 );
- ellipse_image->imageData=(char *)malloc(640*480*3);
- int i, j;
- int *inliers_index;
- CvSize ellipse_axis;
- CvPoint gaze_point;
- static int lost_frame_num = 0;
- cvZero(ellipse_image);
- //Make the ellipse image (in RGB) :
- ellipse_image=cvCreateImageHeader(cvSize(640,480), 8, 3 );
- ellipse_image->imageData=(char *)malloc(640*480*3);
- CvScalar White,Red,Green,Blue,Yellow;
- //Init colors
- White = CV_RGB(255,255,255);
- Red = CV_RGB(255,0,0);
- Green = CV_RGB(0,255,0);
- Blue = CV_RGB(0,0,255);
- Yellow = CV_RGB(255,255,0);
- original_eye_image = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
- eye_image = cvCloneImage(original_eye_image);
- cvSmooth(eye_image, eye_image, CV_GAUSSIAN, 5, 5);
- //Reduce_Line_Noise(eye_image);
- cvNamedWindow("win1", CV_WINDOW_AUTOSIZE);
- cvShowImage("win1", eye_image);
- //cvSetMouseCallback( "win1", on_mouse, 0 );
- //while(1){
- // if(makeGuess){
- // printf("DOn");
- // start_point.x=gX;
- // start_point.y=gY;
- // makeGuess=0;
- // break;
- // }
- // char c = cvWaitKey(33);
- //printf("what you press:%d",c);
- // if(c==27) {return 0;}
- //}
- //corneal reflection
- remove_corneal_reflection(eye_image, threshold_image,
- (int)start_point.x, (int)start_point.y,
- cr_window_size, (int)eye_image->height/10,
- corneal_reflection.x, corneal_reflection.y, corneal_reflection_r);
- printf("corneal reflection: (%d, %d)n", corneal_reflection.x, corneal_reflection.y);
- Draw_Cross(ellipse_image, corneal_reflection.x, corneal_reflection.y, 15, 15, Yellow);
- Draw_Cross(eye_image, corneal_reflection.x, corneal_reflection.y, 15, 15, Yellow);
- //starburst pupil contour detection
- starburst_pupil_contour_detection((UINT8*)eye_image->imageData, eye_image->width, eye_image->height,
- edge_threshold, rays, min_feature_candidates);
- inliers_num = 0;
- inliers_index = pupil_fitting_inliers((UINT8*)eye_image->imageData, eye_image->width, eye_image->height, inliers_num);
- ellipse_axis.width = (int)pupil_param[0];
- ellipse_axis.height = (int)pupil_param[1];
- pupil.x = (int)pupil_param[2];
- pupil.y = (int)pupil_param[3];
- Draw_Cross(ellipse_image, pupil.x, pupil.y, 15, 15, Red);
- Draw_Cross(eye_image, pupil.x, pupil.y, 15, 15, Red);
- //cvLine(eye_image, pupil, corneal_reflection, Red, 4, 8);
- //cvLine(ellipse_image, pupil, corneal_reflection, Red, 4, 8);
- cvSaveImage("d:\eye.jpg", eye_image);
- cvSaveImage("d:\ellipse.jpg", ellipse_image);
- cvDestroyWindow("win1");
- cvNamedWindow("win2", CV_WINDOW_AUTOSIZE);
- cvShowImage("win2", eye_image);
- cvNamedWindow("ellipse_image", CV_WINDOW_AUTOSIZE);
- cvShowImage("ellipse_image", ellipse_image);
- //cvNamedWindow("win2", CV_WINDOW_AUTOSIZE);
- //cvShowImage("win2", original_eye_image);
- while(1){
- char c = cvWaitKey(33);
- //printf("what you press:%d",c);
- if(c==27) break;
- }
- return 0;
- }
- int example2_4(int argc, char** argv){
- char* imageFile = argv[1];
- IplImage* img = cvLoadImage(imageFile);
- smooth_image(img);
- return 0;
- }
- int loadAVIWithTrackbar( char * aviFile){
- cvNamedWindow("WithTrackBar", CV_WINDOW_AUTOSIZE);
- g_capture = cvCreateFileCapture( aviFile);
- int frames = (int) cvGetCaptureProperty(
- g_capture,
- CV_CAP_PROP_FRAME_COUNT
- );
- if( frames!=0){
- cvCreateTrackbar(
- "Position",
- "WithTrackBar",
- &g_slider_position,
- frames,
- onTrackbarSlide
- );
- }
- IplImage * frame;
- while(1){
- frame = cvQueryFrame(g_capture);
- if(frame==NULL) printf("frame is nulln");
- if(!frame)break;
- cvShowImage("WithTrackBar",frame);
- g_slider_position++;
- cvSetTrackbarPos("Position", "WithTrackBar", g_slider_position);
- //printf("press ESC to escapen");
- char c = cvWaitKey(33);
- //printf("what you press:%d",c);
- if(c==27) break;
- }
- cvReleaseCapture(&g_capture);
- cvDestroyWindow("WithTrackBar");
- return 0;
- }
- //example 2_4
- void smooth_image(IplImage* image){
- cvNamedWindow( "Smooth-in");
- cvNamedWindow( "Smooth-out");
- cvShowImage("Smooth-in", image);
- IplImage* out = cvCreateImage(
- cvGetSize(image),
- IPL_DEPTH_8U,
- 3
- );
- cvSmooth(image, out, CV_GAUSSIAN, 3,3);
- cvShowImage("Smooth-out", out);
- cvReleaseImage(&out);
- cvWaitKey(0);
- cvDestroyWindow("Smooth-in");
- cvDestroyWindow("Smooth-out");
- }
- int loadAVI(char * aviFile){
- cvNamedWindow("LoadAVI", CV_WINDOW_AUTOSIZE);
- CvCapture * capture = cvCreateFileCapture(aviFile);
- if(capture==NULL) printf("capture is nulln");
- IplImage * frame;
- while(1){
- frame = cvQueryFrame(capture);
- if(frame==NULL) printf("frame is nulln");
- if(!frame)break;
- cvShowImage("LoadAVI",frame);
- //printf("press ESC to escapen");
- char c = cvWaitKey(33);
- //printf("what you press:%d",c);
- if(c==27) break;
- }
- cvReleaseCapture(&capture);
- cvDestroyWindow("LoadAVI");
- return 0;
- }
- int loadCamera(int index){
- cvNamedWindow("LoadCamera", CV_WINDOW_AUTOSIZE);
- //CvCapture * capture = cvCreateCameraCapture(0);
- CvCapture * capture = cvCaptureFromCAM(index);
- double fps = cvGetCaptureProperty( capture, CV_CAP_PROP_FPS);
- //why this was 0.00
- printf("fps: %lfn", fps);
- if(capture==NULL) printf("capture is nulln");
- IplImage * frame;
- while(1){
- frame = cvQueryFrame(capture);
- if(frame==NULL) printf("frame is nulln");
- if(!frame)break;
- cvShowImage("LoadCamera",frame);
- //printf("press ESC to escapen");
- char c = cvWaitKey(33);
- //printf("what you press:%d",c);
- if(c==27) break;
- }
- cvReleaseCapture(&capture);
- cvDestroyWindow("LoadCamera");
- return 0;
- }
- //save the vedio from camera
- //unable to write in file
- int saveCameraFile(char * savedFile){
- int to_save = 0;
- int to_end = 0;
- cvNamedWindow("LoadCamera", CV_WINDOW_AUTOSIZE);
- //CvCapture * capture = cvCreateCameraCapture(0);
- CvCapture * capture = cvCaptureFromCAM(-1);
- if(capture==NULL) {
- printf("capture is nulln");
- return -1;
- }
- double fps = cvGetCaptureProperty( capture, CV_CAP_PROP_FPS);
- CvSize size = cvSize(
- (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),
- (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT)
- );
- CvVideoWriter* writer = cvCreateVideoWriter(
- "e:\t.avi",
- CV_FOURCC('D', 'I', 'V', 'X'),
- fps,
- size
- );
- IplImage * frame;
- int count = 0;
- char filePath[30] = {0};
- printf("Hot key:n"
- "tEse - quite the programn"
- "ts - to save the videon"
- "te - to end the videon"
- );
- while(1){
- frame = cvQueryFrame(capture);
- if(frame==NULL) printf("frame is nulln");
- if(!frame)break;
- cvShowImage("LoadCamera",frame);
- cvWriteFrame(writer, frame);
- //printf("press ESC to escapen");
- count++;
- sprintf(filePath, "%s\%06d.jpg", savedFile, count);
- //printf("file name: %sn", filePath);
- if(to_save){
- cvSaveImage(filePath, frame);
- }
- char c = cvWaitKey(33);
- if(c==27) break;
- switch( c){
- case 's': to_save=1;break;
- case 'e': to_end=1;to_save=0;break;
- }
- }
- cvReleaseCapture(&capture);
- cvDestroyWindow("LoadCamera");
- cvReleaseVideoWriter(&writer);
- return 0;
- }
- void stereocallback(IplImage* image1,IplImage* image2);
- void onMouse(int Event,int x,int y,int flags,void *param);
- int g_count = 0;
- char g_filePath[30] = {0};
- void syncTwoCamera(){
- int ncams = cvcamGetCamerasCount();
- printf("Cam number: %dn", ncams);
- if(ncams<2) return;
- int * cameraIndexes;
- int count = cvcamSelectCamera(&cameraIndexes);
- if(count <2) return;
- //printf("0: %dn", cameraIndexes[0]);
- //printf("1: %dn", cameraIndexes[1]);
- //for the first camera
- cvNamedWindow("Camera 0", CV_WINDOW_AUTOSIZE);
- HWND captureWindow0 = (HWND)cvGetWindowHandle("Camera 0");
- cvcamSetProperty(cameraIndexes[0], CVCAM_PROP_ENABLE, CVCAMTRUE);
- cvcamSetProperty(cameraIndexes[0], CVCAM_PROP_RENDER, CVCAMTRUE);
- cvcamSetProperty(cameraIndexes[0], CVCAM_PROP_WINDOW, &captureWindow0);
- cvSetMouseCallback("Camera 0",onMouse,0);
- cvcamSetProperty(cameraIndexes[0], CVCAM_STEREO_CALLBACK,stereocallback);
- //CvCapture * capture_0 = cvCaptureFromCAM(cameraIndexes[0]);
- /*
- if(capture_0){
- double fps_0 = cvGetCaptureProperty( capture_0, CV_CAP_PROP_FPS);
- printf("fps of capture_0: %lfn",fps_0);
- }else printf("caputure_0 is nulln");
- */
- //for the second camera
- cvNamedWindow("Camera 1", CV_WINDOW_AUTOSIZE);
- HWND captureWindow1 = (HWND)cvGetWindowHandle("Camera 1");
- cvcamSetProperty(cameraIndexes[1], CVCAM_PROP_ENABLE, CVCAMTRUE);
- cvcamSetProperty(cameraIndexes[1], CVCAM_PROP_RENDER, CVCAMTRUE);
- cvcamSetProperty(cameraIndexes[1], CVCAM_PROP_WINDOW, &captureWindow1);
- cvSetMouseCallback("Camera 1",onMouse,0);
- cvcamSetProperty(cameraIndexes[1], CVCAM_STEREO_CALLBACK,stereocallback);
- //CvCapture * capture_1 = cvCaptureFromCAM(cameraIndexes[1]);
- //让两个摄像头同步
- cvcamInit();
- cvcamStart();
- cvWaitKey(0);
- cvcamStop();
- cvcamExit();
- //cvReleaseCapture(&capture_0);
- //cvReleaseCapture(&capture_1);
- cvDestroyWindow("Camera 0");
- cvDestroyWindow("Camera 1");
- }
- char * savedFile1 = "e:\savedImage\eye";
- char * savedFile2 = "e:\savedImage\scene";
- void stereocallback(IplImage* image1,IplImage* image2)//IplImage* image2
- {
- //创建窗口
- //cvNamedWindow("leftVideo", 1);
- //cvNamedWindow("rightVideo",1);
- //使窗口有序排列
- cvMoveWindow("Camera 0", 30, 0);
- cvMoveWindow("Camera 1", 360, 0);
- //
- //cvShowImage("Camera 0", image1);//cvShowImage("Camera 1", image2);
- if(g_toSave){
- g_count++;
- sprintf(g_filePath, "%s\%06d.jpg", savedFile1, g_count);
- cvSaveImage(g_filePath, image1);
- sprintf(g_filePath, "%s\%06d.jpg", savedFile2, g_count);
- cvSaveImage(g_filePath, image2);
- }
- cvWaitKey(60);
- }
- void onMouse(int Event,int x,int y,int flags,void *param)
- {
- if(Event==CV_EVENT_RBUTTONDOWN)
- {
- g_toSave = abs(g_toSave-1);
- }
- }