天天看點

CVPR2019 Oral 論文《Side Window Filtering》C++代碼實作

算法原理

可以看這篇文章:

https://zhuanlan.zhihu.com/p/58326095

簡單介紹

這篇文章提出了一種新的方法用于濾波保留邊緣和角點,原理在上面的文章可以找到,同時也能找到論文原文和一份github的代碼,這種方法可以配合現有的任何濾波方法使用,讓這些濾波方法都強制保留邊緣。隻需要将對應的濾波的核的系數換成對應的濾波方法的核即可。這裡我實作了均值濾波的Side Window Filtering,代碼奉獻一下。

實作的細節

作者的matlab代碼:https://github.com/YuanhaoGong/SideWindowFilter

CVPR2019 Oral 論文《Side Window Filtering》C++代碼實作
CVPR2019 Oral 論文《Side Window Filtering》C++代碼實作

代碼

//針對灰階圖的均值濾波+CVPR 2019的SideWindowFilter
//其他種類的濾波直接換核即可

int cnt[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
vector <int> filter[8];

void InitFilter(int radius) {
  int n = radius * 2 + 1;
  for (int i = 0; i < 8; i++) {
    cnt[i] = 0;
    filter[i].clear();
  }
  for (int i = 0; i < 8; i++) {
    for (int x = 0; x < n; x++) {
      for (int y = 0; y < n; y++) {
        if (i == 0 && x <= radius && y <= radius) {
          filter[i].push_back(1);
        }
        else if (i == 1 && x <= radius && y >= radius) {
          filter[i].push_back(1);
        }
        else if (i == 2 && x >= radius && y <= radius) {
          filter[i].push_back(1);
        }
        else if (i == 3 && x >= radius && y >= radius) {
          filter[i].push_back(1);
        }
        else if (i == 4 && x <= radius) {
          filter[i].push_back(1);
        }
        else if (i == 5 && x >= radius) {
          filter[i].push_back(1);
        }
        else if (i == 6 && y >= radius) {
          filter[i].push_back(1);
        }
        else if (i == 7 && y <= radius) {
          filter[i].push_back(1);
        }
        else {
          filter[i].push_back(0);
        }
      }
    }
  }
  for (int i = 0; i < 8; i++) {
    int sum = 0;
    for (int j = 0; j < filter[i].size(); j++) sum += filter[i][j] == 1;
    cnt[i] = sum;
  }
}

Mat SideWindowFilter(Mat src, int radius = 1) {
  int row = src.rows;
  int col = src.cols;
  int channels = src.channels();
  InitFilter(radius);
  for (int i = 0; i < 8; i++) {
    printf("%d ", cnt[i]);
  }
  printf("\n");
  if (channels == 1) {
    Mat dst(row, col, CV_8UC1);
    for (int i = 0; i < row; i++) {
      for (int j = 0; j < col; j++) {
        if (i < radius || i + radius >= row || j < radius || j + radius >= col) {
          dst.at<uchar>(i, j) = src.at<uchar>(i, j);
          continue;
        }
        int minn = 256;
        int pos = 0;
        for (int k = 0; k < 8; k++) {
          int val = 0;
          int id = 0;
          for (int x = -radius; x <= radius; x++) {
            for (int y = -radius; y <= radius; y++) {
              //if (x == 0 && y == 0) continue;
              val += src.at<uchar>(i + x, j + y) * filter[k][id++];
            }
          }
          val /= cnt[k];
          if (abs(val - src.at<uchar>(i, j)) < minn) {
            minn = abs(val - src.at<uchar>(i, j));
            pos = k;
          }
        }
        int val = 0;
        int id = 0;
        for (int x = -radius; x <= radius; x++) {
          for (int y = -radius; y <= radius; y++) {
            //if (x == 0 && y == 0) continue;
            val += src.at<uchar>(i + x, j + y) * filter[pos][id++];
          }
        }
        dst.at<uchar>(i, j) = val / cnt[pos];
      }
    }
    return dst;
  }
  Mat dst(row, col, CV_8UC3);
  for (int c = 0; c < 3; c++) {
    for (int i = 0; i < row; i++) {
      for (int j = 0; j < col; j++) {
        if (i < radius || i + radius >= row || j < radius || j + radius >= col) {
          dst.at<Vec3b>(i, j)[c] = src.at<Vec3b>(i, j)[c];
          continue;
        }
        int minn = 256;
        int pos = 0;
        for (int k = 0; k < 8; k++) {
          int val = 0;
          int id = 0;
          for (int x = -radius; x <= radius; x++) {
            for (int y = -radius; y <= radius; y++) {
              //if (x == 0 && y == 0) continue;
              val += src.at<Vec3b>(i + x, j + y)[c] * filter[k][id++];
            }
          }
          val /= cnt[k];
          if (abs(val - src.at<Vec3b>(i, j)[c]) < minn) {
            minn = abs(val - src.at<Vec3b>(i, j)[c]);
            pos = k;
          }
        }
        int val = 0;
        int id = 0;
        for (int x = -radius; x <= radius; x++) {
          for (int y = -radius; y <= radius; y++) {
            //if (x == 0 && y == 0) continue;
            val += src.at<Vec3b>(i + x, j + y)[c] * filter[pos][id++];
          }
        }
        dst.at<Vec3b>(i, j)[c] = val / cnt[pos];
      }
    }
  }
  return dst;
}      

效果

原圖

CVPR2019 Oral 論文《Side Window Filtering》C++代碼實作

均值濾波20次後的結果

CVPR2019 Oral 論文《Side Window Filtering》C++代碼實作

使用論文算法對均值濾波做強制保邊結果

CVPR2019 Oral 論文《Side Window Filtering》C++代碼實作

結論