Search code examples
javaopencvhsv

Are my channel separation calculations correct?


I have an HSV image and I am interested in a certain area of the image. I have gotten image selection to work and determine the area I am interested in but I don't think the way I separate my channels is correct. My channel values don't come close to their actual value .The portion of the image I am looking at is white in RGB scale. I know that in HSV that white has a saturation of about (0..20) and a value(230..255) but the numbers printed out by the program don't come close in that range. I get a high 40 low 50 for S and -93 mostly for V. Are my channels calculations correct?

public void splitChannels() {

    Mat firstImage = Imgcodecs.imread("firstImage.jpg");
    int width = 20;
    int height = 20;
    Rect roi = new Rect(120, 160, width, height);
    Mat smallImg = new Mat(firstImage, roi);
    int channels = smallImg.channels();
    System.out.println("small pixels:" + smallImg.total());
    System.out.println("channels:" + smallImg.channels());
    int totalBytes = (int)(smallImg.total() * smallImg.channels());
    byte buff[] = new byte[totalBytes];
    smallImg.get(0, 0, buff);

    for (int i=0; i< height; i++) {
        // stride is the number of bytes in a row of smallImg
        int stride = channels * width;
        for (int j=0; j<stride; j+=channels) {
        //I don't know if these channels calculations are correct.
            int h = buff[(i * stride) + j];
            int s = buff[(i * stride) + j + 1]; 
            int v = buff[(i * stride) + j + 2];

            // Do something with the hsv.
            System.out.println("s: "+ s + " v: " + v);

        } 
    }
}

Here is my image:

enter image description here

The region of interest is the upper left sticker in the center, they are all white though.

The HSV it program prints out for this is:

h: 99 s: 78 v: 57
h: 97 s: 76 v: 55
h: 101 s: 77 v: 57
h: 101 s: 77 v: 57
h: 103 s: 79 v: 59
h: 103 s: 79 v: 59
h: 103 s: 79 v: 59
h: 102 s: 78 v: 58
h: 100 s: 76 v: 58
h: 99 s: 75 v: 57
h: 98 s: 74 v: 56
h: 98 s: 74 v: 56
h: 98 s: 74 v: 54
h: 99 s: 75 v: 55

Solution

  • It appears that you are getting the correct values but they are RGB intensities coming from the raw bytes. They are coming out -1 -1 -1 because the Java byte data type is 8 bits long, however it is a signed data type, not unsigned.

    You need to convert these values from RGB to HSV. Try this: Here is the link to the definition of the conversion:

    RGBtoHSB:

    Converts the components of a color, as specified by the default RGB model, to an equivalent set of values for hue, saturation, and brightness that are the three components of the HSB model.

    public static float[] RGBtoHSB(int r,
                               int g,
                               int b,
                               float[] hsbvals)
    

    Usage:

    int r = ...
    int g = ... 
    int b = ...
    float[] hsbvals = new float[3];
    Color.RGBtoHSB(r,g,b,hsbvals)