簡體   English   中英

為Java中的緩沖圖像賦予顏色

[英]Give color to a buffered image in java

我有一個看起來像這樣的緩沖圖像。 在此處輸入圖片說明

我如何給它加顏色? 我是Java圖像處理的新手,將不勝感激。 這是生成此圖片的代碼示例。

   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;
}

嘗試更改要計算顏色之間差異的代碼,而不是將每個顏色通道都設置為相同並生成灰度,而是嘗試將每個顏色通道設置為其各自的差異,而不是將每種顏色的差異相加渠道。 換句話說,請嘗試以下操作:

// 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
    }
}

編輯-2014年8月1日

您提到,如果兩個像素在其對應的斑點之間存在任何差異,則應將此顏色設置為黃色(或紫色/洋紅色)。 我們可以很容易地做到這一點。 只需計算所有通道之間的相加差,如果不等於0,則將輸出顏色設置為黃色(或紫色/洋紅色)。 否則,保持顏色不變。 我還要假設如果沒有差異,您將希望圖像1中的像素保持相同。 如果要使圖像2中的像素保持相同,只需更改代碼中的變量即可。 換一種說法:

// 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
    }
}

你可以試試這個

BufferedImage yourFile =

您可以創建顏色模型(例如,請參閱鏈接)

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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM