打开摄像头并每一帧图像进行Canny算子边缘化操作
- 未经过滤直接进行边缘化操作
#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;
}
}
}
可以看到图像有很多的噪声
2.使用高斯滤波过滤噪声再边缘化