opencv学习小结(9) – 利用拉普拉斯算子进行图像锐化

•利用3*3的Laplacian算子图象锐化:

•  Laplace函数实现的方法是先用Sobel 算子计算二阶x和y导数,再求和:

其基本函数模型如下:

Mat getLaplacian(Mat &Image)
{
  Mat Scr_Gray,showImage;
  int kernel_size = 3;
  int scale = 1;
  int delta = 0;
  int ddepth = CV_16S;
  // 使用高斯滤波消除噪声
  GaussianBlur(Image, Image, Size(3, 3), 0, 0, BORDER_DEFAULT);
 
  // 转换为灰度图
  cvtColor(Image, Scr_Gray, CV_RGB2GRAY);
 
  // 使用Laplace函数
  Mat abs_dst;
  Laplacian(Scr_Gray, showImage, ddepth, kernel_size, scale, delta, BORDER_DEFAULT);
  convertScaleAbs(showImage, abs_dst);
  return abs_dst;
}
 
int main()
{
        Mat srcImage_2 = imread("g14.tif");
        namedWindow("【原始图均鞭】", 1);
  imshow("【原始图均鞭】", srcImage_2);
   	/*********************对图像进行Laplacian算子***************************/
   Mat showImage;
   showImage = getLaplacian(srcImage_2);
   imshow("Laplacian【效果图均鞭均鞭均鞭】", showImage);
}

最后的应用例程如下所示:

int main()
{
  Mat srcImage = imread("1.jpg");
  Mat srcImage_1 = imread("g11.tif");
  Mat srcImage_2 = imread("g14.tif");
  if (!srcImage.data)
  {
    cout << "fail to load the image" << endl;
    return -1;
  }
  if (!srcImage_1.data)
  {
    cout << "fail to load the image_1" << endl;
    return -2;
  }	
 
  if (!srcImage_2.data)
  {
    cout << "fail to load the image_2" << endl;
    return -3;
  }
 
  //namedWindow("【原始图】", 1);
  //imshow("【原始图】", srcImage);
  /****************对图像加椒盐噪声,并进行中值滤波******************/
  //salt_noise(srcImage, 4000);
  //pepper_noise(srcImage, 4000);
  //imshow("【噪声图】", srcImage);
  //Mat Medical_showImage, Medical_showImage_1;
  //MedianFlitering(srcImage, Medical_showImage);
  //medianBlur(srcImage, Medical_showImage_1, 3);
  //imshow("自定义中值滤波处理后", Medical_showImage);
  //imshow("openCV自带的中值滤波", Medical_showImage_1);
  /*******************************************************************/
 
  /*********************对图像添加高斯噪声并进行高斯滤波**************/
   //Mat GaussianshowImage,GaussianshowImage_1;
   //GaussianshowImage_1 = addGaussianNoise(srcImage);
   //imshow("高斯噪声【效果图】", GaussianshowImage_1);
   //GaussianBlur(GaussianshowImage_1, GaussianshowImage, Size(3, 3), 1);
   //imshow("高斯滤波【效果图】", GaussianshowImage);
  /*******************************************************************/
 
  /*********************对图像进行椒盐化并进行均值滤波****************/
  //Mat image1(srcImage.size(), srcImage.type());
  //Mat image2;
  //salt_noise(srcImage, 4000);
  //pepper_noise(srcImage, 4000);
  //imshow("椒盐图【效果图】", srcImage);
  //AverFiltering(srcImage, image1);
  //blur(srcImage, image2, Size(3, 3));//openCV库自带的均值滤波函数
  //imshow("自定义均值滤波", image1);
  //imshow("openCV自带的均值滤波", image2);
  /*******************************************************************/
 
  /*********************对图像进行Sobel算子***************************/
  //Mat showImage, showImage_1;
  //showImage=getSobel(srcImage_2);
  //imshow("Sobel算子【效果图】", showImage);
  /*******************************************************************/
 
  /*********************对图像进行Scharr算子***************************/
  //Mat showImage;
  //showImage = getScharr(srcImage_2);
  //imshow("高通滤波【效果图】", showImage);
  /*******************************************************************/
 
  /*********************对图像进行Laplacian算子***************************/
  /* Mat showImage;
   showImage = getLaplacian(srcImage_2);
   imshow("Laplacian【效果图】", showImage);*/
   /*******************************************************************/
 
 
        //Mat showImage = getHistogramImage(srcImage);        //得到相应图片的直方图 小狮子
  //Mat showImage = getHistogram_Equalization(srcImage);//得到相应图片的直方图的均衡图 小狮子

  //imshow("【直方图】", showImage);
 
  //getHistogram_Stetch(srcImage);                     //得到直方图拉伸之后的图像
 
/**********************测试代码*****************/
  //Mat element = getStructuringElement(MORPH_RECT,Size(15,15));
  //Mat dstImage;
  //erode(srcImage, dstImage, element);
  //imshow("腐蚀操作【效果图】", dstImage);
 
  //blur(srcImage, dstImage,Size(7,7));
  //imshow("均值滤波【效果图】", dstImage);
 
  //Mat edge, grayImage;
  //cvtColor(srcImage, grayImage, CV_BGR2GRAY);
  //blur(grayImage, edge, Size(3, 3));
  //Canny(edge, edge, 3, 9, 3);
  //imshow("边缘检测【效果图】", edge);
/**********************************************/
  waitKey(0);
  return 0;
}

部分函数被注释了,想使用的话去掉注释即可

发表评论

电子邮件地址不会被公开。 必填项已用*标注

16 − 16 =