int main() { init_window("Blurcode", 320, 80); CvCapture *capture = init_camera(); IplImage *input = query_frame(capture); AppData data; CvSize size = cvSize(input->width / 2, input->height / 2); create_images(&data, size, 3); CvSize debug_size = cvSize(COLUMNS * size.width, ROWS * size.height); data.debug = cvCreateImage(debug_size, IPL_DEPTH_8U, 3); printf("camera=%dx%d zoomed=%dx%d debug=%dx%d\n", input->width, input->height, size.width, size.height, data.debug->width, data.debug->height); int delay = 100; while ((cvWaitKey(delay) & 255) != 27) { delay = 10; input = query_frame(capture); cvCvtColor(input, input, CV_BGR2RGB); // Horizontal flip (mirror display) cvFlip(input, input, 1); cvResize(input, data.rgb, CV_INTER_AREA); cvSplit(data.rgb, data.red, NULL, NULL, NULL); cvSmooth(data.red, data.red, CV_GAUSSIAN, 3, 3, 0, 0); decode(&data, ANGLE_OF_VIEW, "input trace", NULL, NULL); cvResetImageROI(data.debug); cvCvtColor(data.debug, data.debug, CV_RGB2BGR); cvShowImage("Blurcode", data.debug); } cvReleaseCapture(&capture); cvReleaseImage(&data.debug); release_images(&data); cvDestroyWindow("Blurcode"); return 0; }
int main(int argc, char **argv) { // Check parameters if (argc < 2) { fprintf(stderr, "%s: %s\n", APP_NAME, "No video name given"); fprintf(stderr, "Usage: %s <video file name> [output file name]\n", APP_NAME); exit(EXIT_FAILURE); } char *output_file_name; if (argc == 3) { output_file_name = argv[2]; } else { output_file_name = OUTPUT_FILE_NAME; } // Load video char *file_name = argv[1]; CvCapture *video = cvCaptureFromFile(file_name); if (!video) { exit(EXIT_FAILURE); } // Extract video parameters CvSize video_frame_size; video_frame_size.width = cvGetCaptureProperty(video, CV_CAP_PROP_FRAME_WIDTH); video_frame_size.height = cvGetCaptureProperty(video, CV_CAP_PROP_FRAME_HEIGHT); double video_fps = cvGetCaptureProperty(video, CV_CAP_PROP_FPS); long video_frame_count = cvGetCaptureProperty(video, CV_CAP_PROP_FRAME_COUNT); // Initialize video writer CvVideoWriter *video_writer = cvCreateVideoWriter(output_file_name, FOURCC, video_fps, video_frame_size, true); // Initialize variables for optical flow calculation IplImage *current_frame = cvCreateImage(video_frame_size, IPL_DEPTH_8U, 3); IplImage *eigen_image = cvCreateImage(video_frame_size, IPL_DEPTH_32F, 1); IplImage *temp_image = cvCreateImage(video_frame_size, IPL_DEPTH_32F, 1); int corner_count = MAX_CORNERS; CvPoint2D32f corners[2][MAX_CORNERS]; char features_found[MAX_CORNERS]; float feature_errors[MAX_CORNERS]; IplImage *frame_buffer[2]; IplImage *pyramid_images[2]; CvSize pyramid_size = cvSize(video_frame_size.width + 8, video_frame_size.height / 3); int i; for (i = 0; i < 2; i++) { frame_buffer[i] = cvCreateImage(video_frame_size, IPL_DEPTH_8U, 1); pyramid_images[i] = cvCreateImage(pyramid_size, IPL_DEPTH_32F, 1); } // Process video while (query_frame(video, frame_buffer, current_frame)) { // Corner finding with Shi and Thomasi cvGoodFeaturesToTrack( frame_buffer[0], eigen_image, temp_image, corners[0], &corner_count, 0.01, 5.0, 0, 3, 0, 0.4); cvFindCornerSubPix( frame_buffer[0], corners[0], corner_count, cvSize(WINDOW_SIZE, WINDOW_SIZE), cvSize(-1, -1), cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.3)); // Pyramid Lucas-Kanade cvCalcOpticalFlowPyrLK( frame_buffer[0], frame_buffer[1], pyramid_images[0], pyramid_images[1], corners[0], corners[1], corner_count, cvSize(WINDOW_SIZE, WINDOW_SIZE), 5, features_found, feature_errors, cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.3), 0); // Draw optical flow vectors int i; double l_max, l; for (i = 0; i < corner_count; i++) { if (features_found[i] == 0 || feature_errors[i] > 550) { continue; } l = sqrt(corners[1][i].x*corners[1][i].x+corners[1][i].y*corners[1][i].y); if(l>l_max) l_max = l; } for (i = 0; i < corner_count; i++) { if (features_found[i] == 0 || feature_errors[i] > 550) { continue; } double spinSize = 5.0 * l/l_max; CvPoint points[2]; points[0] = cvPoint(cvRound(corners[0][i].x), cvRound(corners[0][i].y)); points[1] = cvPoint(cvRound(corners[1][i].x), cvRound(corners[1][i].y)); cvLine(current_frame, points[0], points[1], CV_RGB(0, 255, 0), 1, 8, 0); double angle; angle = atan2( (double) points[0].y - points[1].y, (double) points[0].x - points[1].x ); points[0].x = (int) (points[1].x + spinSize * cos(angle + 3.1416 / 4)); points[0].y = (int) (points[1].y + spinSize * sin(angle + 3.1416 / 4)); cvLine(current_frame, points[0], points[1], CV_RGB(0, 255, 0), 1, 8, 0); points[0].x = (int) (points[1].x + spinSize * cos(angle - 3.1416 / 4)); points[0].y = (int) (points[1].y + spinSize * sin(angle - 3.1416 / 4)); cvLine( current_frame, points[0], points[1], CV_RGB(0, 255, 0), 1, 8, 0); } cvWriteFrame(video_writer, current_frame); } // Clean up cvReleaseImage(¤t_frame); cvReleaseImage(&eigen_image); cvReleaseImage(&temp_image); for (i = 0; i < 2; i++) { cvReleaseImage(&frame_buffer[0]); cvReleaseImage(&pyramid_images[0]); } cvReleaseCapture(&video); cvReleaseVideoWriter(&video_writer); return 0; }