qq_40827854 2017-11-18 07:03 采纳率: 0%
浏览 1754
已结题

使用OPENCV进行影片拆图片并逐一拼接 两种方法 其中一个无法成功

编译环境用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)

  • 写回答

1条回答 默认 最新

  • 深圳老凡 2018-07-20 08:04
    关注

    第一种方法与第二种方法的差别在于,先读取10张图片在拼接,修改读取图片那段就可以了

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)