2014-09-04 57 views
0

我正在加載BufferedImage,並且我已切換到HSB色彩空間以進行一些色彩校正,然後我將返回到RGB色彩空間以顯示結果但似乎cjanges成HSB色彩空間沒有效果。 這裏是我的代碼:Color.HSBtoRGB不保留對HSB色彩空間的更改

import java.awt.Color; 
import java.awt.image.BufferedImage; 

public class ImageOperations { 

    public static BufferedImage Threshold(BufferedImage img,int requiredThresholdValue) { 

     int height = img.getHeight(); 
     int width = img.getWidth(); 
     BufferedImage finalThresholdImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB) ; 

     int red = 0; 
     int green = 0; 
     int blue = 0; 

     float hue = 0; 
     float sat = 0; 
     float bri = 0; 

     int r = 0; 
     int b = 0; 
     int g = 0; 



     for (int x = 0; x < width; x++) { 
//   System.out.println("Row: " + x); 
      try { 

       for (int y = 0; y < height; y++) { 

        //Get RGB values of pixels 
        int color = img.getRGB(x, y); 
        red = ImageOperations.getRed(color); 
        green = ImageOperations.getGreen(color); 
        blue = ImageOperations.getBlue(color); 

        //Convert to HSV color space 
        float[] hsb = new float[3]; 
        Color.RGBtoHSB((color>>16)&0xff, (color>>8)&0xff, color&0xff, hsb); 
        hue = hsb[0]; 
        sat = hsb[1]; 
        bri = hsb[2]; 

        //apply changes based on threshold value 
        if ((hue+sat+bri)/3 < (int) (requiredThresholdValue)){ 
         sat = sat -(50); 
         bri = bri +100; 

         int rgb = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]); 
         r = ImageOperations.getRed(rgb); 
         g = ImageOperations.getGreen(rgb); 
         b = ImageOperations.getBlue(rgb); 
         finalThresholdImage.setRGB(x,y,ImageOperations.mixColor(r,g,b)); 
        } 
        else { 
         finalThresholdImage.setRGB(x,y,ImageOperations.mixColor(255, 255,255)); 
        } 


       } 
      } catch (Exception e) { 
       e.getMessage(); 
      } 
     } 

     return finalThresholdImage; 
    } 

    private static int mixColor(int r, int g, int b) { 
     return r<<16|g<<8|b; 
    } 

    public static int getRed(int rgb) { 
     return (rgb & 0x00ff0000) >> 16; 
    } 

    public static int getGreen(int rgb) { 
     return (rgb & 0x0000ff00) >> 8; 
    } 

    public static int getBlue(int rgb) { 
     return (rgb & 0x000000ff) >> 0; 

    } 

} 

我使用Eclipse和它不給任何錯誤,有可能是一個語義錯誤,但我無法弄清楚。

回答

0

首先,你做的事:

sat = hsb[1]; 

然後:

sat = sat -(50); 

然後:

int rgb = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]); 

它幾乎一樣,如果你希望HSB [1]要在更新SAT更新? 總之,做:

int rgb = Color.HSBtoRGB(hue, sat, bri); 
+0

非常感謝,解決了它。 – leonardo 2014-09-04 09:20:53

+0

其實還有另外一個問題。變化現在有效果,但它們不是我所期望的。在「如果」的情況下,我說要淡化所有低於閾值的像素,但是我對圖像的所有像素都有影響,而且它們甚至沒有去飽和,實際上是相反的。我的色彩空間轉換之間可能存在問題嗎? – leonardo 2014-09-04 09:31:12

+0

不是線索。我第一次聽說過HSB。 – nablex 2014-09-04 10:05:52