C++ Bc. 14 cpp

Z GeoWikiCZ
(přesměrováno z C plus plus Bc. 14 cpp)
#include <iostream>
#include <matvec/matvec.h>

GNU_gama::Mat<int> filtr(const GNU_gama::Mat<int>& N, int kernel=3)
{
  const int K = kernel/2;

  GNU_gama::Mat<int> M(N.rows(), N.cols());

  for (int i=1; i<=N.rows(); i++)
    for (int j=1; j<=N.cols(); j++)
      {
        int s = 0;
        int n = 0;
        for (int a=-K; a<=K; a++)
          for (int b=-K; b<=K; b++)
            {
              int k = i+a;
              int l = j+b;
              if (k < 1 || k > N.rows()) continue;
              if (l < 1 || l > N.cols()) continue;
                                     
              s += N(k,l);
              n++;
            }
        M(i,j) = s/n;
      }

  return M;
}


int main()
{
  GNU_gama::Mat<int> A(10,15);

  A =
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
    0,0,9,9,0,0,0,0,0,0,0,0,0,0,2,
    0,0,9,9,0,0,0,0,0,0,3,3,0,0,3,
    0,0,9,9,0,0,0,0,0,0,3,3,0,0,4,
    0,0,9,9,0,0,0,0,0,0,0,0,0,0,5,
    0,0,9,9,9,9,9,0,0,0,0,0,0,0,6,
    0,0,9,9,9,9,9,0,0,0,0,0,0,0,7,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,
    0,1,2,3,4,5,6,7,8,0,0,0,0,0,9;

  std::cout << A << "\n" << filtr(A,3);
}

výstup

10 15

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
0 0 9 9 0 0 0 0 0 0 0 0 0 0 2 
0 0 9 9 0 0 0 0 0 0 3 3 0 0 3 
0 0 9 9 0 0 0 0 0 0 3 3 0 0 4 
0 0 9 9 0 0 0 0 0 0 0 0 0 0 5 
0 0 9 9 9 9 9 0 0 0 0 0 0 0 6 
0 0 9 9 9 9 9 0 0 0 0 0 0 0 7 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 
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