简体   繁体   中英

Give color to a buffered image in java

I have a buffered image that looks something like this. 在此处输入图片说明

How do I give it color? I am new to image processing in java any help would be greatly appreciated. This is a sample of the code that generates this picture.

   public BufferedImage getDifferenceImage(BufferedImage img1, BufferedImage img2) {
    int width1 = img1.getWidth(); // Change - getWidth() and getHeight() for BufferedImage
    int width2 = img2.getWidth(); // take no arguments
    int height1 = img1.getHeight();
    int height2 = img2.getHeight();
    if ((width1 != width2) || (height1 != height2)) {
        System.err.println("Error: Images dimensions mismatch");
        System.exit(1);
    }

    // NEW - Create output Buffered image of type RGB
    BufferedImage outImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    // Modified
    long diff;
    int result; // Stores output pixel
    for (int i = 0; i < height1; i++) {
        for (int j = 0; j < width1; j++) {
            int rgb1 = img1.getRGB(j, i);
            int rgb2 = img2.getRGB(j, i);
            int r1 = (rgb1 >> 16) & 0xff;
            int g1 = (rgb1 >> 8) & 0xff;
            int b1 = (rgb1) & 0xff;
            int r2 = (rgb2 >> 16) & 0xff;
            int g2 = (rgb2 >> 8) & 0xff;
            int b2 = (rgb2) & 0xff;
            diff = Math.abs(r1 - r2); // Change
            diff += Math.abs(g1 - g2);
            diff += Math.abs(b1 - b2);
            diff /= 255; // Change - Ensure result is between 0 - 255
            // Make the difference image gray scale
            // The RGB components are all the same
            result = (diff << 16) | (diff << 8) | diff;
            outImg.setRGB(j, i, result); // Set result
        }
    }

    // Now return
    return outImg;
}

Try changing the code where you are calculating the difference between colours where instead of setting each of the colour channels to be the same and it generating grayscale, try setting each colour channel to be their own respective differences rather than adding up the differences in each colour channel. In other words, try this:

// Modified - Changed to int as pixels are ints
int rDiff, gDiff, bDiff;
int result; // Stores output pixel
for (int i = 0; i < height1; i++) {
    for (int j = 0; j < width1; j++) {
        int rgb1 = img1.getRGB(j, i);
        int rgb2 = img2.getRGB(j, i);
        int r1 = (rgb1 >> 16) & 0xff;
        int g1 = (rgb1 >> 8) & 0xff;
        int b1 = (rgb1) & 0xff;
        int r2 = (rgb2 >> 16) & 0xff;
        int g2 = (rgb2 >> 8) & 0xff;
        int b2 = (rgb2) & 0xff;
        rDiff = Math.abs(r1 - r2); // Change
        gDiff = Math.abs(g1 - g2);
        bDiff = Math.abs(b1 - b2);
        result = (rDiff << 16) | (gDiff << 8) | bDiff;
        outImg.setRGB(j, i, result); // Set result
    }
}

Edit - August 1, 2014

You mentioned that if there is any difference between two pixels in their corresponding spots, we should set this colour to yellow (or purple / magenta). We can very easily do that. Simply calculate the added difference between all of the channels, and if it's not equal to 0, set the output colour to yellow (or purple / magenta). Else, just keep the colour the way it is. I'm also going to assume that you'll want to keep the pixels from image 1 the same if there is no difference. If you want to keep the pixels from image 2 to be the same, just change the variable in the code. In other words:

// Modified - Changed to int as pixels are ints
int diff;
int result; // Stores output pixel
for (int i = 0; i < height1; i++) {
    for (int j = 0; j < width1; j++) {
        int rgb1 = img1.getRGB(j, i);
        int rgb2 = img2.getRGB(j, i);
        int r1 = (rgb1 >> 16) & 0xff;
        int g1 = (rgb1 >> 8) & 0xff;
        int b1 = (rgb1) & 0xff;
        int r2 = (rgb2 >> 16) & 0xff;
        int g2 = (rgb2 >> 8) & 0xff;
        int b2 = (rgb2) & 0xff;
        diff = Math.abs(r1 - r2); // Change
        diff += Math.abs(g1 - g2);
        diff += Math.abs(b1 - b2);

        // Check for any differences
        if (diff != 0) {
           r1 = 255;  // If there is, set output to yellow
           g1 = 255;
           b1 = 0;
           /* // Use this for purple / magenta
           r1 = 255;
           g1 = 0;
           b1 = 255; */
        }

        // If there are no differences, r1, g1 and b1
        // will contain the original colours for image 1
        // If there is, then r1, g1, b1 will be set to yellow

        // Set output pixel
        result = (r1 << 16) | (g1 << 8) | b1;
        outImg.setRGB(j, i, result); // Set result
    }
}

You can try this

BufferedImage yourFile =

You can create a color model(see the link for example)

private static ColorModel createColorModel(int n) {

        // Create a simple color model with all values mapping to
        // a single shade of gray
        // nb. this could be improved by reusing the byte arrays

        byte[] r = new byte[16];
        byte[] g = new byte[16];
        byte[] b = new byte[16];

        for (int i = 0; i < r.length; i++) {
            r[i] = (byte) n;
            g[i] = (byte) n;
            b[i] = (byte) n;
        }
        return new IndexColorModel(4, 16, r, g, b);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM