Opencv3 Robert算子 Sobel算子 拉普拉斯算子 自定义卷积核——实现渐进模糊

Posted herd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Opencv3 Robert算子 Sobel算子 拉普拉斯算子 自定义卷积核——实现渐进模糊相关的知识,希望对你有一定的参考价值。

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

//Robert算子
int Demo_Robert()
{
  char win1[] = "window1";
  char win2[] = "window2";
  char win3[] = "window3";

  Mat img1, img2, img3, kernel_x, kernel_y;
  img1 = imread("D://images//box//0019-00.jpg");
  if (img1.empty())
  {
    cout << "could not load image..."<< endl;
    return 0;
  }
  imshow(win1,img1);

  //X方向—Robert算子
  kernel_x = (Mat_<int>(2,2)<<1,0,0,-1);
  filter2D(img1,img2,-1,kernel_x,Point(-1,-1),0,0);
  //Y方向—Robert算子
  kernel_y = (Mat_<int>(2, 2) << 0, 1, -1, 0);
  filter2D(img1, img3, -1, kernel_y, Point(-1, -1), 0, 0);

  imshow(win2, img2);
  imshow(win3, img3);
  return 0;
}

//Sobel算子
int Demo_Sobel()
{
  char win1[] = "window1";
  char win2[] = "window2";
  char win3[] = "window3";

  Mat img1, img2, img3, kernel_x, kernel_y;
  img1 = imread("D://images//box//0019-00.jpg");
  if (img1.empty())
  {
    cout << "could not load image..." << endl;
    return 0;
  }
  imshow(win1, img1);

  //X方向—Sobel算子
  kernel_x = (Mat_<int>(3, 3) << -1,0,1,-2,0,2,-1,0,1);
  filter2D(img1, img2, -1, kernel_x, Point(-1, -1), 0, 0);
  //Y方向—Sobel算子
  kernel_y = (Mat_<int>(3, 3) << -1,-2,-1,0,0,0,1,2,1);
  filter2D(img1, img3, -1, kernel_y, Point(-1, -1), 0, 0);

  imshow(win2, img2);
  imshow(win3, img3);
  return 0;

}

//拉普拉斯算子
int Demo_Laplace()
{
  char win1[] = "window1";
  char win2[] = "window2";
  char win3[] = "window3";

  Mat img1, img2, img3, kernel_x, kernel_y;
  img1 = imread("D://images//box//0019-00.jpg");
  if (img1.empty())
  {
    cout << "could not load image..." << endl;
    return 0;
  }
  imshow(win1, img1);

  //Laplace算子
  kernel_x = (Mat_<int>(3, 3) << 0, -1, 0, -1, 4, -1, 0, -1, 0);
  filter2D(img1, img2, -1, kernel_x, Point(-1, -1), 0, 0);
  
  imshow(win2, img2);
  return 0;
}

//自定义卷积核——实现渐进模糊
int Demo_Kernel()
{
  char win1[] = "window1";
  char win2[] = "window2";
  char win3[] = "window3";

  Mat img1, img2, img3, kernel_x, kernel_y;
  img1 = imread("D://images//box//0019-00.jpg");
  if (img1.empty())
  {
    cout << "could not load image..." << endl;
    return 0;
  }
  imshow(win1, img1);

  int c = 0;
  int index = 0;
  int ksize = 3;
  while (true)
  {
    c = waitKey(600);
    if ((char)c==27)
    {
      break;
    }
    ksize = 4 + (index % 5) * 2 + 1;
    Mat kernel1 = Mat::ones(Size(ksize,ksize),CV_32F)/(float)(ksize*ksize);
    filter2D(img1,img2,-1,kernel1,Point(-1,-1));
    index++;
    imshow(win2,img2);
  }
  

  imshow(win2, img2);
  return 0;
}

int main()
{
  //Demo_Robert();
  //Demo_Sobel();
  //Demo_Laplace();
  Demo_Kernel();

  waitKey(0);
  return 0;
}

 技术分享图片

技术分享图片

 

































































































以上是关于Opencv3 Robert算子 Sobel算子 拉普拉斯算子 自定义卷积核——实现渐进模糊的主要内容,如果未能解决你的问题,请参考以下文章

实现Sobel算子滤波Robers算子滤波Laplace算子滤波

Sobel算子的算子描述

sobel 算子和 canndy 算子的区别

sobel算子里的阈值是怎么设的

OpenCV中不用库函数实现sobel算子

OpenCV 边缘检测之Sobel算子