Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 3 | PointedEar | 1 | /*------------------------------------------------------------------------------------------*\ |
| 2 | This file contains material supporting chapter 10 of the cookbook: |
||
| 3 | Computer Vision Programming using the OpenCV Library. |
||
| 4 | by Robert Laganiere, Packt Publishing, 2011. |
||
| 5 | |||
| 6 | This program is free software; permission is hereby granted to use, copy, modify, |
||
| 7 | and distribute this source code, or portions thereof, for any purpose, without fee, |
||
| 8 | subject to the restriction that the copyright notice may not be removed |
||
| 9 | or altered from any source or altered source distribution. |
||
| 10 | The software is released on an as-is basis and without any warranties of any kind. |
||
| 11 | In particular, the software is not guaranteed to be fault-tolerant or free from failure. |
||
| 12 | The author disclaims all warranties with regard to this software, any use, |
||
| 13 | and any consequent failure, is purely the responsibility of the user. |
||
| 14 | |||
| 15 | Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name |
||
| 16 | \*------------------------------------------------------------------------------------------*/ |
||
| 17 | |||
| 18 | #include <iostream> |
||
| 19 | #include <opencv2/core/core.hpp> |
||
| 20 | #include <opencv2/imgproc/imgproc.hpp> |
||
| 21 | #include <opencv2/highgui/highgui.hpp> |
||
| 22 | |||
| 23 | #include "videoprocessor.h" |
||
| 24 | |||
| 25 | void draw(cv::Mat& img, cv::Mat& out) { |
||
| 26 | |||
| 27 | img.copyTo(out); |
||
| 28 | cv::circle(out, cv::Point(100,100),5,cv::Scalar(255,0,0),2); |
||
| 29 | } |
||
| 30 | |||
| 31 | void canny(cv::Mat& img, cv::Mat& out) { |
||
| 32 | |||
| 33 | // Convert to gray |
||
| 34 | cv::cvtColor(img,out,CV_BGR2GRAY); |
||
| 35 | // Compute Canny edges |
||
| 36 | cv::Canny(out,out,100,200); |
||
| 37 | // Invert the image |
||
| 38 | cv::threshold(out,out,128,255,cv::THRESH_BINARY_INV); |
||
| 39 | } |
||
| 40 | |||
| 41 | int main() |
||
| 42 | { |
||
| 43 | // Open the video file |
||
| 44 | cv::VideoCapture capture("../bike.avi"); |
||
| 45 | // check if video successfully opened |
||
| 46 | if (!capture.isOpened()) |
||
| 47 | return 1; |
||
| 48 | |||
| 49 | // Get the frame rate |
||
| 50 | double rate= capture.get(CV_CAP_PROP_FPS); |
||
| 51 | |||
| 52 | bool stop(false); |
||
| 53 | cv::Mat frame; // current video frame |
||
| 54 | cv::namedWindow("Extracted Frame"); |
||
| 55 | |||
| 56 | // Delay between each frame |
||
| 57 | // corresponds to video frame rate |
||
| 58 | int delay= 1000/rate; |
||
| 59 | |||
| 60 | // for all frames in video |
||
| 61 | while (!stop) { |
||
| 62 | |||
| 63 | // read next frame if any |
||
| 64 | if (!capture.read(frame)) |
||
| 65 | break; |
||
| 66 | |||
| 67 | cv::imshow("Extracted Frame",frame); |
||
| 68 | |||
| 69 | // introduce a delay |
||
| 70 | // or press key to stop |
||
| 71 | if (cv::waitKey(delay)>=0) |
||
| 72 | stop= true; |
||
| 73 | } |
||
| 74 | |||
| 75 | // Close the video file |
||
| 76 | capture.release(); |
||
| 77 | |||
| 78 | cv::waitKey(); |
||
| 79 | |||
| 80 | // Now using the VideoProcessor class |
||
| 81 | |||
| 82 | // Create instance |
||
| 83 | VideoProcessor processor; |
||
| 84 | // Open video file |
||
| 85 | processor.setInput("../bike.avi"); |
||
| 86 | // Declare a window to display the video |
||
| 87 | processor.displayInput("Input Video"); |
||
| 88 | processor.displayOutput("Output Video"); |
||
| 89 | // Play the video at the original frame rate |
||
| 90 | processor.setDelay(1000./processor.getFrameRate()); |
||
| 91 | // Set the frame processor callback function |
||
| 92 | processor.setFrameProcessor(canny); |
||
| 93 | // Start the process |
||
| 94 | processor.run(); |
||
| 95 | cv::waitKey(); |
||
| 96 | |||
| 97 | // Second test |
||
| 98 | // Create instance |
||
| 99 | // VideoProcessor processor; |
||
| 100 | // Open video file |
||
| 101 | processor.setInput("../bike.avi"); |
||
| 102 | |||
| 103 | // Get basic info about video file |
||
| 104 | cv::Size size= processor.getFrameSize(); |
||
| 105 | std::cout << size.width << " " << size.height << std::endl; |
||
| 106 | std::cout << processor.getFrameRate() << std::endl; |
||
| 107 | std::cout << processor.getTotalFrameCount() << std::endl; |
||
| 108 | std::cout << processor.getFrameNumber() << std::endl; |
||
| 109 | std::cout << processor.getPositionMS() << std::endl; |
||
| 110 | |||
| 111 | // No processing |
||
| 112 | processor.dontCallProcess(); |
||
| 113 | // Output filename |
||
| 114 | // processor.setOutput("../output/bikeOut",".jpg"); |
||
| 115 | char codec[4]; |
||
| 116 | processor.setOutput("../output/bike.avi",processor.getCodec(codec),processor.getFrameRate()); |
||
| 117 | std::cout << "Codec: " << codec[0] << codec[1] << codec[2] << codec[3] << std::endl; |
||
| 118 | |||
| 119 | // Position the stream at frame 300 |
||
| 120 | // processor.setFrameNumber(300); |
||
| 121 | // processor.stopAtFrameNo(120); |
||
| 122 | |||
| 123 | // Declare a window to display the video |
||
| 124 | processor.displayInput("Current Frame"); |
||
| 125 | processor.displayOutput("Output Frame"); |
||
| 126 | |||
| 127 | // Play the video at the original frame rate |
||
| 128 | processor.setDelay(1000./processor.getFrameRate()); |
||
| 129 | |||
| 130 | // Start the process |
||
| 131 | processor.run(); |
||
| 132 | |||
| 133 | std::cout << processor.getFrameNumber() << std::endl; |
||
| 134 | std::cout << processor.getPositionMS() << std::endl; |
||
| 135 | |||
| 136 | cv::waitKey(); |
||
| 137 | } |