本文共 3227 字,大约阅读时间需要 10 分钟。
感谢 @浅墨_毛星云 的博客和书籍《OpenCV 3.0 编程入门》,我通过学习这些内容整理了本篇笔记及心得。由于自身水平有限,欢迎交流与指正。
Canny 算子是一种用于边缘检测的算法,通过计算图像的梯度来检测边缘。其核心思想是对图像进行滤波,提取强边缘,并通过双阈值分离背景噪声和真实边缘。
C++ 实现如下:
void Canny(InputArray image, OutputArray edges, double threshold1, double threshold2, int apertureSize=3, bool L2gradient=false)
false
。Sobel 算子是一种计算图像梯度的滤波器,通过对图像进行一阶微分,检测边缘的方向和强度。
C++ 实现如下:
void Sobel(InputArray src, OutputArray dst, int ddepth, int dx, int dy, int ksize=3, double scale=1, double delta=0, int borderType=BORDER_DEFAULT)
BORDER_DEFAULT
。Laplace 算子通过计算图像的二阶导数(拉普拉斯变换),用于边缘检测和图像增强。
C++ 实现如下:
void Laplacian(InputArray src, OutputArray dst, int ddepth, int ksize=1, double scale=1, double delta=0, int borderType=BORDER_DEFAULT)
BORDER_DEFAULT
。Laplacian 算子通过计算 Sobel 算子的 x 和 y 分量之和,得到图像的拉普拉斯变换结果,常用于边缘检测和图像去模糊。
Scharr 滤波器是一种用于计算图像梯度的滤波器,其原理类似于 Sobel 算子,但支持多个方向的梯度计算。
C++ 实现如下:
void Scharr(InputArray src, OutputArray dst, int ddepth, int dx, int dy, double scale=1, double delta=0, int borderType=BORDER_DEFAULT)
BORDER_DEFAULT
。#include#include #include #include using namespace std;using namespace cv;void process() { if (Way_num == 0) { // Canny 算子 Canny(Img_gray, Img_out, struct_size, struct_size * 5, 3); } else if (Way_num == 1) { // Sobel 算子 Mat grad_x, grad_y; Sobel(Img_gray, grad_x, CV_16U, 1, 0, 3, 1, 0, BORDER_DEFAULT); Sobel(Img_gray, grad_y, CV_16U, 0, 1, 3, 1, 0, BORDER_DEFAULT); addWeighted(grad_x, 0.5, grad_y, 0.5, 0, Img_out); } else if (Way_num == 2) { // Laplace 算子 Laplacian(Img_gray, Img_out, CV_16U, 3, 1, 0, BORDER_DEFAULT); convertScaleAbs(Img_out, Img_out); } else if (Way_num == 3) { // Scharr 滤波 Mat scharr_x, scharr_y; Scharr(Img_gray, scharr_x, CV_16U, 1, 0, 1, 0, BORDER_DEFAULT); Scharr(Img_gray, scharr_y, CV_16U, 0, 1, 1, 0, BORDER_DEFAULT); addWeighted(scharr_x, 0.5, scharr_y, 0.5, 0, Img_out); } imshow("【效果图】", Img_out);}
通过不同算子的组合,可以实现多种边缘检测效果。以下是对原图和处理后图像的对比:
通过这些算子的组合和参数调整,可以根据具体需求定制边缘检测效果。
转载地址:http://ugrfk.baihongyu.com/