- 操作系统:ubuntu22.04
- OpenCV版本:OpenCV4.9
- IDE:Visual Studio Code
- 编程语言:C++11
算法描述
cv::cuda::createLaplacianFilter 是 OpenCV CUDA 模块中的一个函数,用于创建一个 拉普拉斯(Laplacian)滤波器。该滤波器常用于图像边缘检测。
函数原型
Ptr<Filter> cv::cuda::createLaplacianFilter
(
int srcType,
int dstType,
int ksize = 1,
double scale = 1,
int borderMode = BORDER_DEFAULT,
Scalar borderVal = Scalar::all(0)
)
参数
参数名 | 描述 |
---|---|
srcType | 输入图像类型。支持 CV_8U、CV_16U 和 CV_32F 的单通道和四通道图像。 |
dstType | 输出图像类型。目前仅支持与输入图像相同的类型。 |
ksize | 用于计算二阶导数滤波器的孔径大小(参见 getDerivKernels)。必须为正奇数。目前只支持 ksize = 1 和 ksize = 3。 |
scale | 可选的比例因子,用于对计算得到的 Laplacian 值进行缩放。默认情况下不进行缩放(参见 getDerivKernels)。 |
borderMode | 像素外推方法。详细信息请参见 borderInterpolate。 |
borderVal | 默认的边界值。 |
代码示例
#include <opencv2/cudafilters.hpp>
#include <opencv2/cudaimgproc.hpp>
#include <opencv2/opencv.hpp>
int main()
{
// 读取图像并转换为灰度图
cv::Mat h_input = cv::imread( "/media/dingxin/data/study/OpenCV/sources/images/Lenna.png", cv::IMREAD_GRAYSCALE );
cv::Mat h_output;
// 将图像上传到 GPU
cv::cuda::GpuMat d_input, d_output;
d_input.upload( h_input );
// 创建 Laplacian 滤波器
cv::Ptr< cv::cuda::Filter > laplacianFilter = cv::cuda::createLaplacianFilter( d_input.type(), d_input.type(), 3 // 使用 3x3 的核
);
// 应用滤波器
laplacianFilter->apply( d_input, d_output );
// 下载结果回 CPU
d_output.download( h_output );
// 显示或保存结果
cv::imshow( "Laplacian Output", h_output );
cv::waitKey();
return 0;
}