Advertisement
Guest User

bfs_pasterized

a guest
Apr 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #include "opencv2/video/tracking.hpp"
  6. #include "opencv2/imgproc.hpp"
  7. #include "opencv2/videoio.hpp"
  8. #include "opencv2/highgui.hpp"
  9. #include <vector>
  10. #include <queue>
  11.  
  12. using namespace std;
  13. using namespace cv;
  14.  
  15. const bool output = false;
  16.  
  17. struct color
  18. {
  19.     int R, G, B;
  20. };
  21.  
  22. struct point
  23. {
  24.     point();
  25.     int x, y;
  26. };
  27.  
  28. point::point()
  29. {
  30.     x = 1;
  31.     y = 1;
  32. }
  33.  
  34. bool colors_similar(color c1, color c2, int diff_colors = 40)
  35. {
  36.     return ((abs(c1.R - c2.R) < diff_colors) && (abs(c1.G - c2.G) < diff_colors) && (abs(c1.B - c2.B) < diff_colors));
  37.  
  38. }
  39.  
  40. color point_to_color(Mat& img, point& point)
  41. {
  42.     color color;
  43.     color.R = img.at<cv::Vec3b>(point.y - 1, point.x - 1)[2];
  44.     color.G = img.at<cv::Vec3b>(point.y - 1, point.x - 1)[1];
  45.     color.B = img.at<cv::Vec3b>(point.y - 1, point.x - 1)[0];
  46.     return color;
  47. }
  48.  
  49. void BFS(Mat& img, point& start_point, color& start_color, vector<vector<int>>& comp, int comp_now)
  50. {
  51.     queue<point> queue;
  52.     queue.push(start_point);
  53.     comp[start_point.y][start_point.x] = comp_now;
  54.  
  55.     cout << start_point.x << " " << start_point.y << endl;
  56.  
  57.     while (!queue.empty())
  58.     {
  59.         point last_point = queue.front();
  60.         queue.pop();
  61.         color new_color;
  62.         point new_point;
  63.         if (comp[last_point.y][last_point.x + 1] == 0)
  64.         {
  65.             new_point.x = last_point.x + 1;
  66.             new_point.y = last_point.y;
  67.             new_color = point_to_color(img, new_point);
  68.             if (colors_similar(start_color, new_color))
  69.             {
  70.                 comp[new_point.y][new_point.x] = comp_now;
  71.                 queue.push(new_point);
  72.             }
  73.         }
  74.         if (comp[last_point.y][last_point.x - 1] == 0)
  75.         {
  76.             new_point.x = last_point.x - 1;
  77.             new_point.y = last_point.y;
  78.             new_color = point_to_color(img, new_point);
  79.             if (colors_similar(start_color, new_color))
  80.             {
  81.                 comp[new_point.y][new_point.x] = comp_now;
  82.                 queue.push(new_point);
  83.             }
  84.         }
  85.         if (comp[last_point.y + 1][last_point.x] == 0)
  86.         {
  87.             new_point.x = last_point.x;
  88.             new_point.y = last_point.y + 1;
  89.             new_color = point_to_color(img, new_point);
  90.             if (colors_similar(start_color, new_color))
  91.             {
  92.                 comp[new_point.y][new_point.x] = comp_now;
  93.                 queue.push(new_point);
  94.             }
  95.         }
  96.         if (comp[last_point.y - 1][last_point.x] == 0)
  97.         {
  98.             new_point.x = last_point.x;
  99.             new_point.y = last_point.y - 1;
  100.             new_color = point_to_color(img, new_point);
  101.             if (colors_similar(start_color, new_color))
  102.             {
  103.                 comp[new_point.y][new_point.x] = comp_now;
  104.                 queue.push(new_point);
  105.             }
  106.         }
  107.     }
  108. }
  109.  
  110. void comp_init(vector<vector<int>>& comp, int x, int y)
  111. {
  112.     vector<int> new_line1, new_line2;
  113.  
  114.     for (int i = 0; i < x + 2; i++)
  115.     {
  116.         new_line1.push_back(-1);
  117.     }
  118.  
  119.     new_line2.push_back(-1);
  120.     for (int i = 0; i < x; i++)
  121.     {
  122.         new_line2.push_back(0);
  123.     }
  124.     new_line2.push_back(-1);
  125.  
  126.     comp.push_back(new_line1);
  127.     for (int j = 0; j < y; j++)
  128.     {
  129.         comp.push_back(new_line2);
  130.     }
  131.     comp.push_back(new_line1);
  132. }
  133.  
  134. void print_components(vector<vector<int>>& comp, int x, int y)
  135. {
  136.     if (output)
  137.     {
  138.         for (int i = 0; i < y + 2; i++)
  139.         {
  140.             for (int j = 0; j < x + 2; j++)
  141.             {
  142.                 if (comp[i][j] == -1)
  143.                     cout << " ";
  144.                 if ((comp[i][j] >= 0) && (comp[i][j] <= 9))
  145.                     cout << "  ";
  146.                 if ((comp[i][j] >= 10) && (comp[i][j] <= 99))
  147.                     cout << " ";
  148.                 cout << comp[i][j];
  149.             }
  150.             cout << endl;
  151.         }
  152.     }
  153. }
  154.  
  155. void print_colors(vector<color>& colors)
  156. {
  157.     if (output)
  158.     {
  159.         for (int i = 0; i < colors.size(); i++)
  160.         {
  161.             cout << i + 1 << " " << colors[i].R << " " << colors[i].G << " " << colors[i].B << endl;
  162.         }
  163.     }
  164. }
  165.  
  166. int main()
  167. {
  168.     Mat img;
  169.     img = imread("img_sample_4.bmp");
  170.  
  171.     Mat new_img = img;
  172.  
  173.     vector<color> colors; //every first color in component
  174.     vector<vector<int>> comp;
  175.  
  176.     comp_init(comp, img.cols, img.rows);   
  177.     int comp_now = 1;
  178.  
  179.     print_components(comp, img.cols, img.rows);
  180.  
  181.     int sizeX = img.cols;
  182.     int sizeY = img.rows;
  183.    
  184.     cout << sizeX << " " << sizeY << endl;
  185.  
  186.     for (int i = 1; i < img.rows + 1; i++)
  187.     {
  188.         for (int j = 1; j < img.cols + 1; j++)
  189.         {
  190.             if (comp[i][j] == 0)
  191.             {
  192.                 color start_color;
  193.                 point start_point;
  194.                 start_point.x = j;
  195.                 start_point.y = i;
  196.                 start_color = point_to_color(img, start_point);
  197.  
  198.                 colors.push_back(start_color);
  199.  
  200.                 BFS(img, start_point, start_color, comp, comp_now);
  201.                 print_components(comp, img.cols, img.rows);
  202.  
  203.                 comp_now += 1;
  204.             }
  205.         }
  206.     }
  207.  
  208.     color upd_color;
  209.  
  210.     for (int i = 0; i < new_img.rows; i++)
  211.     {
  212.         for (int j = 0; j < new_img.cols; j++)
  213.         {
  214.             upd_color = colors[comp[i + 1][j + 1] - 1];
  215.             new_img.at<cv::Vec3b>(i, j)[2] = upd_color.R;
  216.             new_img.at<cv::Vec3b>(i, j)[1] = upd_color.G;
  217.             new_img.at<cv::Vec3b>(i, j)[0] = upd_color.B;
  218.         }
  219.     }
  220.  
  221.     string way = "D:/images/pasterized4.bmp";
  222.     imwrite(way, new_img);
  223.  
  224.     print_colors(colors);
  225.  
  226.     system("pause");
  227.     return 0;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement