欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

opencv图片处理和摄像头边缘检测

发布时间:2025/4/5 70 豆豆
生活随笔 收集整理的这篇文章主要介绍了 opencv图片处理和摄像头边缘检测 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

腐蚀

结果

代码
erode函数

#include "pch.h" #include <iostream> #include<opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp>//Opencv highgui 模块头文件 #include<opencv2/imgproc/imgproc.hpp>//图像处理头文件 using namespace cv;int main(){//功能一。进行腐蚀操作Mat srcImage = imread("cat15.jpg",2|4);//loading pictureimshow("原始图", srcImage);//show picturewaitKey(0);//按键Mat element = getStructuringElement(MORPH_RECT,Size(15,15));Mat dstImage;erode(srcImage,dstImage,element);imshow("效果图",dstImage);waitKey(0);//wait for any keyreturn 0; }

图像模糊

结果

代码
均值滤波blur()函数的使用

#include "pch.h" #include <iostream> #include<opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp>//Opencv highgui 模块头文件 #include<opencv2/imgproc/imgproc.hpp>//图像处理头文件 using namespace cv;int main(){//功能二:图像模糊Mat srcImage = imread("catandmao.jpg");imshow("均值滤波原始图",srcImage);Mat dstImage;blur(srcImage,dstImage,Size(7,7));imshow("均值滤波效果图",dstImage);waitKey(0); }

边缘检测

结果

代码
canny算子

#include "pch.h" #include <iostream> #include<opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp>//Opencv highgui 模块头文件 #include<opencv2/imgproc/imgproc.hpp>//图像处理头文件 using namespace cv;int main(){//功能三练习。canny 边缘检测Mat srcImage = imread("catandmao.jpg");imshow("原始图",srcImage);Mat edge, greyImage;//将原图转化为灰度图像cvtColor(srcImage,greyImage,CV_BGR2GRAY);//使用3*3内核来降噪blur(greyImage,edge,Size(3,3));//运行canny算子Canny(edge, edge, 3, 9, 3);//显示效果图imshow("边缘检测,效果图",edge);waitKey(0);}

视频显示

读取并播放视频
代码

#include "pch.h" #include <iostream> #include<opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp>//Opencv highgui 模块头文件 #include<opencv2/imgproc/imgproc.hpp>//图像处理头文件 using namespace cv;int main(){VideoCapture capture("H:\\how.mp4");//需要使用两个反斜杠while (1){Mat frame;//定义一个Mat变量,用于存储每一帧的图像capture >> frame;//读取当前帧imshow("读取视频",frame);waitKey(30);}}

打开摄像头

代码

#include "pch.h" #include <iostream> #include<opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp>//Opencv highgui 模块头文件 #include<opencv2/imgproc/imgproc.hpp>//图像处理头文件 using namespace cv; int main(){//功能五:摄像头采集VideoCapture capture(0);while (1){Mat frame;//定义一个Mat变量,用于存储每一帧的图像capture >> frame;//读取当前帧imshow("读取视频", frame);waitKey(30);} }

摄像头配合边缘检测

结果
代码

#include "pch.h" #include <iostream> #include<opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp>//Opencv highgui 模块头文件 #include<opencv2/imgproc/imgproc.hpp>//图像处理头文件 using namespace cv; int main(){//功能五:摄像头配合canny边缘检测VideoCapture capture(0);Mat edges;while (true){Mat frame;capture >> frame;cvtColor(frame, edges, CV_BGR2GRAY);blur(edges,edges,Size(7,7));Canny(edges, edges, 0, 30, 3);imshow("canny边缘化之后的视频", edges);if (waitKey(30) >= 0)break;}return 0; }

注:以上基于x64,debug模式

总结

以上是生活随笔为你收集整理的opencv图片处理和摄像头边缘检测的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。