cuber cuber -4 years ago 140
Java Question

RGB to HSV values not being shown

I am trying to convert RGB values of an image and get the HSV values of it. I am trying to do this without using

Color.RGBtoHSB
because I don't like the float values I want the numbers to be in the range of 0-255. When I run this program my conversion algorithm prints out nothing even though I ask it to print out values.

public void splitChannels() {

Mat firstImage = Imgcodecs.imread("darkGreen.jpg");
Imgproc.cvtColor(firstImage, firstImage, Imgproc.COLOR_BGR2RGB);
int width = 20;
int height = 20;
Rect roi = new Rect(100,100, width, height);
Mat smallImg = new Mat(firstImage, roi);

Imgproc.cvtColor(smallImg,smallImg,Imgproc.COLOR_BGR2RGB);
// 3 channels in smallImg
int 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) {
int r = buff[(i * stride) + j];
int g = buff[(i * stride) + j + 1];
int b = buff[(i * stride) + j + 2];

RGBtoHSV(r, g, b);

}
}
}

private int[] RGBtoHSV(int r, int g, int b){
int computedH = 0;
int computedS = 0;
int computedV = 0;
int[] HSVarr = new int[3];


HSVarr[0] = computedH;
HSVarr[1] = computedS;
HSVarr[2] = computedV;
if(r< 0 || g< 0 || b< 0 || r> 255 || g>255 || b> 255){
System.err.println("RGB values must be in range 0 to 255");
}

r=r/255; g=g/255; b=b/255;
int minRGB = Math.min(r, Math.min(g, b));
int maxRGB = Math.max(r, Math.min(g, b));

// Black-gray-white
if(minRGB==maxRGB){
computedV = minRGB;
return HSVarr;
}

int d = (r==minRGB) ? g-b : ((b==minRGB) ? r-g : b-r);
int h = (r==minRGB) ? 3 : ((b==minRGB) ? 1 : 5);
computedH = 60*(h - d/(maxRGB - minRGB));
computedS = (maxRGB = minRGB)/maxRGB;
computedV = maxRGB;

System.out.println("H: " + computedH + " V: "+ computedS +" S: " + computedV);
return HSVarr;
}

Answer Source

I am trying to convert RGB values of an image and get the HSV values of it. I am trying to do this without using Color.RGBtoHSB because I don't like the float values.

Create a wrapper method for the Color.RGBtoHSB(...) method to convert the float values to the appropriate int values.

Something like:

import java.awt.*;

public class Main
{
    public static void main(String[] args) throws Exception
    {
        int[] hsb = RGBtoHSB(0, 0, 255);

        for (int value: hsb)
            System.out.println( value );
    }

    public static int[] RGBtoHSB(int red, int green, int blue)
    {
        float[] hsbFloat = Color.RGBtoHSB(red, green, blue, null);
        int[] hsbInt = new int[3];

        hsbInt[0] = Math.round( hsbFloat[0] * 360 );
        hsbInt[1] = Math.round( hsbFloat[1] * 100 );
        hsbInt[2] = Math.round( hsbFloat[2] * 100 );

        return hsbInt;
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download