OpenCV3.0 Examples学习笔记(14)-laplace.cpp-LaplaLacian实现对摄像头预览图边缘检测

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012566751/article/details/54602907
这个系列的目的是通过对OpenCV示例,进一步了解OpenCV函数的使用,不涉及具体原理。

目录
简介
Example运行截图
Example分析
Example代码

简介
本文记录了对OpenCV示例 laplace .cpp 的分析。
资料地址:http://docs.opencv.org/3.0.0/de/db2/laplace_8cpp-example.html

这个示例主要演示了如何使用 laplace 对摄像头图像进行处理。

示例涉及到 VideoCapture类, GaussianBlur, blur, medianBlur, Laplacian, convertScaleAbs 函数使用

VideoCapture类说明

用于从视频文件、 图像序列或摄像机获取视频的类。

示例涉及函数:
编号
函数
说明
1
bool open  ( int  device )
打开指定编号的摄像头,open函数是重载函数,还支持打开视频文件和图像序列。
2
bool isOpened  ( ) const
返回 VideoCapture是否已经成功初始化。
3
>>
重载操作符,从打开的摄像头,视频文件或者图像序列中获取一帧图像。
PS: VideoCapture功能非常强大,如果有兴趣可以查看参考资料 1.《 cv::VideoCapture Class Reference
PS2:VideoCapture除了能打开摄像头以外,还能打开视频文件,甚至图片序列。使用构造函数或者open函数时传入路径,如 img_%02d.jpg, VideoCapture会按照顺序加载如img_00.jpg, img_01.jpg, img_02.jpg, ...,这个功能很强大,特别是有很多样本需要测试时。


GaussianBlur,blur,medianBlur三大低通滤波

高斯滤波,均值滤波,中值滤波 这个三个低通滤波函数,毫不夸张的说,都是OpenCV最常用的函数,接触过OpenCV的用户基本都使用过。作为低通滤波器,常常用于平滑和去噪操作。

函数调用都非常简单,可以查看参考资料 3. OpenCV 学习(几种基本的低通滤波)

Laplacian
LaplaLacian( )函数其实主要是利用sobel算子的运算。它通过加上sobel算子运算出的图像x方向和y方向上的导数,来得到我们载入图像的拉普拉斯变换结果。
根据图像处理的原理我们知道,二阶导数可以用来进行检测边缘 ,因此 LaplaLacian函数一般用于边缘检测。

函数原型:
void Laplacian( InputArray src, OutputArray dst, int ddepth,
                             int ksize = 1, double scale = 1, double delta = 0,
                             int borderType = BORDER_DEFAULT );

参数说明:
image: 输入图像,即源图像,填Mat类的对象即可,且需为单通道8位图像。
edges: 输出的边缘图,需要和源图片有一样的尺寸和通道数。
ddept: 目标图像的深度。
ksize: 用于计算二阶导数的滤波器的孔径尺寸,大小必须为正奇数,且有默认值1。
scale: 计算拉普拉斯值的时候可选的比例因子, 有默认值1。
delta: 表示在结果存入目标图(第二个参数dst)之前可选的delta值, 有默认值0
borderType: 边界模式,默认值为 BORDER_DEFAULT。这个参数可以在官方文档中borderInterpolate()处得到更详细的信息。


convertScaleAbs
使用线性变换转换输入数组元素成8位无符号整型。原理如下:
B = abs(A* alpha beta)

函数原型:
void convertScaleAbs(InputArray src, OutputArray dst,
                                  double alpha = 1, double beta = 0);

参数说明:
src : 原数组
dst :输出数组 (深度为 8u).
alpha :比例因子.
beta :原数组元素按比例缩放后添加的值。

PS:看以看作 cvConvertScale;和cvAbs 的联合使用。

Example运行截图
参数
截图
sigma = 3

Example分析
1.声明需要使用的变量
enum {GAUSSIAN, BLUR, MEDIAN};

int sigma = 3;
int smoothType = GAUSSIAN;

注意:
(1)
sigma, Laplacian滤波尺寸,ksize = sigma*5;
(2) smoothType ,滤波类型

2.声明
VideoCapture 对象,用于开启摄像头
VideoCapture cap;

3.从命令行参数选择打开摄像头,默认为第0个
if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
        cap.open(argc == 2 ? argv[1][0] - '0' : 0);
    else if( argc >= 2 )
    {
        cap.open(argv[1]);
        if( cap.isOpened() )
            cout << "Video " << argv[1] <<
                ": width=" << cap.get(CAP_PROP_FRAME_WIDTH) <<
                ", height=" << cap.get(CAP_PROP_FRAME_HEIGHT) <<
                ", nframes=" << cap.get(CAP_PROP_FRAME_COUNT) << endl;
        if( argc > 2 && isdigit(argv[2][0]) )
        {
            int pos;
            sscanf(argv[2], "%d", &pos);
            cout << "seeking to frame #" << pos << endl;
            cap.set(CAP_PROP_POS_FRAMES, pos);
        }
    }

4.判断摄像头是否可以使用
if( !cap.isOpened() )
    {
        cout << "Could not initialize capturing...\n";
        return -1;
    }

5.创建预览窗口,并创建滑动条人工交互设置滤波尺寸
namedWindow( "Laplacian", 0 );
    createTrackbar( "Sigma", "Laplacian", &sigma, 15, 0 );

6.声明图像
Mat smoothed, laplace, result;

7.通过循环反复获取摄像头预览图像进行处理
for(;;)
{
    ...
    
int c = waitKey(30);
        if( c == ' ' )
            smoothType = smoothType == GAUSSIAN ? BLUR : smoothType == BLUR ? MEDIAN : GAUSSIAN;
        if( c == 'q' || c == 'Q' || (c & 255) == 27 )
            break;
}

8.具体分析循环中图像处理过程
8.1.从摄像头获取一帧图像进行处理
Mat frame;
cap >> frame;
if( frame.empty() )
     break;

8.2.计算Laplacian滤波尺寸
int ksize = (sigma*5)|1;

8.3.根据参数对图像进行去噪
if(smoothType == GAUSSIAN)
            GaussianBlur(frame, smoothed, Size(ksize, ksize), sigma, sigma);
else if(smoothType == BLUR)
            blur(frame, smoothed, Size(ksize, ksize));
 else
            medianBlur(frame, smoothed, ksize);

8.4.调用Laplacian,进行边缘检测
Laplacian(smoothed, laplace, CV_16S, 5);

8.5.将 laplace结果*(sigma+1)*0.25
convertScaleAbs(laplace, result, (sigma+1)*0.25);

8.6显示结果
imshow("Laplacian", result);

Example代码
#include "opencv2/videoio/videoio.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <ctype.h>
#include <stdio.h>
#include <iostream>

using namespace cv;
using namespace std;

static void help()
{
    cout <<
            "\nThis program demonstrates Laplace point/edge detection using OpenCV function Laplacian()\n"
            "It captures from the camera of your choice: 0, 1, ... default 0\n"
            "Call:\n"
            "./laplace [camera #, default 0]\n" << endl;
}

enum {GAUSSIAN, BLUR, MEDIAN};

int sigma = 3;
int smoothType = GAUSSIAN;

int main( int argc, char** argv )
{
    VideoCapture cap;
    help();

    if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
        cap.open(argc == 2 ? argv[1][0] - '0' : 0);
    else if( argc >= 2 )
    {
        cap.open(argv[1]);
        if( cap.isOpened() )
            cout << "Video " << argv[1] <<
                ": width=" << cap.get(CAP_PROP_FRAME_WIDTH) <<
                ", height=" << cap.get(CAP_PROP_FRAME_HEIGHT) <<
                ", nframes=" << cap.get(CAP_PROP_FRAME_COUNT) << endl;
        if( argc > 2 && isdigit(argv[2][0]) )
        {
            int pos;
            sscanf(argv[2], "%d", &pos);
            cout << "seeking to frame #" << pos << endl;
            cap.set(CAP_PROP_POS_FRAMES, pos);
        }
    }

    if( !cap.isOpened() )
    {
        cout << "Could not initialize capturing...\n";
        return -1;
    }

    namedWindow( "Laplacian", 0 );
    createTrackbar( "Sigma", "Laplacian", &sigma, 15, 0 );

    Mat smoothed, laplace, result;

    for(;;)
    {
        Mat frame;
        cap >> frame;
        if( frame.empty() )
            break;

        int ksize = (sigma*5)|1;
        if(smoothType == GAUSSIAN)
            GaussianBlur(frame, smoothed, Size(ksize, ksize), sigma, sigma);
        else if(smoothType == BLUR)
            blur(frame, smoothed, Size(ksize, ksize));
        else
            medianBlur(frame, smoothed, ksize);

        Laplacian(smoothed, laplace, CV_16S, 5);
        convertScaleAbs(laplace, result, (sigma+1)*0.25);
        imshow("Laplacian", result);

        int c = waitKey(30);
        if( c == ' ' )
            smoothType = smoothType == GAUSSIAN ? BLUR : smoothType == BLUR ? MEDIAN : GAUSSIAN;
        if( c == 'q' || c == 'Q' || (c & 255) == 27 )
            break;
    }

    return 0;
}




参考资料:

猜你喜欢

转载自blog.csdn.net/u012566751/article/details/54602907