static void scatter(GBufferedImage &img) { cout << "Enter degree of scatter [1-100]: "; int degree; cin >> degree; Grid<int> original = img.toGrid(); int height = original.height(); int width = original.width(); Grid<int> new_grid(height, width); for (int row = 0; row < height; ++row) for (int col = 0; col < width; ++col) { int left_col = max(0, col - degree); int right_col = min(width - 1, col + degree); int sample_col = randomInteger(left_col, right_col); int up_row = max(0, row - degree); int down_row = min(height - 1, row + degree); int sample_row = randomInteger(up_row, down_row); new_grid[row][col] = original[sample_row][sample_col]; } img.fromGrid(new_grid); }
static void edge_detection(GBufferedImage &img) { cout << "Enter threshold for edge detection: "; int threshold; cin >> threshold; Grid<int> original = img.toGrid(); int height = original.height(); int width = original.width(); Grid<int> new_grid(height, width); for (int row = 0; row < height; ++row) for (int col = 0; col < width; ++col) { if (is_edge(original, row, col, threshold)) { new_grid[row][col] = BLACK; } else { new_grid[row][col] = WHITE; } } img.fromGrid(new_grid); }
static void dealWithImage(GBufferedImage & img, int n) { Grid<int> original = img.toGrid(); switch (n) { case 1: scatter(img, original); break; case 2: detectEdge(img, original); break; case 3: //greenScreen(img, original); break; case 4: //compareImg(); break; } }
static void green_screen(GBufferedImage &img) { cout << "Now choose another file to add to your background image.\n"; cout << "Enter name of image file to open: "; string img_name; getline(cin, img_name); cout << "Opening image file, may take a minute...\n"; GBufferedImage sticker_img; while (!openImageFromFilename(sticker_img, img_name)) { cout << "Can't open the file, please enter again: "; getline(cin, img_name); } string choose_threshold = "Now choose a tolerance threshold: "; int threshold = getInteger(choose_threshold, choose_threshold); cout << "Enter location to place image as \"(row, col)\" (or blank to use mouse): "; string location; getline(cin, location); int loc_row, loc_col; while(sscanf(location.c_str(), "(%d,%d)", &loc_row, &loc_col) != 2) { if (location.size() == 0) { cout << "Now click the background image to place image: " << endl; getMouseClickLocation(loc_row, loc_col); cout << "You chose " << loc_row << "," << loc_col << endl; break; } else { cout << "Can't read location, please enter again: "; getline(cin, location); } } Grid<int> original = img.toGrid(); int ori_height = original.height(); int ori_width = original.width(); Grid<int> sticker = sticker_img.toGrid(); int sticker_height = sticker.height(); int sticker_width = sticker.width(); for (int row = 0; row < sticker_height; ++row) for (int col = 0; col < sticker_width; ++col) { int ori_row = loc_row + row; int ori_col = loc_col + col; if (ori_row < ori_height && ori_col < ori_width) { int pixel = sticker[row][col]; if (rgb_diff(pixel, GREEN) > threshold) { original[ori_row][ori_col] = pixel; } } } img.fromGrid(original); }