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

asked 2017-02-23 11:59:59 -0600

thatc0der m8 gravatar image

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

berak gravatar image

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);

        } 
    }
}
edit retag flag offensive close merge delete

Comments

oh, please, don't ever try to loop over pixels for anything

berak gravatar imageberak ( 2017-02-23 13:16:23 -0600 )edit

then what would you advise? @berak

thatc0der m8 gravatar imagethatc0der m8 ( 2017-02-23 13:30:58 -0600 )edit

get rid of the loop.

split() your Mat into 3 channels

dunp() the S channel

berak gravatar imageberak ( 2017-02-23 13:33:29 -0600 )edit

Would I still be able to determine the HSV of the area of interest?

thatc0der m8 gravatar imagethatc0der m8 ( 2017-02-23 13:40:01 -0600 )edit

@berak I did what you said and the values aren't coming out right for the channels

      List <Mat> image = new ArrayList<>(3);
        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);

        Core.split(smallImg,image);
        Mat h = image.get(0);
        Mat s = image.get(1);
        Mat v = image.get(2);


        System.out.println("Hue: \n" + h.dump());
thatc0der m8 gravatar imagethatc0der m8 ( 2017-02-23 15:27:02 -0600 )edit

if you want hsv, you'd have to convert your image to hsv first. (currently, it's all BGR)

berak gravatar imageberak ( 2017-02-24 06:19:03 -0600 )edit

I do convert the image to HSV before it is read, but when I do image.get(0) the dump values look as though they are only for BGR, the numbers it gives me don't follow HSV. @berak

thatc0der m8 gravatar imagethatc0der m8 ( 2017-02-24 06:59:24 -0600 )edit

I pulled a darkGreen image I am reading that now.

public void splitChannels() {

        Mat firstImage = Imgcodecs.imread("darkGreen.jpg");

        int width = 20;
        int height = 20;
        Rect roi = new Rect(120, 160, width, height);
        Mat smallImg = new Mat(firstImage, roi);
        Imgproc.cvtColor(smallImg,smallImg,Imgproc.COLOR_BGR2HSV);
        List <Mat> image = new ArrayList<>(3);

        Core.split(smallImg,image);
        Mat h = image.get(0);
        Mat s = image.get(1);
        Mat v = image.get(2);


        System.out.println("Hue: \n" + h.dump());

        System.out.println("Saturation: \n" + s.dump());

The values it gives me are H:60, S:255, V:255@berak

thatc0der m8 gravatar imagethatc0der m8 ( 2017-02-24 07:11:52 -0600 )edit

Even after converting the image color the values still put out are RGB and not HSV, the values should be H:132, S: 76, V: 100

thatc0der m8 gravatar imagethatc0der m8 ( 2017-02-24 07:14:05 -0600 )edit