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); }
void silhouetteCount(string nameImageFile){ int silhouetteCounter=0; GImage* imageFile = new GImage(nameImageFile); GBufferedImage* imgageInBuffer = new GBufferedImage(0,0,imageFile->getWidth(), imageFile->getHeight()); imgageInBuffer->load(nameImageFile); int imgHaight = imgageInBuffer->getHeight(); int imgWidth = imgageInBuffer->getWidth(); GWindow gw(imgWidth, imgHaight); gw.add(imageFile); myGrid imageBinaring(imgHaight, imgWidth); binaringImageToGrid(imgageInBuffer, imageBinaring); bool silhouetteIsValid = false; for(int x = 0; x < imgHaight; x++){ for (int y = 0; y < imgWidth; y++){ if (imageBinaring.get_color(x,y) == true && imageBinaring.get_visited(x,y) == false) { selectFindeArea(x,y, imageBinaring, silhouetteIsValid); if (silhouetteIsValid) silhouetteCounter++; } } } cout<<"in File: "<<nameImageFile<<"; found "<< silhouetteCounter<<" silhouettes."<<endl; cout<<endl; delete imgageInBuffer; delete imageFile; }
/* Depending on how you approach your problem decomposition, you * may want to rewrite some of these lines, move them inside loops, * or move them inside helper functions, etc. * * TODO: rewrite this comment. */ int main() { GWindow gw; gw.setTitle("Fauxtoshop"); gw.setVisible(true); cout << "Welcome to Fauxtoshop!" << endl; cout << "Enter name of image file to open (or blank to quiet): "; string img_name; getline(cin, img_name); cout << "Opening image file, may take a minute..." << endl; GBufferedImage img; while (!openImageFromFilename(img, img_name)) { cout << "Can't open the file, please enter again: "; getline(cin, img_name); } gw.setSize(img.getWidth(), img.getHeight()); gw.add(&img,0,0); string menu = "Which image filter would you like to apply?\n" " 1 - Scatter\n" " 2 - Edge detection\n" " 3 - \"Green screen\" with another image\n" " 4 - Compare image with another image"; cout << menu << endl; string choice_str = "Your choice: "; int choice = getInteger(choice_str, choice_str); switch(choice) { case 1: scatter(img); break; case 2: edge_detection(img); break; case 3: green_screen(img); break; } int row, col; getMouseClickLocation(row, col); return 0; }
/* Main function */ int main() { GWindow window(WIDTH, HEIGHT); GBufferedImage *image = new GBufferedImage(WIDTH, HEIGHT, 0xFFFFFF); window.add(image); Point newPoint(WIDTH - OFFSET, HEIGHT - OFFSET); // start point for (int i = 0; i <= 200000; i++){ image->setRGB(newPoint.getX() - 1, newPoint.getY() - 1, 0x000000); newPoint = moveRandomly(newPoint); } return 0; }
/* STARTER CODE HELPER FUNCTION - DO NOT EDIT * * Attempts to save the image file to 'filename'. * * This function returns true when the image was successfully saved * to the file specified, otherwise it returns false. */ static bool saveImageToFilename(const GBufferedImage &img, string filename) { try { img.save(filename); } catch (...) { return false; } return true; }
/* This function takes a picture and make from it filled grid. Each element of * grid is image`s pixel. Grid is filled with boolean values. If it is a black * point, it will be marked as true, other - false. */ void imageParsing(string fileName) { string fileAdress = "images/"; fileAdress = fileAdress + fileName; GBufferedImage image; image.load(fileAdress); pixels.resize(image.getWidth(), image.getHeight()); for (int column = 0; column < image.getWidth(); column++) { for (int row = 0; row < image.getHeight(); row++) { if (image.getRGB(column, row) <= GREY) { pixels.set(column, row, true); } else { pixels.set(column, row, false); } } } for (int column = 0; column < pixels.nCols; column++) { for (int row = 0; row < pixels.nRows; row++) { if (pixels.inBounds(column, row) && pixels.get(column, row) == true) { pixels.set(column, row, false); Points blackPoint = makePoint(column, row); checkAllNeighbours.push(blackPoint); detectingSilouettes(pixels); } } } }
/* Depending on how you approach your problem decomposition, you * may want to rewrite some of these lines, move them inside loops, * or move them inside helper functions, etc. * * TODO: rewrite this comment. */ int main() { GWindow gw; gw.setTitle("Fauxtoshop"); gw.setVisible(true); GBufferedImage img; while (true) { ifstream infile; cout << "Welcome to Fauxtoshop!" << endl; string prompt = "Enter the name of image file to open (or blank to quit):"; string filename= openFile(infile, prompt, img); if (filename == "") break; gw.setSize(img.getWidth(), img.getHeight()); gw.add(&img,0,0); int choice = chooseFilter(); dealWithImage(img, choice); saveImage(img); } /*int row, col; getMouseClickLocation(row, col);*/ return 0; }
GBufferedImage* GBufferedImage::diff(GBufferedImage& image, int diffPixelColor) const { int w1 = (int) getWidth(); int h1 = (int) getHeight(); int w2 = (int) image.getWidth(); int h2 = (int) image.getHeight(); int wmin = std::min(w1, w2); int hmin = std::min(h1, h2); int wmax = std::max(w1, w2); int hmax = std::max(h1, h2); GBufferedImage* result = new GBufferedImage(wmax, hmax, diffPixelColor); result->fillRegion(0, 0, w1, h1, m_backgroundColor); for (int y = 0; y < hmin; y++) { for (int x = 0; x < wmin; x++) { int px1 = m_pixels[y][x]; int px2 = image.m_pixels[y][x]; if (px1 != px2) { result->setRGB(x, y, diffPixelColor); } } } return result; }
/* * Function: floodFill(); * -------------------- * Function that fills clicked color with selected color * * Preconditions: None * * @param: A buffered image object that tells us where and what was clicked * x,y position clicked * color selected to change to * @return: returns the numebr of pixels changed */ int floodFill(GBufferedImage& image, int x, int y, int color) { int colorClicked = image.getRGB(x,y); int numChangedPixels = 0; // First check to make sure that we don't do anything if the color picked is also // The color selected. If it is same return 0 if(colorClicked==color){ return 0; } // Doubly make sure that the place clicked is inBounds if(image.inBounds(x,y)){ return floodFillRecursion(image,x,y,colorClicked,color,numChangedPixels); } else { try { throw 0003; } catch (int e) { cout << "An exception occurred (x||y out of bounds). Exception Nr. " << e << '\n'; } } return 0; }
static void detectEdge(GBufferedImage & img,const Grid<int> & original) { int diff = askForNumber("Enter threshold for edge detection:", 0, 256); Grid<int> edgeGrid(original.numRows(), original.numCols()); for (int row = 0; row < original.numRows(); row++) { for (int col = 0; col < original.numCols(); col++) { if (isEdge(original, row, col, diff)) { edgeGrid[row][col] = BLACK; } else { edgeGrid[row][col] = WHITE; } } } img.fromGrid(edgeGrid); }
int GBufferedImage::countDiffPixels(GBufferedImage& image) const { int w1 = (int) getWidth(); int h1 = (int) getHeight(); int w2 = (int) image.getWidth(); int h2 = (int) image.getHeight(); int wmin = std::min(w1, w2); int hmin = std::min(h1, h2); int overlap = std::min(w1, w2) * std::min(h1, h2); int diffPxCount = (w1 * h1 - overlap) + (w2 * h2 - overlap); for (int y = 0; y < hmin; y++) { for (int x = 0; x < wmin; x++) { int px1 = m_pixels[y][x]; int px2 = image.m_pixels[y][x]; if (px1 != px2) { diffPxCount++; } } } return diffPxCount; }
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; } }
/* * scatter the image by using the RGB value in grid within radius and bound. */ static void scatter(GBufferedImage & img,const Grid<int> & original) { int r = askForNumber("Enter degree of scatter [1-100]:", 0, 101); //for radius Grid<int> scatterGrid(original.numRows(), original.numCols()); for (int row = 0; row < original.numRows(); row++) { for (int col = 0; col < original.numCols(); col++) { while (true) { int rrow = row + randomInteger(-r,r); int rcol = col + randomInteger(-r,r); if(original.inBounds(rrow, rcol)) { scatterGrid[row][col] = original[rrow][rcol]; break; } //cout << row << "," << col << endl; } } } img.fromGrid(scatterGrid); }
/* * Function: floodFillRecursion() * -------------------- * Helper function that does the actual recursion for floodFill * * Preconditions: None * * @param: A buffered image object that tells us where and what was clicked * x,y position clicked * color selected to change to * @return: The number of pixels actually changed. */ int floodFillRecursion(GBufferedImage& image, int x, int y, int colorClicked, int color,int& numChangedPixels) { if(image.getRGB(x,y)==colorClicked){ image.setRGB(x,y,color); numChangedPixels++; } else { return numChangedPixels; } if(image.inBounds(x-1,y)){ floodFillRecursion(image,x-1,y,colorClicked,color,numChangedPixels); } if(image.inBounds(x+1,y)){ floodFillRecursion(image,x+1,y,colorClicked,color,numChangedPixels); } if(image.inBounds(x,y-1)){ floodFillRecursion(image,x,y-1,colorClicked,color,numChangedPixels); } if(image.inBounds(x,y+1)){ floodFillRecursion(image,x,y+1,colorClicked,color,numChangedPixels); } return numChangedPixels; }
/* STARTER CODE HELPER FUNCTION - DO NOT EDIT * * Attempts to open the image file 'filename'. * * This function returns true when the image file was successfully * opened and the 'img' object now contains that image, otherwise it * returns false. */ static bool openImageFromFilename(GBufferedImage &img, string filename) { try { img.load(filename); } catch (...) { return false; } return true; }
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); }
void gbufferedImageTest() { GWindow gw; gw.setSize(900, 700); gw.setTitle("Test"); GButton* button1 = new GButton("Click Me 1"); gw.add(button1, 250, 80); //GButton* button2 = new GButton("Click Me 2"); //gw.addToRegion(button2, "NORTH"); GLabel* label = new GLabel("test!"); gw.add(label, 10, 60); std::cout << "About to construct GBufferedImage." << std::endl; GBufferedImage* img = new GBufferedImage(10, 80, 200, 250); std::cout << "Done constructing GBufferedImage." << std::endl; gw.add(img, 50, 50); // gw.addToRegion(img, "SOUTH"); gw.setVisible(true); // GBufferedImage* img2 = new GBufferedImage(20, 20); // img2->fill(GBufferedImage::createRgbPixel(255, 0, 255)); // Grid<int> grid = img2->toGrid(); // cout << "grid of pixels before: " << grid << endl; // for (int y = 4; y <= 18; y++) { // for (int x = 2; x <= 9; x++) { // grid[y][x] = GBufferedImage::createRgbPixel(0, 255, 0); // } // } // cout << "grid of pixels after: " << grid << endl; // img2->fromGrid(grid); // gw.add(img2, 350, 20); GBufferedImage* img3 = new GBufferedImage(); img3->load("rainbow.png"); cout << "adding the image!" << endl; gw.add(img3, 10, 20); // pause(2000); cout << "start toGrid" << endl; Grid<int> grid3 = img3->toGrid(); cout << "end toGrid, start rgb shit" << endl; for (int y = 0; y < grid3.height(); y++) { for (int x = 0; x < grid3.width(); x++) { int red, green, blue; GBufferedImage::getRedGreenBlue(grid3[y][x], red, green, blue); grid3[y][x] = GBufferedImage::createRgbPixel(green, red, blue); } } cout << "end rgb shit, start fromGrid" << endl; img3->fromGrid(grid3); cout << "end fromGrid" << endl; pause(2000); return; // fill img->fill(0xff00ff); // purple std::cout << "About to setRGB on GBufferedImage." << std::endl; for (int y = 2; y < img->getHeight() - 2; y++) { for (int x = 5; x <= img->getWidth() - 5; x++) { img->setRGB(x, y, 0xffcc33); } } std::cout << "Done setting RGB on GBufferedImage." << std::endl; border(img); pause(500); std::cout << "About to resize on GBufferedImage." << std::endl; img->resize(100, 50); border(img); pause(500); std::cout << "About to resize on GBufferedImage." << std::endl; img->resize(200, 80); border(img); pause(500); std::cout << "About to setRGB on GBufferedImage." << std::endl; for (int y = 10; y < img->getHeight() - 10; y++) { for (int x = 10; x <= img->getWidth() - 10; x++) { img->setRGB(x, y, 0xff33cc); } } border(img); std::cout << "About to remove other shit." << std::endl; pause(200); gw.remove(label); gw.remove(button1); //gw.removeFromRegion(button2, "NORTH"); pause(200); std::cout << "About to remove GBufferedImage." << std::endl; pause(200); gw.remove(img); // gw.removeFromRegion(img, "SOUTH"); pause(200); std::cout << "Test complete." << std::endl; std::cout << std::endl; }