码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-10-01 17:53:51      阅读:326      评论:0      收藏:0      [点我收藏+]

标签:info   clu   ++   load   enc   float   bubuko   nes   mat   

#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算子 拉普拉斯算子 自定义卷积核——实现渐进模糊

标签:info   clu   ++   load   enc   float   bubuko   nes   mat   

原文地址:https://www.cnblogs.com/herd/p/9735042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!