赞
踩
第一种:
关键函数cvAbsDiff()
代码如下:
- #include "cv.h"
- #include "highgui.h"
- #include "cxcore.h"
-
- int main(int argc,char** argv)
- {cvNamedWindow("a",0);
- IplImage* img=cvLoadImage("11.jpg");
- cvShowImage("a",img);
-
- cvNamedWindow("b",0);
- IplImage* img1=cvLoadImage("12.jpg");
- cvShowImage("b",img1);
-
- IplImage* diff=cvCreateImage(cvGetSize(img),img->depth,img->nChannels);
- cvAbsDiff(img,img1,diff);
- cvNamedWindow("r",0);
- cvShowImage("r",diff);
-
-
- while (1)
- {if (cvWaitKey(100)==27) break;
- }
-
- cvDestroyWindow("a");
- cvDestroyWindow("b");
- cvReleaseImage(&img);
- cvReleaseImage(&img1);
- cvReleaseImage(&diff);
- return 0;
- }
第二种:
这个程序是两张图片做帧差,用C++实现的,把不同的地方用框框起来
- #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;
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。