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);
}
}
}
oh, please, don't ever try to loop over pixels for anything
then what would you advise? @berak
get rid of the loop.
split() your Mat into 3 channels
dunp() the S channel
Would I still be able to determine the HSV of the area of interest?
@berak I did what you said and the values aren't coming out right for the channels
if you want hsv, you'd have to convert your image to hsv first. (currently, it's all BGR)
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. @berakI pulled a darkGreen image I am reading that now.
The values it gives me are
H:60, S:255, V:255
@berakEven 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