学更好的别人,
做更好的自己。
——《微卡智享》
前言
实现效果
代码演示
微卡智享
新建一个项目opencvimgeffect,配置参考《VS2017配置OpenCV通用属性》
向下展开核心代码
for (int i = 1; i
tmpsrc = src(Rect(0, 0, src.cols, i));
tmpsrc.copyTo(dst);
imshow("dst", dst);
waitKey(1);
}
向下展开的效果
从左向后展开
//从左向右展开
Mat dst2;
for (int i = 1; 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
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
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" 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);
futurevoid> vertical0 = async(launch::async, directionshow, src, src.cols, src.rows, 0);
futurevoid> vertical1 = async(launch::async, directionshow, src, src.cols, src.rows, 1);
futurevoid> vertical2 = async(launch::async, directionshow, src, src.cols, src.rows, 2);
futurevoid> vertical3 = async(launch::async, directionshow, src, src.cols, src.rows, 3);
//向下展开
//for (int i = 1; i
// tmpsrc = src(Rect(0, 0, src.cols, i));
// tmpsrc.copyTo(dst);
// imshow("dst", dst);
// waitKey(1);
//}
//从左向右展开
//Mat dst2;
//for (int i = 1; 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
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
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" endl;
waitKey(0);
}
完
扫描二维码
获取更多精彩
微卡智享
「 往期文章 」
本文分享自微信公众号 - 微卡智享(VaccaeShare)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。