00001
00002 #include <stdio.h>
00003 #include <iostream>
00004
00005 #include "opencv_vs.h"
00006 #include "iniparser.h"
00007
00008 using namespace std;
00009
00010 void OpenCVFactory::registerParameters(ParamSection *sec) {
00011 sec->addStringParam("opencv.source", &source, "0");
00012 sec->addBoolParam("opencv.use", &use, true);
00013 sec->addIntParam("opencv.width", &width, -1);
00014 sec->addIntParam("opencv.height", &height, -1);
00015 };
00016
00017 VideoSource *OpenCVFactory::construct() {
00018 if (use) {
00019 OpenCVVideoSource *vs = new OpenCVVideoSource(source, width, height);
00020 if (vs->initialize()) return vs;
00021 delete vs;
00022 }
00023 return 0;
00024 }
00025
00026 OpenCVVideoSource::OpenCVVideoSource(char *source, int w, int h)
00027 : source(source), width(w), height(h)
00028 {
00029 playing=true;
00030 frameCnt = 0;
00031 frame = 0;
00032 movie = false;
00033 }
00034
00035 bool OpenCVVideoSource::initialize()
00036 {
00037 if (strlen(source)==0 || (strlen(source)==1 && isdigit(source[0])))
00038 capture = cvCaptureFromCAM(strlen(source)>0?(source[0]-'0'):0);
00039 else {
00040 capture = cvCaptureFromAVI(source);
00041 movie = true;
00042 }
00043
00044 if(width>0 && height>0) {
00045 cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH, width);
00046 cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT, height);
00047 }
00048
00049 return capture!=0;
00050 }
00051
00052 bool OpenCVVideoSource::getFrame(IplImage *dst)
00053 {
00054
00055 if (!frame || playing) {
00056 frame = cvQueryFrame( capture );
00057 frameCnt++;
00058 }
00059
00060 if (!frame) return false;
00061
00062 if (frame->depth == dst->depth) {
00063 if (frame->nChannels == 3 && dst->nChannels==1) {
00064 cvCvtColor(frame,dst, CV_BGR2GRAY);
00065 } else if (frame->width == dst->width && frame->height==dst->height)
00066 cvCopy(frame,dst);
00067 else
00068 cvResize(frame,dst);
00069 } else {
00070 if (frame->width == dst->width && frame->height==dst->height)
00071 cvConvertScale(frame,dst);
00072 else {
00073 IplImage *tmp = cvCreateImage(cvGetSize(frame), dst->depth, dst->nChannels);
00074 cvConvertScale(frame,tmp);
00075 cvResize(tmp,dst);
00076 cvReleaseImage(&tmp);
00077 }
00078 }
00079 return true;
00080 }
00081
00082 int OpenCVVideoSource::getId()
00083 {
00084 if (movie) {
00085 double pos = cvGetCaptureProperty(capture, CV_CAP_PROP_POS_MSEC);
00086 double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
00087 return pos;
00088
00089 } else
00090 return frameCnt;
00091 }
00092
00093 OpenCVVideoSource::~OpenCVVideoSource() {
00094 }
00095
00096 void OpenCVVideoSource::start() {
00097 playing = true;
00098 }
00099
00100 void OpenCVVideoSource::stop() {
00101 playing = false;
00102 }
00103
00104
00105 void OpenCVVideoSource::getSize(int &width, int &height)
00106 {
00107 if (!frame)
00108 frame = cvQueryFrame( capture );
00109 width = frame->width;
00110 height = frame->height;
00111 }