编译环境用Visual Studio 2012
OPENCV2413
要弄得程式概念就是先把影片拆成一堆图片(影格)存起来
再来把每个图片(影格)跟前面的合成变成另一堆图片(影格)
(目前只能处理单向无杂物影片,未来想处理双向影片,以及有杂物的影片,还有制作单向影片的播放器,然后更进一步制作双向影片的播放器)
第一种方法大概就是
1,2,3,4,5,6,7,8,9,10,11,12...
第一张变成1
第二张变成1+2
第三张变成1+2+3
...
第十张变成1+2+...+10
第十一张变成"刚刚弄好的第十张"+11
也就是累积好十张就直接用那张
#include
#include
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/stitching/stitcher.hpp"
#include
using namespace std;
using namespace cv;
int main(int argc, char* argv[])
{
bool try_use_gpu = false;
vector imgs;
Mat img;
VideoCapture video("Produce.avi");
char str[100];
int a = 0;
string c = "Produce ";
string b = ".jpg";
while(true)
{
a++;
video >> img;
if( img.empty())
{
break;
}
sprintf(str, "%s%d%s", c , a , b);
imwrite(str, img);
}
for (int i = 1; i <= a; i+=10)
{
for (int j = i; j <= i + 9 && j <= a; j++)
{
for (int k = i; k <= j; k++)
{
sprintf(str, "%s%d%s", c , k , b);
img = imread(str);
imgs.push_back(img);
}
if (i / 10 > 0)
{
for (int k = 1; k <= i / 10; k++)
{
sprintf(str, "%d%s", k*10, b);
img = imread(str);
imgs.push_back(img);
}
}
for(int k=0;k<imgs.size();k++)
{
imshow("",imgs[k]);
waitKey(2000);
}
Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
Stitcher::Status status = stitcher.stitch(imgs, img);
sprintf(str, "%d%s", j, b);
imwrite(str, img);
imshow("/",img);
waitKey(2000);
imgs.clear();
}
}
return 0;
}
第二种方法便是每次都跟上一次变好的拼接
1,2,3,4,5,.....
第一张变成1
第二张变成"刚刚弄好的第一张"+2
第三张变成"刚刚弄好的第二张"+3
...
#include
#include
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/stitching/stitcher.hpp"
#include
#include
using namespace std;
using namespace cv;
int main(int argc, char* argv[])
{
bool try_use_gpu = false;
vector imgs;
Mat img;
VideoCapture video("Produce.avi");
char str[100];
int a = 0;
string c = "Produce ";
string b = ".jpg";
while(true)
{
a++;
video >> img;
if( img.empty())
{
break;
}
sprintf(str, "%s%d%s", c , a , b);
imwrite(str, img);
}
for (int i = 1; i <= a; i++)
{
sprintf(str, "%s%d%s", c , i , b);
img = imread(str);
imgs.push_back(img);
if ( i == 1)
{
sprintf(str, "%d%s", i , b);
imwrite(str, img);
imgs.clear();
}
else
{
sprintf(str, "%d%s", i-1 , b);
img = imread(str);
imgs.push_back(img);
for(int j=0;j<imgs.size();j++)
{
imshow("",imgs[j]);
waitKey(2000);
}
Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
Stitcher::Status status = stitcher.stitch(imgs, img);
cout<<status<<" ";
imshow("/",img);
waitKey(2000);
sprintf(str, "%d%s", i , b);
imwrite(str, img);
imgs.clear();
}
}
return 0;
}
实测之后影片拆图片(影格)都是ok的
第一种方法成功但跑颇久
为了让他跑快一点 我写了第二种方法
但是第二种方法在合第二张就出事了
明明都是用
Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
Stitcher::Status status = stitcher.stitch(imgs, img);
这两行合成的
status输出也都是0 (OK)