Ask Your Question

Revision history [back]

Is the way I calculate the the HSV color channels correct?

I have an HSV image and am interested in a certain part of that image and I want to determine what the Hue Saturation and Value are of that area. I have gotten image selection to work and determine the area I am interested in but I don't think the way I calculate the channels are correct. The reason being that I the portion of the image I am looking at is white in RGB scale and 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. Is my calculation 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);

        } 
    }
}
click to hide/show revision 2
retagged

updated 2017-02-23 13:15:23 -0600

berak gravatar image

Is the way I calculate the the HSV color channels correct?

I have an HSV image and am interested in a certain part of that image and I want to determine what the Hue Saturation and Value are of that area. I have gotten image selection to work and determine the area I am interested in but I don't think the way I calculate the channels are correct. The reason being that I the portion of the image I am looking at is white in RGB scale and 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. Is my calculation 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);

        } 
    }
}