【OpenCV】笔记(11)——边缘检测
来源:互联网 发布:java售票系统 编辑:程序博客网 时间:2024/06/07 08:27
边缘检测
Tip:abort()终值,这种错误最常见于,1.参数设置错误;2.直接从别处粘贴代码,含有隐含的编码不一致的情况
用形态学来检测边缘的原理非常简单:
case CV_MOP_GRADIENT: erode( src, temp, kernel, anchor, iterations, borderType, borderValue ); dilate( src, dst, kernel, anchor, iterations, borderType, borderValue ); dst -= temp; break;
可以看出来,它是对图像先做了一个腐蚀,再做了一次膨胀,然后将两次的结果相减即可。
#include <opencv2/opencv.hpp>#include <opencv2/imgproc.hpp>using namespace std;using namespace cv;int main(){ //1 read the source image Mat src; src = imread("industrial.png" ); namedWindow( "Src", WINDOW_AUTOSIZE ); imshow( "Src", src); //2 RGB2GRAY Mat dst; cvtColor(src, dst, COLOR_BGR2GRAY); //3 morphology Mat edge; morphologyEx(dst, edge, MORPH_GRADIENT, Mat ()); //4 threshold threshold(edge, edge, 40, 255, THRESH_BINARY); //5 show the edge namedWindow( "Edge", WINDOW_AUTOSIZE ); imshow( "Edge", edge); //6 wait waitKey(); return 0;}
#include <iostream>#include <opencv2/opencv.hpp>#include <opencv.hpp>using namespace std;using namespace cv;int main(){ Mat in_frame, out_frame; Mat out_edge_l; Mat out_edge_s; Mat out_edge_m;Mat temp; const char win1[] = "Grabbing......", win2[] = "Recording......" ; const char win3[] = "Laplacian"; const char win4[] = "Sobel"; const char win5[] = "Morphology"; double fps = 30;//每秒的帧数 char file_out[] = "Recorded.avi" ; VideoCapture inVid(0);//打开默认摄像机 if (!inVid.isOpened())//检查错误 { cout << "发生错误,摄像机无法打开!" << endl; return -1; } //获取视频的宽度和高度 int width = (int )inVid.get( CAP_PROP_FRAME_WIDTH); int height = (int )inVid.get( CAP_PROP_FRAME_HEIGHT); VideoWriter recVid(file_out, VideoWriter ::fourcc( 'M', 'S', 'V', 'C'), fps, Size (width, height)); if (!recVid.isOpened()) { cout << "发生错误,视频文件无法打开!" << endl; return -1; } //为原始视频和最终视频创建两个窗口 namedWindow(win1); namedWindow(win2); while (true ) { //从摄像机读取帧(抓取并解码) inVid >> in_frame; //将帧转换为灰度 cvtColor(in_frame, out_frame, COLOR_BGR2GRAY ); Laplacian(in_frame, out_edge_l, -1); Sobel(in_frame, out_edge_s, -1, 1, 1); cvtColor(in_frame, temp, COLOR_BGR2GRAY ); morphologyEx(temp, out_edge_m, MORPH_GRADIENT , Mat ()); threshold(out_edge_m, out_edge_m, 40, 255, THRESH_BINARY ); //将帧写入视频文件(编码并保存) //recVid << out_frame; //recVid << out_edge; imshow(win1, in_frame); imshow(win2, out_frame); //在窗口中显示帧 imshow(win3, out_edge_l); imshow(win4, out_edge_s); imshow(win5, out_edge_m); if (waitKey(1000 / fps) >= 0) break ; } inVid.release(); //关闭摄像机 return 0;}
0 0
- 【OpenCV】笔记(11)——边缘检测
- opencv——边缘检测
- OpenCV学习笔记(6)——边缘检测
- OpenCV学习笔记——视频的边缘检测
- OpenCV学习笔记(6)——边缘检测
- opencv—学习笔记6,sobel边缘检测
- [Opencv入门笔记] 写图像—Canny边缘检测
- OpenCV入门笔记(四) 边缘检测
- OpenCV学习笔记(十一):边缘检测
- OpenCV学习笔记(十三)边缘检测
- opencv学习笔记(三) 边缘检测
- OpenCV学习笔记(3)—— canny算子边缘检测
- opencv笔记(4):边缘检测
- openCV—Python(11)——图像边缘检测
- openCV—Python(11)——图像边缘检测
- OpenCV学习——边缘检测
- opencv——Canny边缘检测
- opencv学习——canny边缘检测
- 复合索引的优点及注意事项
- 务自由 Eclipse中使用buildship管理Gradle项目
- 蓝桥杯 BASIC-10 基础练习 十进制转十六进制
- JavaScript DOM编程艺术中的几个方法
- 把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
- 【OpenCV】笔记(11)——边缘检测
- ionic项目之cordova自定义插件的配置
- spring security报错--java.lang.NoClassDefFoundError: org/springframework/core/SpringProperties
- 蓝桥杯 BASIC-11 基础练习 十六进制转十进制
- HDU 2564 词组缩写
- //点击屏幕任何地方让键盘消失
- Android 关于Unable to resolve host "...": No address associated服务器返回错误
- c#
- A1103. Integer Factorization