Mochi
Mochi

Reputation: 71

Removing BufferedImage pixel values and or setting them transparent

I have been working with the polygon class and trying to set the pixel values inside of the polygon to transparent or remove them all together if this is possible, however I have hit a bit of a wall as I am trying to store the values as RGB int values and don't know how I would be able to make a pixel transparent/removed via this method.

Additionally to this I would also like to do the same thing but keeping pixels inside the polygon and deleting those outside if possible in order to be left with only the pixels contained within the polygon. I have searched around for this before but to no avail.

I did attempt to create a SSCCE for this to make it easier to work with and view for anyone taking the time to help however as its part of a much larger programme that I am working on creating one is proving to take some time, however once I have one working to better demonstrate this problem I will edit this post.

Thank you to anyone for taking the time to help me with this problem

Below I have some code for what I am currently using to segment the pixels that are contained within an already specified polygon. This is extremely similar to the way i do it for setting pixels outside the polygon to transparent only with the if statement arguments swapped around to remove a segment of the image and haveing a return for newImage rather than save image stuff and it works perfectly, however when I do it this way to save the pixels contained in the polygon it doesn't save for some reason.

    public void saveSegment(int tabNum, BufferedImage img) {
    segmentation = new GUI.Segmentation();
    Polygon p = new Polygon();
    Color pixel;

    p = createPolygon(segmentation);

    int height = img.getHeight();
    int width = img.getWidth();
    newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    //loop through the image to fill the 2d array up with the segmented pixels
    for(int y = 0; y < height; y++) {
        for(int x = 0; x < width; x++) {

            //If the pixel is inside polygon
            if(p.contains(x, y) == true) {
                pixel = new Color(img.getRGB(x, y));
                //set pixel equal to the RGB value of the pixel being looked at
                int r = pixel.getRed(); // red component 0...255
                int g = pixel.getGreen(); // green component 0...255
                int b = pixel.getBlue(); // blue component 0...255
                int a = pixel.getAlpha(); // alpha (transparency) component 0...255
                int col = (a << 24) | (r << 16) | (g << 8) | b;
                newImage.setRGB(x, y, col);
            }
            else {
                pixel = new Color(img.getRGB(x, y));
                int a = 0; // alpha (transparency) component 0...255
                int col = (a << 24);
                newImage.setRGB(x, y, col);
            }
        }
    }
    try {
        //then save as image once all in correct order
        ImageIO.write(newImage, "bmp", new File("saved-Segment.bmp"));
        JOptionPane.showMessageDialog(null, "New image saved successfully");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Upvotes: 2

Views: 3129

Answers (1)

VGR
VGR

Reputation: 44328

An easier way is to use Java2D's clipping capability:

BufferedImage cutHole(BufferedImage image, Polygon holeShape) {

    BufferedImage newImage = new BufferedImage(
        image.getWidth(), image.getHeight(), image.getType());

    Graphics2D g = newImage.createGraphics();

    Rectangle entireImage =
        new Rectangle(image.getWidth(), image.getHeight());

    Area clip = new Area(entireImage);
    clip.subtract(new Area(holeShape));

    g.clip(clip);
    g.drawImage(image, 0, 0, null);

    g.dispose();

    return newImage;
}

BufferedImage clipToPolygon(BufferedImage image, Polygon polygon) {

    BufferedImage newImage = new BufferedImage(
        image.getWidth(), image.getHeight(), image.getType());

    Graphics2D g = newImage.createGraphics();

    g.clip(polygon);
    g.drawImage(image, 0, 0, null);

    g.dispose();

    return newImage;
}

Upvotes: 3

Related Questions