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

学更好的别人,

做更好的自己。

——《微卡智享》

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

前言

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

format,png

实现效果

format,png

代码演示

format,png

微卡智享

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

format,png

向下展开核心代码

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


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

向下展开的效果

format,png

从左向后展开

  //从左向右展开
  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);
  }

从左向右展开效果

format,png

从右向左,从下到上的效果也可以根据这样我们来实现,当然到这来说基本的这样显示就已经完成了,像文章开始那个同时展示的效果实现,我们就是把这几个方式封装起来了,然后使用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);
}


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

format,png

开头要加入future的引用

format,png

通过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);
}


format,png

format,png

扫描二维码

获取更多精彩

微卡智享

format,png

「 往期文章 」

OpenCV实现图像转换为素描效果

C++ OpenCV输出中文

C++ OpenCV标记函数drawMarker的使用

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vaccae

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值