zoukankan      html  css  js  c++  java
  • 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;
    }

     

  • 相关阅读:
    Samba 4.0 RC3 发布
    SymmetricDS 3.1.7 发布,数据同步和复制
    Express.js 3.0 发布,Node.js 的高性能封装
    GIFLIB 5.0.1 发布,C语言的GIF处理库
    jQuery UI 1.9.1 发布
    SVN Access Manager 0.5.5.14 发布 SVN 管理工具
    DynamicReports 3.0.3 发布 Java 报表工具
    HttpComponents HttpClient 4.2.2 GA 发布
    AppCan 2.0 正式发布,推移动应用云服务
    Ruby 2.0 的新功能已经冻结
  • 原文地址:https://www.cnblogs.com/herd/p/9735042.html
Copyright © 2011-2022 走看看