Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <stdlib.h>
- #include <conio.h>
- #include "opencv2/video/tracking.hpp"
- #include "opencv2/imgproc.hpp"
- #include "opencv2/videoio.hpp"
- #include "opencv2/highgui.hpp"
- #include <vector>
- #include <queue>
- using namespace std;
- using namespace cv;
- const bool output = false;
- struct color
- {
- int R, G, B;
- };
- struct point
- {
- point();
- int x, y;
- };
- point::point()
- {
- x = 1;
- y = 1;
- }
- bool colors_similar(color c1, color c2, int diff_colors = 40)
- {
- return ((abs(c1.R - c2.R) < diff_colors) && (abs(c1.G - c2.G) < diff_colors) && (abs(c1.B - c2.B) < diff_colors));
- }
- color point_to_color(Mat& img, point& point)
- {
- color color;
- color.R = img.at<cv::Vec3b>(point.y - 1, point.x - 1)[2];
- color.G = img.at<cv::Vec3b>(point.y - 1, point.x - 1)[1];
- color.B = img.at<cv::Vec3b>(point.y - 1, point.x - 1)[0];
- return color;
- }
- void BFS(Mat& img, point& start_point, color& start_color, vector<vector<int>>& comp, int comp_now)
- {
- queue<point> queue;
- queue.push(start_point);
- comp[start_point.y][start_point.x] = comp_now;
- cout << start_point.x << " " << start_point.y << endl;
- while (!queue.empty())
- {
- point last_point = queue.front();
- queue.pop();
- color new_color;
- point new_point;
- if (comp[last_point.y][last_point.x + 1] == 0)
- {
- new_point.x = last_point.x + 1;
- new_point.y = last_point.y;
- new_color = point_to_color(img, new_point);
- if (colors_similar(start_color, new_color))
- {
- comp[new_point.y][new_point.x] = comp_now;
- queue.push(new_point);
- }
- }
- if (comp[last_point.y][last_point.x - 1] == 0)
- {
- new_point.x = last_point.x - 1;
- new_point.y = last_point.y;
- new_color = point_to_color(img, new_point);
- if (colors_similar(start_color, new_color))
- {
- comp[new_point.y][new_point.x] = comp_now;
- queue.push(new_point);
- }
- }
- if (comp[last_point.y + 1][last_point.x] == 0)
- {
- new_point.x = last_point.x;
- new_point.y = last_point.y + 1;
- new_color = point_to_color(img, new_point);
- if (colors_similar(start_color, new_color))
- {
- comp[new_point.y][new_point.x] = comp_now;
- queue.push(new_point);
- }
- }
- if (comp[last_point.y - 1][last_point.x] == 0)
- {
- new_point.x = last_point.x;
- new_point.y = last_point.y - 1;
- new_color = point_to_color(img, new_point);
- if (colors_similar(start_color, new_color))
- {
- comp[new_point.y][new_point.x] = comp_now;
- queue.push(new_point);
- }
- }
- }
- }
- void comp_init(vector<vector<int>>& comp, int x, int y)
- {
- vector<int> new_line1, new_line2;
- for (int i = 0; i < x + 2; i++)
- {
- new_line1.push_back(-1);
- }
- new_line2.push_back(-1);
- for (int i = 0; i < x; i++)
- {
- new_line2.push_back(0);
- }
- new_line2.push_back(-1);
- comp.push_back(new_line1);
- for (int j = 0; j < y; j++)
- {
- comp.push_back(new_line2);
- }
- comp.push_back(new_line1);
- }
- void print_components(vector<vector<int>>& comp, int x, int y)
- {
- if (output)
- {
- for (int i = 0; i < y + 2; i++)
- {
- for (int j = 0; j < x + 2; j++)
- {
- if (comp[i][j] == -1)
- cout << " ";
- if ((comp[i][j] >= 0) && (comp[i][j] <= 9))
- cout << " ";
- if ((comp[i][j] >= 10) && (comp[i][j] <= 99))
- cout << " ";
- cout << comp[i][j];
- }
- cout << endl;
- }
- }
- }
- void print_colors(vector<color>& colors)
- {
- if (output)
- {
- for (int i = 0; i < colors.size(); i++)
- {
- cout << i + 1 << " " << colors[i].R << " " << colors[i].G << " " << colors[i].B << endl;
- }
- }
- }
- int main()
- {
- Mat img;
- img = imread("img_sample_4.bmp");
- Mat new_img = img;
- vector<color> colors; //every first color in component
- vector<vector<int>> comp;
- comp_init(comp, img.cols, img.rows);
- int comp_now = 1;
- print_components(comp, img.cols, img.rows);
- int sizeX = img.cols;
- int sizeY = img.rows;
- cout << sizeX << " " << sizeY << endl;
- for (int i = 1; i < img.rows + 1; i++)
- {
- for (int j = 1; j < img.cols + 1; j++)
- {
- if (comp[i][j] == 0)
- {
- color start_color;
- point start_point;
- start_point.x = j;
- start_point.y = i;
- start_color = point_to_color(img, start_point);
- colors.push_back(start_color);
- BFS(img, start_point, start_color, comp, comp_now);
- print_components(comp, img.cols, img.rows);
- comp_now += 1;
- }
- }
- }
- color upd_color;
- for (int i = 0; i < new_img.rows; i++)
- {
- for (int j = 0; j < new_img.cols; j++)
- {
- upd_color = colors[comp[i + 1][j + 1] - 1];
- new_img.at<cv::Vec3b>(i, j)[2] = upd_color.R;
- new_img.at<cv::Vec3b>(i, j)[1] = upd_color.G;
- new_img.at<cv::Vec3b>(i, j)[0] = upd_color.B;
- }
- }
- string way = "D:/images/pasterized4.bmp";
- imwrite(way, new_img);
- print_colors(colors);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement