【OpenCV】笔记(11)——边缘检测

边缘检测

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值