当前位置:首页 > 通信资讯 > 正文

opencv图像做差(opencv把视频帧保存为图片)

本文为大家分享了使用opencv处理两张图片帧差的具体代码,供大家参考,具体内容如下

这个程序是两张图片做帧差,用C++实现的,把不同的地方用框框起来,仔细读一下程序,应该还是蛮简单的哈哈,opencv处理图片的基础。

opencv配置不用我说了吧,源码cmake编译,然后导入vs即可。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 #include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main() { Mat currentframe, previousframe; Mat img1, img2, img3; img1 = imread("D:/1129/20006/1123120.jpg"); img2 = imread("D:/1129/20006/1128120.jpg"); img3 = imread("D:/1129/20006/1128120.jpg"); cvtColor(img1, previousframe, CV_BGR2GRAY); cvtColor(img2, currentframe, CV_BGR2GRAY); //转化为单通道灰度图 absdiff(currentframe, previousframe, currentframe);//做差求绝对值 threshold(currentframe, currentframe, 130, 255.0, CV_THRESH_BINARY); dilate(currentframe, currentframe, Mat());//膨胀 erode(currentframe, currentframe, Mat());//腐蚀 imshow("moving area", currentframe); //显示图像 vector<vector<Point> > v; vector<Vec4i> hierarchy; Mat result; Rect rect; findContours(currentframe, v, hierarchy, RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); for (int i = 0; i < hierarchy.size(); i++) { rect = boundingRect(v.at(i)); //rect.width *= 1.5; rect.height *= 1.5; // 画最小的圆,贴着黑色 //drawContours(currentframe, v, i, Scalar(0, 0, 255), 1, 8, hierarchy); // 画矩形包围圆 rectangle(img3, rect, Scalar(0, 255, 0), 2); } imwrite("E:/res1.jpg", img3); imshow("moving area1", img3); //把当前帧保存作为下一次处理的前一帧 //cvtColor(tempframe, previousframe, CV_BGR2GRAY); waitKey(33); system("pause"); return 0; }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_42755375/article/details/84724516

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。