imgproc模块--Laplace边缘检测算子

本文介绍如何利用OpenCV的Laplacian函数进行Laplace边缘检测。通过探讨算子的原理,即在图像一阶导数极值处二阶导数为0,来寻找图像边缘。代码部分展示了Laplacian算子的使用,并提到了由于其内部调用了Sobel算子,卷积核大小需为奇数。
摘要由CSDN通过智能技术生成

1.目的
(1)如何使用openCV函数Laplacian实现Laplacian算子的离散模拟

2.原理
在一阶导数的极值位置,二阶导数为0。所以我们也可以用这个特点来作为检测图像边缘的方法。
二阶导数

Laplacian算子的定义:
Laplacian算子

OpenCV函数 Laplacian 实现了Laplacian算子。 实际上,由于 Laplacian使用了图像梯度,它内部调用了 Sobel 算子。Laplacian算子卷积核大小必须是奇数

3.部分代码解释
(1)Laplacian

            /*
            Laplacian参数解释
            src:输入图像
            grad:输出图像
            ddepth:输出图像的深度
            kernel_size:卷积核大小(必须是奇数)
            scale:计算导数时的缩放因子:scale×导数
            delta:偏置,对计算结果的偏置
            BORDER_DEFAULT:默认边界设置
            */  
            Laplacian(src, grad, ddepth, 2*kernel_size+1, scale, delta, BORDER_DEFAULT);

4.完整代码
(1)CommonInclude.h

#include<iostream>
using namespace std;
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace cv;

(2)Edge.cpp

#include "CommonInclude.h"
int edge_type = 0;
int kernel_size = 3;
int scale = 1;
int max_edge_type = 3;
int max_kernel_size = 11;
int ddepth = CV_16S;
double delta = 0;
char windowNameOrigin[] = "Origin";
char windowNameEdge[] = "Edge";
/*
0:Sobel
1:Scharr
2:laplace
3:canny
*/
Mat src, grad;
Mat gradX, gradY;
Mat absGradX, absGradY;
void EdgeDetector(int, void*){
    switch(edge_type){
        case(0):
            //Sobel detector
            /*
            sobel参数解释
            src:输入图像
            ddepth:图像深度
            x_order:x方向梯度
            y_order:y方向梯度
            kernel_size:核大小(为奇数)
            scale:尺度,计算导数时的缩放因子
            delta:梯度偏置值,对计算结果的偏置
            BORDER_DEFAULT:默认边界设置
            */
            Sobel(src, gradX, ddepth, 1, 0, 2*kernel_size+1, scale, delta, BORDER_DEFAULT);
            Sobel(src, gradY, ddepth, 0, 1, 2*kernel_size+1, scale, delta, BORDER_DEFAULT);
            convertScaleAbs(gradX, absGradX);
            convertScaleAbs(gradY, absGradY);
            addWeighted(absGradX, 0.5, absGradY, 0.5, 0, grad);
            break;
        case(1):
            /*
            scharr参数解释
            src:输入图像
            ddepth:图像深度
            x_order:x方向梯度
            y_order:y方向梯度
            scale:尺度,计算导数时的缩放因子
            delta:梯度偏置值,对计算结果的偏置
            BORDER_DEFAULT:默认边界设置
            */
            //Scharr只能使用大小为3的卷积核
            Scharr(src, gradX, ddepth, 1, 0, scale, delta, BORDER_DEFAULT);
            Scharr(src, gradY, ddepth, 0, 1, scale, delta, BORDER_DEFAULT);
            convertScaleAbs(gradX, absGradX);
            convertScaleAbs(gradY, absGradY);
            addWeighted(absGradX, 0.5, absGradY, 0.5, 0, grad);
            break;
        case(2):
            /*
            Laplacian参数解释
            src:输入图像
            grad:输出图像
            ddepth:输出图像的深度
            kernel_size:卷积核大小(必须是奇数)
            scale:计算导数时的缩放因子:scale×导数
            delta:偏置,对计算结果的偏置
            BORDER_DEFAULT:默认边界设置
            */  
            Laplacian(src, grad, ddepth, 2*kernel_size+1, scale, delta, BORDER_DEFAULT);
            convertScaleAbs(grad, grad);
            break;
        case(3):
            //待续
            break;
        default:
            cout << "error type!!!" << endl;
            break;
    }
    imshow(windowNameEdge, grad);
}

int main(int argc, char** argv){
    if(argc<2){
        cout << "more parameters are required!!!" << endl;
        return(-1);
    }
    src = imread(argv[1]);
    if(!src.data){
        cout << "erro to read image!!!" << endl;
        return(-1);
    }
    namedWindow(windowNameEdge, CV_WINDOW_AUTOSIZE);
    //高斯处理
    GaussianBlur(src, src, Size(3,3), 0, 0, BORDER_DEFAULT);
    //转化为灰度图像
    cvtColor(src, src, CV_BGR2GRAY);
    imshow(windowNameOrigin, src);
    createTrackbar("Edge Type:\n0 Sobel\n1 Scharr\n2 laplace \n3 Canny", windowNameEdge,
            &edge_type, max_edge_type,
            EdgeDetector);
    createTrackbar("Kernel Size:2*n+1", windowNameEdge,
            &kernel_size, max_kernel_size,
            EdgeDetector);
    EdgeDetector(0,0);
    waitKey(0);
}

参考文献
1.http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/imgtrans/laplace_operator/laplace_operator.html#laplace-operator

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值