#ifndef CVBASE_H #define CVBASE_H #include "cv.h" #include "highgui.h" #include #include /** * Base class for basic OpenCV applications. * * Opens a display window and reads input frames from either a camera or video * file. Processes each frame (no operation by default) and displays the * results. * * Provides basic functions to pause/unpause the live input, emit debugging * information for selected frames, and to save both raw capture and processed * images. * * See cvdemo.cpp for a simple usage example and build instructions. * * You can use this class as a base for your own work. Just create a subclass * that overrides the necessary functions. The main place you probably need to * do this is the process() function. Also, the various *Ext*() functions are * hooks that mostly do nothing in the base class, but are convenient places * for you to insert your own code in a subclass (but note that you can also * override even non-Ext functions, if needed). * * If you do use this class, it is best if you do not modify it. I.e., use the * files cvbase.hpp and cvbase.cpp as you download them, with no modifications. * That way if it becomes necessary for us to supply an updated version of the * files (e.g. with bugfixes) you will be able to just easily drop in the * replacement, rather than having to merge it line-by-line with your code. * * @author vona **/ class CvBase { protected: /** The cv capture structure. **/ CvCapture *cap; /** Maximum frames per second. **/ float maxFPS; /** Minimum inter-frame delay. **/ int minDelayMS; /** Application name. **/ const char *appname; /** The file save image, if any (freed by destructor iff allocated). **/ IplImage *saveImage; /** Most recently captured image, if any (do not mutate or free). **/ IplImage *capImage; /** Most recently processed image, if any (do not free). **/ IplImage *procImage; /** Most recent captured frame number. **/ int frameN; /** Milliseconds per OpenCV clock tick. **/ double msPerTick; /** Whether processing is currently paused. **/ bool paused; /** Whether debug is requested for next frame. **/ bool dbg; /** Current time in milliseconds according to OpenCV clock. **/ virtual double nowMS() { return cvGetTickCount()*msPerTick; } /** Display command line help. **/ virtual void cmdHelp(); /** Whether the camera index is optional on the command line. **/ virtual bool camIndexOptional() { return true; } /** Display extra command line help, for subclasses. **/ virtual void cmdHelpExt() {} /** Extra command line parameters, for subclasses. **/ virtual std::string cmdHelpExtParams() { return ""; } /** Display GUI help. **/ virtual void guiHelp(); /** Display extra GUI help, for subclasses. **/ virtual void guiHelpExt() {} /** * Process the given frame. * * Default impl is identity. * * Note that the passed image may not be mutated. So to do any significant * processing, you will need to allocate your own return image. It is * typically best to do this once for the first frame, store the results in * subclass instance variables, and then deallocate any allocated space in * the subclass destructor. * * All frames are guaranteed to have the same dimensions and pixel format. * * This will be called for every iteration of mainLoop(), even while paused. * You may use the various instance fields (#frameN, #capImage, #paused, * #dbg, etc) to determine what operations to perform. Of course, when * writing a subclass, you may also add your own fields. * * @return the processed image, null will cause image display to be skipped * for this frame **/ virtual IplImage *process(IplImage *frame) { return frame; } /** * Handle keypresses. * * @return true to continue processing frames, false to end program **/ virtual bool handleKey(int code); /** * Keypresses not handled by the default implementation of handleKey() are * passed here. * * Default impl just prints the keycode. * * Overriding this is one way that subclasses can handle extra keypresses. **/ virtual bool handleKeyExt(int code) { std::cout<<"unhandled keycode "<