cv::cuda::copyMakeBorder 是 OpenCV 的 CUDA 模块中的一个函数,用于在 GPU 上为图像矩阵添加边框(padding)。这个函数类似于 OpenCV 的标准 cv::copyMakeBorder 函数,但它是针对 GPU 加速设计的。
通过 cv::cuda::copyMakeBorder,你可以在图像周围添加额外的像素行和列,以扩展图像的大小。
函数原型:
void cv::cuda::copyMakeBorder(InputArray src, OutputArray dst, int top, int bottom, int left, int right, int borderType, const Scalar& value = Scalar(), Stream& stream = Stream::Null());
src:输入图像,类型为 cv::cuda::GpuMat。
dst:输出图像,类型为 cv::cuda::GpuMat,包含了添加了边框的输入图像。
top、bottom、left、right:分别指定在图像顶部、底部、左侧和右侧要添加的边框像素数量。
borderType:边框类型,可以是 cv::BORDER_CONSTANT、cv::BORDER_REPLICATE、cv::BORDER_REFLECT 或 cv::BORDER_WRAP 等之一。
value:当 borderType 设置为 cv::BORDER_CONSTANT 时,用于填充边框的常数值。
stream:CUDA 流,用于异步执行 CUDA 调用。
使用 cv::cuda::copyMakeBorder 可以方便地在 GPU 上对图像进行边框扩展,这在许多图像处理任务中都是必要的步骤,比如卷积操作、滤波等。
示例:
#include <opencv2/opencv.hpp>
#include <opencv2/cudaimgproc.hpp>
int main()
{
// 初始化 CUDA
cv::cuda::setDevice(0); // 设置要使用的 GPU 设备
// 读取图像到 GpuMat
cv::Mat hostImage = cv::imread("path_to_image.jpg", cv::IMREAD_GRAYSCALE);
cv::cuda::GpuMat gpuImage(hostImage);
// 定义边框的大小
int top = 10, bottom = 10, left = 10, right = 10;
// 定义输出 GpuMat
cv::cuda::GpuMat gpuResult;
// 在 GPU 上添加边框
cv::cuda::copyMakeBorder(gpuImage, gpuResult, top, bottom, left, right, cv::BORDER_CONSTANT, cv::Scalar::all(0));
// (可选)将结果从 GPU 拷贝回 CPU
cv::Mat result(gpuResult);
// ... 处理或显示 result ...
return 0;
}
在这个示例中,我们首先读取了一张灰度图像到 GpuMat,然后使用 cv::cuda::copyMakeBorder 在图像周围添加了指定大小的常数边框(值为 0)。最后,我们将结果从 GPU 拷贝回 CPU(如果需要的话)。