首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OpenCV图片动态特效显示(一)--展开显示

OpenCV图片动态特效显示(一)--展开显示

作者头像
Vaccae
发布2020-11-23 10:56:58
2.6K0
发布2020-11-23 10:56:58
举报
文章被收录于专栏:微卡智享微卡智享微卡智享

学更好的别人,

做更好的自己。

——《微卡智享》

本文长度为3033,预计阅读8分钟

前言

最近在规划自己的学习路径,大概又有了一个新的方向,正好最近抽着空做一些OpenCV的基础的小练习,图片的动态特效展示就是用了最简单的函数来做了一些效果。

实现效果

代码演示

微卡智享

新建一个项目opencvimgeffect,配置参考《VS2017配置OpenCV通用属性

向下展开核心代码

  for (int i = 1; i < src.rows; ++i) {
    tmpsrc = src(Rect(0, 0, src.cols, i));
    tmpsrc.copyTo(dst);

    imshow("dst", dst);
    waitKey(1);
  }

向下展开的效果

从左向后展开

  //从左向右展开
  Mat dst2;
  for (int i = 1; i < src.cols; ++i) {
    tmpsrc = src(Rect(0, 0, i, src.rows));
    tmpsrc.copyTo(dst2);

    imshow("dst2", dst2);
    waitKey(1);
  }

从左向右展开效果

从右向左,从下到上的效果也可以根据这样我们来实现,当然到这来说基本的这样显示就已经完成了,像文章开始那个同时展示的效果实现,我们就是把这几个方式封装起来了,然后使用C++11中的future的多线程方式呈现了出来。

封装函数

//垂直方向显示   direction 0-从上到下  1-从下到上 2-从左向右  3-从右向左
void directionshow(Mat src, int width, int height, int direction) {
  Mat tmpsrc, dst;
  if (direction == 0) {
    for (int i = 1; i < height; ++i) {
      tmpsrc = src(Rect(0, 0, width, i));
      tmpsrc.copyTo(dst);

      imshow("direction0", dst);
      waitKey(1);
    }
  }
  else if (direction == 1) {
    for (int i = height - 1; i > 0; --i) {
      tmpsrc = src(Rect(0, i, width, height - i));
      tmpsrc.copyTo(dst);

      imshow("direction1", dst);
      waitKey(1);
    }
  }
  else if (direction == 2) {
    for (int i = 1; i < width; ++i) {
      tmpsrc = src(Rect(0, 0, i, height));
      tmpsrc.copyTo(dst);

      imshow("direction2", dst);
      waitKey(1);
    }
  }
  else {
    for (int i = width - 1; i > 1; --i) {
      tmpsrc = src(Rect(i, 0, width - i, height));
      tmpsrc.copyTo(dst);

      imshow("direction3", dst);
      waitKey(1);
    }
  }
  cout << "over" << direction << endl;
  waitKey(0);
}

通过上面的封装函数后,我们再用多线程的方式进行调用,主要注意以下几点:

开头要加入future的引用

通过future<void>的方式进行线程的调用,关于这块的可以看我以前的学习|C++线程与指针结合的小例子》这篇文章。

完整代码

#include<iostream>
#include<opencv2/opencv.hpp>
#include<future>

using namespace cv;
using namespace std;

Mat src, dst, tmpsrc;
void directionshow(Mat, int, int, int);

int main(int argc, char** argv) {
  src = imread("E:/DCIM/test3.jpg");
  if (!src.data) {
    cout << "could not read src" << endl;
    return -1;
  }

  imshow("src", src);

  future<void> vertical0 = async(launch::async, directionshow, src, src.cols, src.rows, 0);
  future<void> vertical1 = async(launch::async, directionshow, src, src.cols, src.rows, 1);
  future<void> vertical2 = async(launch::async, directionshow, src, src.cols, src.rows, 2);
  future<void> vertical3 = async(launch::async, directionshow, src, src.cols, src.rows, 3);
  //向下展开
  //for (int i = 1; i < src.rows; ++i) {
  //  tmpsrc = src(Rect(0, 0, src.cols, i));
  //  tmpsrc.copyTo(dst);

  //  imshow("dst", dst);
  //  waitKey(1);
  //}

  //从左向右展开
  //Mat dst2;
  //for (int i = 1; i < src.cols; ++i) {
  //  tmpsrc = src(Rect(0, 0, i, src.rows));
  //  tmpsrc.copyTo(dst2);

  //  imshow("dst2", dst2);
  //  waitKey(1);
  //}


  waitKey(0);
  return 0;
}

//垂直方向显示   direction 0-从上到下  1-从下到上 2-从左向右  3-从右向左
void directionshow(Mat src, int width, int height, int direction) {
  Mat tmpsrc, dst;
  if (direction == 0) {
    for (int i = 1; i < height; ++i) {
      tmpsrc = src(Rect(0, 0, width, i));
      tmpsrc.copyTo(dst);

      imshow("direction0", dst);
      waitKey(1);
    }
  }
  else if (direction == 1) {
    for (int i = height - 1; i > 0; --i) {
      tmpsrc = src(Rect(0, i, width, height - i));
      tmpsrc.copyTo(dst);

      imshow("direction1", dst);
      waitKey(1);
    }
  }
  else if (direction == 2) {
    for (int i = 1; i < width; ++i) {
      tmpsrc = src(Rect(0, 0, i, height));
      tmpsrc.copyTo(dst);

      imshow("direction2", dst);
      waitKey(1);
    }
  }
  else {
    for (int i = width - 1; i > 1; --i) {
      tmpsrc = src(Rect(i, 0, width - i, height));
      tmpsrc.copyTo(dst);

      imshow("direction3", dst);
      waitKey(1);
    }
  }
  cout << "over" << direction << endl;
  waitKey(0);
}

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-11-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 微卡智享 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 向下展开核心代码
  • 从左向后展开
  • 封装函数
  • 完整代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档