00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00057 #include <QVApplication>
00058 #include <QVPropertyContainer>
00059 #include <QVImage>
00060 #include <QVVideoReader>
00061
00062 int main(int argc, char *argv[])
00063 {
00064 QVApplication app(argc,argv,
00065 "This program will save a series of requested frames in individual image files, as captured \n"
00066 "from a generic video source, which can be anything from a webcam or a firewire cam, to a video \n"
00067 "file, a TDT channel, a DVD, remote sources (via http or rtsp protocols) to even directories \n"
00068 "containing sequences of images in individual frames. See documentation on class QVVideoReader \n"
00069 "for further information on about the format and types of accepted source video identifier \n"
00070 "strings.\n"
00071 "\nHere are some URL examples:\n"
00072 "Reading directly an uncompressed yuv4mpeg file:\n"
00073 " ./videoReader --URL=yuv4mpeg:///home/pedroe/svnvideos/futbol.yuv\n"
00074 "Reading directly a sequence of images in a given directory:\n"
00075 " ./videoReader --URL=dir:///home/pedroe/imagenes\n"
00076 "Reading a video file through OpenCV (needs the OpenCV library):\n"
00077 " ./videoReader --URL=cvf:///home/pedroe/svnvideos/arrow.avi\n"
00078 "Reading a webcam/firewire camera through OpenCV (needs the OpenCV library):\n"
00079 " ./videoReader --URL=cvc://#0\n"
00080 "Reading a video file through mplayer (needs mplayer installed):\n"
00081 " ./videoReader --URL=file:///home/pedroe/svnvideos/penguin.dv\n"
00082 "Reading remote sources using mplayer(needs mplayer installed):\n"
00083 " ./videoReader --URL=http://perception.inf.um.es/public_data/videos/misc/minuto.avi\n"
00084 " ./videoReader --URL=rtsp://v7.cache4.c.youtube.com/CjgLENy73wIaLwmNoYPdmgJFpRMYJCAkFEIJbXYtZ29vZ2xlSARSB3Jlc3VsdHNg-uzp15Dl_9FNDA==/0/0/0/video.3gp",
00085 FALSE);
00086
00087
00088 QVPropertyContainer forArgs("videoReader");
00089 forArgs.addProperty<QString>("URL",QVPropertyContainer::inputFlag,"ocvc:#0",
00090 "URL to read video from (default OpenCV main camera)");
00091 forArgs.addProperty<int>("Rows",QVPropertyContainer::inputFlag,0,
00092 "Number of rows of output image (0->same as input)",0,65536);
00093 forArgs.addProperty<int>("Cols",QVPropertyContainer::inputFlag,0,
00094 "Number of columns of output image (0->same as input)",0,65536);
00095 forArgs.addProperty<int>("fps",QVPropertyContainer::inputFlag,0,
00096 "(Requested) number of frames per second (0->same as input)",0,200);
00097 forArgs.addProperty<bool>("NoLoop",QVPropertyContainer::inputFlag,false,
00098 "Whether the video should loop or not when reaching the end (only valid for non live sources)");
00099 forArgs.addProperty<bool>("Deinterlaced",QVPropertyContainer::inputFlag,false,
00100 "Whether the video should be deinterlaced or not (only valid for mplayer sources)");
00101 forArgs.addProperty<bool>("RealTime",QVPropertyContainer::inputFlag,false,
00102 "If the camera should be read in real time (at requested fps) or as fast as possible (not valid for live sources)");
00103 forArgs.addProperty<int>("skip",QVPropertyContainer::inputFlag,0,
00104 "Number of frames to skip between dumped frames",0,65536);
00105 forArgs.addProperty<int>("frames",QVPropertyContainer::inputFlag,10,
00106 "Number of frames to dump",1,65536);
00107 forArgs.addProperty<int>("initpos",QVPropertyContainer::inputFlag,0.0,
00108 "Initial position of video (in number of frames)",0.0,100000.0);
00109 forArgs.addProperty<int>("mode",QVPropertyContainer::inputFlag,0,
00110 "Read mode (0->RGB, 1->RGB (separate), 2->Gray, 3->YUV (separate))",0,3);
00111 forArgs.addProperty<QString>("ext",QVPropertyContainer::inputFlag,"jpg",
00112 "Image format for output images (selected by extension name)");
00113
00114 QString URL = forArgs.getPropertyValue<QString>("URL");
00115 unsigned int Rows = forArgs.getPropertyValue<int>("Rows");
00116 unsigned int Cols = forArgs.getPropertyValue<int>("Cols");
00117 unsigned int fps = forArgs.getPropertyValue<int>("fps");
00118 bool NoLoop = forArgs.getPropertyValue<bool>("NoLoop");
00119 bool Deinterlaced = forArgs.getPropertyValue<bool>("Deinterlaced");
00120 bool RealTime = forArgs.getPropertyValue<bool>("RealTime");
00121 QVVideoReader::OpenOptions options = QVVideoReader::Default;
00122 if(NoLoop) options |= QVVideoReader::NoLoop;
00123 if(Deinterlaced) options |= QVVideoReader::Deinterlaced;
00124 if(RealTime) options |= QVVideoReader::RealTime;
00125 unsigned int skip = forArgs.getPropertyValue<int>("skip");
00126 unsigned int frames = forArgs.getPropertyValue<int>("frames");
00127 unsigned int initpos = forArgs.getPropertyValue<int>("initpos");
00128 unsigned int mode = forArgs.getPropertyValue<int>("mode");
00129 QString ext = forArgs.getPropertyValue<QString>("ext");
00130
00131
00132 if(app.arguments().contains("--help"))
00133 {
00134 std::cout << qPrintable(app.getHelp());
00135 exit(0);
00136 }
00137
00138
00139 QStringList extensions;
00140 extensions << "jpg" << "png" << "bmp" << "pgm" << "gif" << "jpeg" << "pbm" << "ppm" << "tiff" << "xbm" << "xpm";
00141 if(not extensions.contains(ext))
00142 {
00143 std::cout << "Error: output format " << qPrintable(ext) << " not supported.\n";
00144 exit(1);
00145 }
00146
00147
00148 QVVideoReader videoReader;
00149 QVVideoReader::TSourceMode source_mode;
00150 if (!videoReader.open(URL,Cols,Rows,fps,options,source_mode))
00151 {
00152 std::cout << "Error opening requested video source (" << qPrintable(URL) << ").\n";
00153 exit(2);
00154 }
00155 std::cout << "Video source successfully opened. Info on source:\n";
00156 std::cout << " Output cols: " << Cols << ".\n";
00157 std::cout << " Output rows: " << Rows << ".\n";
00158 std::cout << " Output fps: " << fps << ".\n";
00159 std::cout << " Video preferred source mode (other outputs need conversion): ";
00160 if(source_mode == QVVideoReader::YUVMode)
00161 std::cout << "YUV mode.";
00162 else if(source_mode == QVVideoReader::RGBMode)
00163 std::cout << "RGB mode.";
00164 else if(source_mode == QVVideoReader::GrayOnlyMode)
00165 std::cout << "Gray only mode.";
00166 std::cout << "\n";
00167
00168
00169 if(initpos != 0)
00170 videoReader.seek(initpos);
00171
00172
00173 QVImage<uChar,3> imageRGB;
00174 QVImage<uChar> image_planes[3];
00175 unsigned int count = 0;
00176 while(videoReader.grab())
00177 {
00178 QStringList imagenames;
00179 if(mode==0)
00180 {
00181 videoReader.getRGBImage(imageRGB);
00182 imagenames.append(QString("frame-RGB-%1.%2").arg((short int)count,4,10,QChar('0')).arg(ext));
00183 }
00184 else if(mode==1)
00185 {
00186 videoReader.getRGBImage(image_planes[0],image_planes[1],image_planes[2]);
00187 imagenames.append(QString("frame-R-%1.%2").arg((short int)count,4,10,QChar('0')).arg(ext));
00188 imagenames.append(QString("frame-G-%1.%2").arg((short int)count,4,10,QChar('0')).arg(ext));
00189 imagenames.append(QString("frame-B-%1.%2").arg((short int)count,4,10,QChar('0')).arg(ext));
00190 }
00191 else if(mode==2)
00192 {
00193 videoReader.getGrayImage(image_planes[0]);
00194 imagenames.append(QString("frame-gray-%1.%2").arg((short int)count,4,10,QChar('0')).arg(ext));
00195 }
00196 else
00197 {
00198 videoReader.getYUVImage(image_planes[0],image_planes[1],image_planes[2]);
00199 imagenames.append(QString("frame-Y-%1.%2").arg((short int)count,4,10,QChar('0')).arg(ext));
00200 imagenames.append(QString("frame-U-%1.%2").arg((short int)count,4,10,QChar('0')).arg(ext));
00201 imagenames.append(QString("frame-V-%1.%2").arg((short int)count,4,10,QChar('0')).arg(ext));
00202 }
00203 count++;
00204 if((count-1) % (skip+1) != 0)
00205 continue;
00206 for(int i=0;i<imagenames.size();i++)
00207 {
00208 QString imagename = imagenames[i];
00209 std::cout << "Saving image " << qPrintable(imagename) << ".\n";
00210 QImage qimage;
00211 if(mode==0)
00212 qimage = imageRGB;
00213 else
00214 qimage = QVImage<uchar,3>(image_planes[i],image_planes[i],image_planes[i]);
00215 qimage.save(imagename);
00216 }
00217 frames--;
00218 if(frames == 0)
00219 break;
00220 }
00221
00222
00223 std::cout << "Length of video source (in frames): " << videoReader.getLength() << "\n";
00224 std::cout << "Current position in video source (in frames): " << videoReader.getPos() << "\n";
00225 std::cout << "Complete URL of video source: " << qPrintable(videoReader.getURL()) << "\n";
00226 std::cout << "URL scheme of video source: " << qPrintable(videoReader.getScheme()) << "\n";
00227 std::cout << "Number of columns of video source: " << videoReader.getCols() << "\n";
00228 std::cout << "Number of rows of video source: " << videoReader.getRows() << "\n";
00229 std::cout << "FPS of video source: " << videoReader.getFPS() << "\n";
00230
00231
00232 videoReader.close();
00233 std::cout << "Done!\n";
00234 }