Laplacian边缘检测是一种常见的图像处理技术,可以用于检测图像中的边缘。下面是使用OpenCV库在C++中实现Laplacian边缘检测的示例代码:
int main()
{
Mat img, gray, laplacianImg;
img = imread("image.jpg");
if (img.empty())
{
cout << "Could not read the image" << endl;
return -1;
}
cvtColor(img, gray, COLOR_BGR2GRAY); // 将图像转换为灰度图像
Laplacian(gray, laplacianImg, CV_16S, 3, 1, 0, BORDER_DEFAULT); // 执行Laplacian边缘检测
convertScaleAbs(laplacianImg, laplacianImg); // 将结果转换为8位无符号整数
namedWindow("Original Image", WINDOW_NORMAL);
namedWindow("Laplacian Edge Detection", WINDOW_NORMAL);
imshow("Original Image", img);
imshow("Laplacian Edge Detection", laplacianImg);
waitKey(0);
return 0;
}
在这个示例中,我们首先读入一张图像,然后将其转换为灰度图像。接着,我们使用OpenCV的Laplacian函数对灰度图像进行边缘检测。最后,我们将检测到的边缘结果转换为8位无符号整数,并将其显示在屏幕上。