Dear readers, today read in this article how to smooth the image in C ++ program?
If you have a 2D matrix M that represents the gray scale of an image, then we need to design a smoothie to create a gray scale of every 8 gray pixels that surrounds all 8 pixels and the average gray scale of itself (Downwards). If a cell has less than 8 surrounding cells, change all possible pixels.
Example for Image Smoother in C++
#include <bits/stdc++.h> using namespace std; void print_vector(vector<vector<auto> > v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << "["; for(int j = 0; j <v[i].size(); j++){ cout << v[i][j] << ", "; } cout << "],"; } cout << "]"<<endl; } class ImageSmoothProg{ public: vector<vector<int>> imageSmoother(vector<vector<int>>& M) { int R = M.size(); int C = M[0].size(); vector<int> d{ -1, 0, 1 }; vector<vector<int> > res(R, vector<int>(C, 0)); for (int i = 0; i < R; ++i) { for (int j = 0; j < C; ++j) { int sum = 0, count = 0; for (int k = 0; k < 3; ++k) { for (int l = 0; l < 3; ++l) { int m = i + d[k], n = j + d[l]; if (m >= 0 && m < R && n >= 0 && n < C) ++count, sum += M[m][n]; } } res[i][j] = sum / count; } } return res; } }; main(){ ImageSmoothProg ob; vector<vector<int>> v = {{1,1,1},{1,0,1},{1,1,1}}; print_vector(ob.imageSmoother(v)); }
Output
[[0, 0, 0, ],[0, 0, 0, ],[0, 0, 0, ],]