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);}
从左向右展开效果
封装函数
//垂直方向显示 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的引用
完整代码
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);}
完
扫描二维码
获取更多精彩
微卡智享
「 往期文章 」
本文分享自微信公众号 - 微卡智享(VaccaeShare)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。






