Example #1
0
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);
}
Example #2
0
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);
}
Example #3
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);
}
Example #4
0
/*
 * 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);
}
Example #5
0
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);
}