opencv入門三函數說明

函數說明

  • medianBlur bilateralFilter createTrackbar

medianBlur

  • 官方說明:通過周圍數據中值來進行平滑,優點效果好,就是慢就是慢

    The function smoothes an image using the median filter with the

  • 參數說明

    @param src input 1-, 3-, or 4-channel image; when ksize is 3 or 5, the image depth should be
    CV_8U, CV_16U, or CV_32F, for larger aperture sizes, it can only be CV_8U.
    @param dst destination array of the same size and type as src.
    //內核大小隻要奇數大於1
    @param ksize aperture linear size; it must be odd and greater than 1, for example: 3, 5, 7 …
    @sa bilateralFilter, blur, boxFilter, GaussianBlur

  • 示例
//中值濾波
 int medianBlurimg(const string & filename)
 {
     Mat srcimg = imread(filename);
     Mat dst;
     medianBlur(srcimg, dst, 10 * 2 + 1);
     imshow("medianBlurimg", dst);
     //waitKey(0);
     return 0;

 }

bilateralFilter


  • 官方說明:去燥並保證邊緣完整

The function applies bilateral filtering to the input image, as described in
http://www.dai.ed.ac.uk/CVonline/LOCAL_COPIES/MANDUCHI1/Bilateral_Filtering.html
bilateralFilter can reduce unwanted noise very well while keeping edges fairly sharp. However, it is
very slow compared to most filters.
  • 參數說明
  • @param src Source 8-bit or floating-point, 1-channel or 3-channel image.
    @param dst Destination image of the same size and type as src .
    //象素直徑如果不是正數它會從sigmaspace算出來
    @param d Diameter of each pixel neighborhood that is used during filtering. If it is non-positive,
    it is computed from sigmaSpace.
    //顏色濾波數越大表示邊緣越多的顏色 被混合到一起
    @param sigmaColor Filter sigma in the color space. A larger value of the parameter means that
    farther colors within the pixel neighborhood (see sigmaSpace) will be mixed together, resulting
    in larger areas of semi-equal color.
    //空間參數,數越大越大的區域顏色相同第三個參數大於0鄰域大小與此參數無關,否則正此參數成正比正相關
    @param sigmaSpace Filter sigma in the coordinate space. A larger value of the parameter means that
    farther pixels will influence each other as long as their colors are close enough (see sigmaColor
    ). When d>0, it specifies the neighborhood size regardless of sigmaSpace. Otherwise, d is
    proportional to sigmaSpace.
    @param borderType border mode used to extrapolate pixels outside of the image, see cv::BorderTypes
  • 示例代碼
  •  Mat srcimg = imread(filename);
         // Mat dst;
          g_srcImage = srcimg.clone();
         // bilateralFilter(srcimg, dst, 25,25*2,25/2);
         // imshow("bilateralFilterimg",dst);
          namedWindow("bilateralFilterimg", 1);

    createTrackbar


    • 官方說明:就是一個slider來改變相關數值大小

    The function createTrackbar creates a trackbar (a slider or range control) with the specified name
    and range, assigns a variable value to be a position synchronized with the trackbar and specifies
    the callback function onChange to be called on the trackbar position change. The created trackbar is
    displayed in the specified window winname.
  • 參數說明
    //軌跡名
    @param trackbarname Name of the created trackbar.
    //窗口名
    @param winname Name of the window that will be used as a parent of the created trackbar.
    //滑塊初始位置指針類型
    @param value Optional pointer to an integer variable whose value reflects the position of the
    slider. Upon creation, the slider position is defined by this variable.
    //滑塊最大值 ,最小值始終爲0
    @param count Maximal position of the slider. The minimal position is always 0.
    //更改回調函數
    @param onChange Pointer to the function to be called every time the slider changes position. This
    function should be prototyped as void Foo(int,void*); , where the first parameter is the trackbar
    position and the second parameter is the user data (see the next parameter). If the callback is
    the NULL pointer, no callbacks are called, but only value is updated.
    //傳給用戶數據 全局變量可以不用考慮
    @param userdata User data that is passed as is to the callback. It can be used to handle trackbar
    events without using global variables.
  • 示例代碼
  • //雙邊濾波
      int bilateralFilterimg(const string & filename)
     {
          Mat srcimg = imread(filename);
         // Mat dst;
          g_srcImage = srcimg.clone();
         // bilateralFilter(srcimg, dst, 25,25*2,25/2);
         // imshow("bilateralFilterimg",dst);
          namedWindow("bilateralFilterimg", 1);
          //創建軌跡條  
          createTrackbar("參數值:", "bilateralFilterimg", &g_nMedianBlurValue, 50, on_MedianBlur);
          on_MedianBlur(g_nMedianBlurValue, 0);
         /* Mat image1(srcimg.rows, srcimg.cols, srcimg.type(), Scalar(180, 120, 50));
          for (int x = 0; x < srcimg.cols; x++)
          {
              for (int y = 0; y < srcimg.rows; y++)
              {
    
                      image1.at<Vec3b>(Point(x, y))[0] = 0;// srcimg.at<Vec3b>(Point(x, y))[0];
                      image1.at<Vec3b>(Point(x, y))[1] =  srcimg.at<Vec3b>(Point(x, y))[1];
                      image1.at<Vec3b>(Point(x, y))[2] = 0;// srcimg.at<Vec3b>(Point(x, y))[2];
    
              }
          }
          imshow("image1", image1);*/
          waitKey(0);
         return  0;
     }
      void on_MedianBlur(int, void *)
      {
          Mat g_dstImage4= g_srcImage.clone();//一次就好
        //  medianBlur(g_srcImage, g_dstImage4, g_nMedianBlurValue * 2 + 1);
        bilateralFilter(g_srcImage, g_dstImage4, g_nMedianBlurValue, g_nMedianBlurValue *2, g_nMedianBlurValue /2);
    
          imshow("bilateralFilterimg", g_dstImage4);
      }
    發佈了80 篇原創文章 · 獲贊 13 · 訪問量 10萬+
    發表評論
    所有評論
    還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
    相關文章