您的位置:首页 > 运维架构

opencv实践程序4——canny实现摄像头的边缘检测,高斯背景建模

2014-04-04 16:36 686 查看
/*
//原摄像头视频,轮廓,高斯模型
#include <iostream>
#include <cv.h>
#include <highgui.h>
#include"cvaux.h"
using namespace std;
using namespace cv;

int main( int argc, char** argv )
{
VideoCapture cap(0); //打开默认的摄像头号  
    Mat edges,fore;  
    namedWindow("edges",1); //边缘的窗口
int num = 0;   
    cap.open(num); 
BackgroundSubtractorMOG mog;
while(1)
    {  
        Mat frame;  
        cap >> frame; // 从摄像头中获取新的一帧  
if ( !frame.empty() )  imshow("Video", frame);//摄像头部分到这就结束了,下面全是关于轮廓的
        cvtColor(frame, edges, CV_BGR2GRAY);  
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);  
        Canny(edges, edges, 0, 30, 3);  
        imshow("edges", edges);  

mog(frame,fore,0.01);//封信背景并返回前景
threshold(fore,fore,128,255,THRESH_BINARY_INV);//对图像取反
        //imshow("原视频", frame);  
        imshow("提取的前景", fore);  

        if(waitKey(30) >= 0) break;  
}
cap.release();
return 0;
}*/
//教材上混合高斯算法
#include <iostream>
#include <cv.h>
#include <highgui.h>
#include "cvaux.h" 
using namespace std;
using namespace cv;

int main( int argc, char** argv )
{
	VideoCapture cap(0);  
	Mat frame,foreground; //前景图像
	//namedWindow("video",1);
    namedWindow("提取的前景",1); 
	//int num = 0;   
   // cap.open(num); 
	BackgroundSubtractorMOG mog;
	bool stop(false);
	while(!stop) 
    {  
		// Mat frame;  
		if(!cap.read(frame))break;
		mog(frame,foreground,0.01);//封信背景并返回前景
		threshold(foreground,foreground,128,255,THRESH_BINARY_INV);//对图像取反	
        imshow("原视频", frame);  
        imshow("提取的前景", foreground);  
        if(waitKey(10) >= 0) stop=true;  
	}
	//cap.release();
	   return 0;
	}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: