opencv+自带摄像头+canny算子边缘化

打开摄像头并每一帧图像进行Canny算子边缘化操作

  1. 未经过滤直接进行边缘化操作
#include <iostream>
#include <opencv2/core/core.hpp> 
#include<opencv2/highgui/highgui.hpp> 
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
	//打开第一个摄像头
	VideoCapture cap(0);
	//检查摄像头是否成功打开
	if (!cap.isOpened())
	{
		cout << "摄像头未成功打开" << endl;
	}
	//创建Mat对象
	Mat M;
	//创建窗口
	namedWindow("打开摄像头", 1);
	//读取摄像头中的帧
	for (;;)
	{
		Mat frame;
		//从cap中读取一帧存到frame中
		cap >> frame;
		//判断是否读取到
		if (frame.empty())
		{
			break;
		}
		//将摄像头读取到的图像转化为灰度图
		cvtColor(frame, M, CV_BGR2GRAY);
		//进行canny算子边缘化提取
		Canny(M, M, 0, 30, 3);
		//在窗口中显示视频中每一帧被边缘化的图像
		imshow("打开摄像头", M);
		//等待300秒,如果按键则退出循环
		if (waitKey(300) >= 0)
		{
			break;
		}
	}
}

可以看到图像有很多的噪声
opencv+自带摄像头+canny算子边缘化
2.使用高斯滤波过滤噪声再边缘化

#include <iostream>
#include <opencv2/core/core.hpp> 
#include<opencv2/highgui/highgui.hpp> 
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
	//打开第一个摄像头
	VideoCapture cap(0);
	//检查摄像头是否成功打开
	if (!cap.isOpened())
	{
		cout << "摄像头未成功打开" << endl;
	}
	//创建Mat对象
	Mat M;
	//创建窗口
	namedWindow("打开摄像头", 1);
	//读取摄像头中的帧
	for (;;)
	{
		Mat frame;
		//从cap中读取一帧存到frame中
		cap >> frame;
		//判断是否读取到
		if (frame.empty())
		{
			break;
		}
		//将摄像头读取到的图像转化为灰度图
		cvtColor(frame, M, CV_BGR2GRAY);
		//进行canny算子边缘化提取
		GaussianBlur(M, M, Size(7, 7), 1.5, 1.5);
		//高斯滤波使图像平滑
		Canny(M, M, 0, 30, 3);
		//在窗口中显示视频中每一帧被边缘化的图像
		imshow("打开摄像头", M);
		//等待300秒,如果按键则退出循环
		if (waitKey(300) >= 0)
		{
			break;
		}
	}
}
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
using namespace cv;
int main()
{
	VideoCapture cap(0);
	if (!cap.isOpened())
	{
		return -1;
	}
	Mat frame;
	Mat edges;
	bool stop = false;
	while (!stop)

	{
		cap >> frame;
		cvtColor(frame, edges, CV_BGR2GRAY);
		GaussianBlur(edges, edges, Size(7, 7), 1.5, 1.5);
		Canny(edges, edges, 0, 30, 3);
		imshow("1", edges);
		if (waitKey(30) >= 0)
			stop = true;
	}
	return 0;
}

GaussianBlur()函数用高斯滤波器(GaussianFilter)对图像进行平滑处理。
opencv+自带摄像头+canny算子边缘化