中值滤波器(2D int)中的边界情况c ++

时间:2018-10-04 00:36:06

标签: c++ median

在开发2D整数中值滤波器时,我在寻找2D数组边界值的中值时遇到了一个问题。

我的代码

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <math.h>
    #include <cilk/cilk.h>

    int median(int x1, int y1, int k);
    void recursion(int x1, int y1, int x2, int y2);

    using namespace std;

    std::vector < std::vector <int>> x;
    std::vector < std::vector <int>> y;

    int n, m, k;
    //n=y
    //m=x

    int main()
    {
        std::fstream File;
        File.open("test.txt");

        //error msg reading
        if (File.fail()) {
            cout << "error opening file";
        }

        //read file
        while (!File.eof())
        {
            File >> n;
            File >> m;
            File >> k;

            for (int i = 0; i < n; i++) {
                vector <int> row;
                for (int j = 0; j < m; j++) {
                    int readFromFile = 0;
                    File >> readFromFile;
                    row.push_back(readFromFile);
                }
                x.push_back(row);
            }

        }

        //print vector values to console to see if its correct
        cout << n; 
        cout << "\n";
        cout << m;
        cout << "\n";
        cout << k;
        cout << "\n";

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cout << x[i][j];
                cout << " ";
            }
            cout << "\n";
        }

        recursion (0,0,n,m);
    }

    void recursion(int x1, int y1, int x2, int y2)
    {
        int mDiv2=x2/2;
        int nDiv2=y2/2;

        if ((x2 == x1) && (y2 == y1))
        {
            y[x2][y2] = median(k);
            return;
        }
        else
        {
            cilk_spawn recursion(x1, y1, x2/2, y2/2); //quadrant 1
            cilk_spawn recursion(mDiv2, y1, x2/2, nDiv2/2); //quadrant 2
            cilk_spawn recursion(x1, nDiv2, mDiv2/2, y2/2); //quadrant 3
            cilk_spawn recursion(mDiv2, nDiv2, x2/2, y2/2); //quadrant 4
        }
    }

int median(int x, int y, int k)
{
    vector <int> s;

    for (int i = -k; i < k; i++) {
        for (int j = -k ; j < k; j++) {
            if (((x+i>=0) && (y+j>=0)) && ((x+i<=m) && (y+j>=n))){
            s.push_back(x[x+i][y+i]);}
            else
            {
                //boundary
            }
        }
    }
//sort
}

在中位数函数中,我将与向量x一起存储在向量x中存储的2D数组的点。如果值超出范围,我想存储最近的元素的值。例如,带有k个中值滤波器3的点(0,0)将收集从(-3,-3)到(3,3)的所有数据。因为(-3,-3)不存在,所以最接近的元素将是(0,0),从而存储x [0] [0]的值。如果向量s超出范围,如何将最接近的元素的值分配给vector?

0 个答案:

没有答案