功能:
cvLaplace 是计算图像的 Laplacian 变换 ,是Intel开源项目opencv中的函数,要使用它应该在程序中包含#include <cv.h>
函数形式:
void cvLaplace( const CvArr* src, CvArr* dst, int aperture_size=3 );
参数列表:
Src 输入图像.
Dst 输出图像.
aperture_size算子内核大小(滤波计算矩阵的大小默认为3)可以是1、3、5、7
注释:源图像src既可以是8位(无符号)图像,也可以是32位(浮点)图像
目标图像src必须是16位(有符号)或者32位(浮点)图像
对程序源代码写一个流程,如
第一步:灰度化
第二步:高斯滤波
第三步:Laplace边缘检测
第四步:显示结果
程序源代码(.cpp):
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <ctype.h>
#include <stdio.h>
#include <iostream>
using namespace cv;
using namespace std;
static void help()
{
cout <<
"\nThis program demonstrates Laplace point/edge detection using OpenCV function Laplacian()\n"
"It captures from the camera of your choice: 0, 1, ... default 0\n"
"Call:\n"
"./laplace [camera #, default 0]\n" << endl;
}
//声明需要使用的变量
int sigma = 3;
int smoothType = CV_GAUSSIAN;
int main( int argc, char** argv )
{
//声明
VideoCapture cap;
help();
//从命令行参数选择打开摄像头,默认为第0个
if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
cap.open(argc == 2 ? argv[1][0] - '0' : 0);
else if( argc >= 2 )
{
cap.open(argv[1]);
if( cap.isOpened() )
cout << "Video " << argv[1] <<
": width=" << cap.get(CV_CAP_PROP_FRAME_WIDTH) <<
", height=" << cap.get(CV_CAP_PROP_FRAME_HEIGHT) <<
", nframes=" << cap.get(CV_CAP_PROP_FRAME_COUNT) << endl;
//判断摄像头是否可以使用
if( argc > 2 && isdigit(argv[2][0]) )
{
int pos;
sscanf(argv[2], "%d", &pos);
cout << "seeking to frame #" << pos << endl;
cap.set(CV_CAP_PROP_POS_FRAMES, pos);
}
}
if( !cap.isOpened() )
{
cout << "Could not initialize capturing...\n";
return -1;
}
//创建预览窗口,并创建滑动条人工交互设置滤波尺寸
namedWindow( "Laplacian", 0 );
createTrackbar( "Sigma", "Laplacian", &sigma, 15, 0 );
//声明图像
Mat smoothed, laplace, result;
//通过循环反复获取摄像头预览图像进行处理
for(;;)
{
//从摄像头获取一帧图像进行处理
Mat frame;
cap >> frame;
if( frame.empty() )
break;
int ksize = (sigma*5)|1;
//根据参数对图像进行去噪
if(smoothType == CV_GAUSSIAN)
GaussianBlur(frame, smoothed, Size(ksize, ksize), sigma, sigma);
else if(smoothType == CV_BLUR)
blur(frame, smoothed, Size(ksize, ksize));
else
medianBlur(frame, smoothed, ksize);
//调用Laplacian,进行边缘检测
Laplacian(smoothed, laplace, CV_16S, 5);
//将laplace结果*(sigma+1)*0.25
convertScaleAbs(laplace, result, (sigma+1)*0.25);
//显示结果
imshow("Laplacian", result);
//循环反复获取摄像头预览图像进行处理
int c = waitKey(30);
if( c == ' ' )
smoothType = smoothType == CV_GAUSSIAN ? CV_BLUR : smoothType == CV_BLUR ? CV_MEDIAN : CV_GAUSSIAN;
if( c == 'q' || c == 'Q' || (c & 255) == 27 )
break;
}
return 0;
}
程序应用示例:
#include <highgui.h>
#include <cv.h>
int main(int argc, char ** argv)
{
IplImage* src, *dst;
src = cvLoadImage("1.jpg");
dst = cvCreateImage( cvGetSize( src ), IPL_DEPTH_16S, 3 );
cvNamedWindow( "src", 0 );
cvShowImage( "src", src );
cvNamedWindow( "Laplace", 0 );
cvLaplace( src, dst, 3); //拉普拉斯变换,边缘检测
cvShowImage( "Laplace", dst );
cvWaitKey(0);
cvReleaseImage( &src );
cvReleaseImage( &dst );
return 0;
}