Color.HSBtoRGB不保持对HSB颜色空间的更改

时间:2014-09-04 08:56:50

标签: java image-processing color-space

我正在加载一个BufferedImage并且我已经切换到HSB颜色空间进行一些颜色校正,然后我将回到RGB颜色空间来显示结果,但似乎是进入HSB颜色空间的cjanges不是有效果。 这是我的代码:

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而且它没有出现任何错误,可能存在语义错误,但我无法弄明白。

1 个答案:

答案 0 :(得分:0)

首先你做:

sat = hsb[1];

然后:

sat = sat -(50);

然后:

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

这几乎就像你期望在更新sat时更新hsb [1]一样? 总之,做:

int rgb = Color.HSBtoRGB(hue, sat, bri);
相关问题