#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
Mat ProcessImage(Mat source)
{
Mat res;
//把彩色图转换成灰度图//
cvtColor(source, res, CV_BGR2GRAY);
//模糊化//
blur(res, res, Size(7, 7));
//边缘化//
Canny(res, res, 0, 30, 3);
//高斯模糊//
//GaussianBlur(source, res, Size(3,3),0,0,BORDER_DEFAULT);
//灰度化//
//cvtColor(res, res, CV_RGB2GRAY);
//Sobel算子//
//Sobel(res, res, CV_16S, 0, 1, 3, 1, 1, BORDER_DEFAULT);
//convertScaleAbs(res, res);
//二值化处理//
threshold(res, res, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY);
//闭环处理//
//morphologyEx(res, res, cv::MORPH_CLOSE, getStructuringElement(MORPH_RECT,
// Size(17, 5)));
return res;
}
int main()
{
//打开索引为0的系统摄像头//
VideoCapture capture(0);
//检测摄像头是否打开//
if (!capture.isOpened())
{
cout << "No open..." << endl;
return -1;
}
//定义结果集//
Mat resultMat;
//每隔30ms获取一次图片数据//
while (1)
{
//获取一帧的图片数据集//
Mat frame;
capture >> frame;
//检测数据集是否为空//
if (frame.empty())
{
cout << "No frame..." << endl;
}
else
{
//对图片数据集进行处理//
resultMat = ProcessImage(frame);
//显示处理后的图片//
imshow("result", resultMat);
//显示原图//
imshow("source", frame);
}
waitKey(30);
}
}
电脑摄像头在opencv的运用
最新推荐文章于 2024-03-07 12:55:02 发布