00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024
00025
00026 #include <QUrl>
00027
00028 #ifdef QVIPP
00029 #include <qvipp.h>
00030 #endif // QVIPP
00031
00032 #include <QVOpenCVReader>
00033
00034 bool QVOpenCVReader:: open(const QString & url_string,
00035 unsigned int & suggested_cols, unsigned int & suggested_rows,
00036 unsigned int & suggested_fps, QVVideoReader::OpenOptions & suggested_options,
00037 QVVideoReader::TSourceMode & source_mode)
00038 {
00039
00040 source_mode = QVVideoReader::RGBMode;
00041
00042 isFile = isCamera = false;
00043
00044 QUrl URL(url_string);
00045 if(URL.scheme() == "cvf") {
00046 isFile = true;
00047 QString fileName = URL.path();
00048 video_capture = new cv::VideoCapture(fileName.toStdString());
00049 } else if(URL.scheme() == "cvc") {
00050 isCamera = true;
00051
00052 bool okDevice;
00053
00054 int device = URL.fragment().toInt(&okDevice);
00055 if(okDevice)
00056 video_capture = new cv::VideoCapture(device);
00057 } else {
00058 std::cout << "Warning:: unknown scheme " << qPrintable (URL.scheme())
00059 << "in QVOpenCVReader::open() method\n";
00060 return false;
00061 }
00062
00063
00064
00065
00066
00067 if(suggested_rows != 0)
00068 if(not video_capture->set(CV_CAP_PROP_FRAME_HEIGHT,suggested_rows))
00069 {;}
00070 if(suggested_cols != 0)
00071 if(not video_capture->set(CV_CAP_PROP_FRAME_WIDTH,suggested_cols))
00072 {;}
00073 if(suggested_fps != 0)
00074 if(not video_capture->set(CV_CAP_PROP_FPS,suggested_fps))
00075 {;}
00076
00077 double value;
00078 value = video_capture->get(CV_CAP_PROP_FRAME_WIDTH);
00079 suggested_cols = static_cast<int>(value);
00080 value = video_capture->get(CV_CAP_PROP_FRAME_HEIGHT);
00081 suggested_rows = static_cast<int>(value);
00082 value = video_capture->get(CV_CAP_PROP_FPS);
00083 suggested_fps = static_cast<int>(value);
00084 value = video_capture->get(CV_CAP_PROP_FRAME_COUNT);
00085 total_frames = static_cast<int>(value);
00086
00087
00088
00089 noLoop = (suggested_options & QVVideoReader::NoLoop);
00090
00091
00092 video_capture->set(CV_CAP_PROP_CONVERT_RGB,1);
00093
00094 if(video_capture->isOpened())
00095 return TRUE;
00096 else
00097 return FALSE;
00098 }
00099
00100 bool QVOpenCVReader::close()
00101 {
00102 if(video_capture != 0)
00103 delete video_capture;
00104 return true;
00105 }
00106
00107
00108 bool QVOpenCVReader::grab(QVImage<uChar,1> &imgR, QVImage<uChar,1> &imgG, QVImage<uChar,1> &imgB)
00109 {
00110 cv::Mat frame;
00111
00112 bool grabbedframe = video_capture->grab();
00113 if(not noLoop & not grabbedframe) {
00114 seek(0);
00115 grabbedframe = video_capture->grab();
00116 }
00117
00118 if(grabbedframe)
00119 video_capture->retrieve(frame);
00120 else
00121 return FALSE;
00122
00123 IplImage ipl_image = frame;
00124 QVImage<uChar,3> ColorImage = QVImage<uChar,3>(&ipl_image);
00125 imgR = imgG = imgB = QVImage<uchar>(ColorImage.getCols(),ColorImage.getRows());
00126
00127 Copy(ColorImage,0,imgB);
00128 Copy(ColorImage,1,imgG);
00129 Copy(ColorImage,2,imgR);
00130
00131 return TRUE;
00132
00133 }
00134
00135 int QVOpenCVReader::getLength()
00136 {
00137 return total_frames;
00138 }
00139
00140 int QVOpenCVReader::getPos()
00141 {
00142 double value;
00143 if(isFile)
00144 value = video_capture->get(CV_CAP_PROP_POS_FRAMES);
00145 else
00146 value = -1;
00147
00148
00149 return static_cast<int>(value);
00150 }
00151
00152 bool QVOpenCVReader::seek(int pos)
00153 {
00154 double value = static_cast<double>(pos);
00155
00156
00157 return video_capture->set(CV_CAP_PROP_POS_FRAMES,value);
00158 }