Color.HSBtoRGB不保留对HSB色彩空间的更改

问题描述:

我正在加载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和它不给任何错误,有可能是一个语义错误,但我无法弄清楚。

首先,你做的事:

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