• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * test-image-stabilization.cpp - test image stabilization
3  *
4  *  Copyright (c) 2016 Intel Corporation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Author: Zong Wei <wei.zong@intel.com>
19  */
20 
21 
22 #include <unistd.h>
23 #include <getopt.h>
24 #include <string>
25 #include <base/xcam_defs.h>
26 #include "stabilizer.h"
27 
28 using namespace std;
29 using namespace cv;
30 using namespace cv::videostab;
31 
usage(const char * arg0)32 void usage(const char* arg0)
33 {
34     printf ("Usage:\n"
35             "%s --input file --output file\n"
36             "\t--input            input video\n"
37             "\t--output           output video\n"
38             "\t--enable-twopass   two pass stabilization\n"
39             "\t--enable-deblur    do deblur on output video\n"
40             "\t--wobble-suppress  do wobble suppress\n"
41             "\t--save             save file or not, default: true\n"
42             "\t--help             usage\n",
43             arg0);
44 }
45 
main(int argc,char * argv[])46 int main(int argc, char *argv[])
47 {
48     char inputPath[XCAM_MAX_STR_SIZE] = {0};
49     char outputPath[XCAM_MAX_STR_SIZE] = {0};
50     bool enableTwoPass = false;
51     bool enableDeblur = false;
52     bool wobbleSuppress = false;
53     bool saveOutput = true;
54 
55     const struct option long_opts[] = {
56         {"input", required_argument, NULL, 'i'},
57         {"output", required_argument, NULL, 'o'},
58         {"enable-twopass", required_argument, NULL, 'p'},
59         {"enable-deblur", required_argument, NULL, 'd'},
60         {"wobble-suppress", required_argument, NULL, 'w'},
61         {"save", required_argument, NULL, 's'},
62         {"help", no_argument, NULL, 'e'},
63         {NULL, 0, NULL, 0},
64     };
65 
66     int opt = -1;
67     while ((opt = getopt_long(argc, argv, "", long_opts, NULL)) != -1) {
68         switch (opt) {
69         case 'i':
70             strncpy (inputPath, optarg, XCAM_MAX_STR_SIZE);
71             break;
72         case 'o':
73             strncpy (outputPath, optarg, XCAM_MAX_STR_SIZE);
74             break;
75         case 'p':
76             enableTwoPass = (strcasecmp (optarg, "false") == 0 ? false : true);;
77             break;
78         case 'd':
79             enableDeblur = (strcasecmp (optarg, "false") == 0 ? false : true);
80             break;
81         case 'w':
82             wobbleSuppress = (strcasecmp (optarg, "false") == 0 ? false : true);
83             break;
84         case 's':
85             saveOutput = (strcasecmp (optarg, "false") == 0 ? false : true);
86             break;
87         case 'e':
88             usage (argv[0]);
89             return -1;
90         default:
91             printf ("getopt_long return unknown value:%c \n", opt);
92             usage (argv[0]);
93             return -1;
94         }
95     }
96 
97     if (optind < argc || argc < 2) {
98         printf ("unknown option %s \n", argv[optind]);
99         usage (argv[0]);
100         return -1;
101     }
102 
103     printf ("Description----------------\n");
104     printf ("input file:\t%s\n", inputPath);
105     printf ("output file:\t%s\n", outputPath);
106     printf ("enable two pass stabilizer:\t%s\n", enableTwoPass ? "true" : "false");
107     printf ("enable deblur:\t%s\n", enableDeblur ? "true" : "false");
108     printf ("enable wobble suppress:\t%s\n", wobbleSuppress ? "true" : "false");
109     printf ("save file:\t%s\n", saveOutput ? "true" : "false");
110     printf ("---------------------------\n");
111 
112     Ptr<VideoStabilizer> dvs = makePtr<VideoStabilizer>(enableTwoPass, wobbleSuppress, enableDeblur);
113     Ptr<StabilizerBase> stabilizer = dvs->stabilizer();
114 
115     Ptr<VideoFileSource> source = makePtr<VideoFileSource>(inputPath);
116     stabilizer->setFrameSource(source);
117 
118     int outputFps = source->fps();
119     Size frameSize = Size(source->width(), source->height());
120     cout << "frame count (rough): " << source->count() << endl;
121     cout << "output FPS: " << outputFps << endl;
122     cout << "frame size: " << source->width() << "x" << source->height() << endl;
123 
124     // stabilizer configuration
125     dvs->configFeatureDetector(1000, 15.0f);
126     dvs->configMotionFilter(15, 10.0f);
127 
128     // start to run
129     Mat stabilizedFrame, croppedStabilizedFrame;
130     int nframes = 0;
131 
132     while (!(stabilizedFrame = dvs->nextFrame()).empty())
133     {
134         nframes++;
135         cout << nframes << endl;
136 
137         // doing cropping here
138         croppedStabilizedFrame = dvs->cropVideoFrame(stabilizedFrame);
139 
140         if (saveOutput) {
141             if (!dvs->writer_.isOpened()) {
142                 dvs->writer_.open(outputPath, VideoWriter::fourcc('X', '2', '6', '4'),
143                                   outputFps, dvs->trimedVideoSize(frameSize));
144             }
145             dvs->writer_.write(croppedStabilizedFrame);
146         }
147 
148         imshow("stabilizedFrame", croppedStabilizedFrame);
149         char key = static_cast<char>(waitKey(3));
150         if (key == 27) {
151             cout << endl;
152             break;
153         }
154     }
155 
156     return 0;
157 }
158