I've found a method on the Internet to convert RGB values to HSV values.
Unfortunately, when the values are R=G=B, I'm getting a NaN, because of the 0/0 operation.
Do you know if there is an implemented method for this conversion in Java, or what do I have to do when I get the 0/0 division to get the right value of the HSV?
Here comes my method, adapted from some code on the Internet:
public static double[] RGBtoHSV(double r, double g, double b){
double h, s, v;
double min, max, delta;
min = Math.min(Math.min(r, g), b);
max = Math.max(Math.max(r, g), b);
// V
v = max;
delta = max - min;
// S
if( max != 0 )
s = delta / max;
else {
s = 0;
h = -1;
return new double[]{h,s,v};
}
// H
if( r == max )
h = ( g - b ) / delta; // between yellow & magenta
else if( g == max )
h = 2 + ( b - r ) / delta; // between cyan & yellow
else
h = 4 + ( r - g ) / delta; // between magenta & cyan
h *= 60; // degrees
if( h < 0 )
h += 360;
return new double[]{h,s,v};
}
I'm pretty sure what you want is RGBtoHSB
int r = ...
int g = ...
int b = ...
float[] hsv = new float[3];
Color.RGBtoHSB(r,g,b,hsv)
//hsv contains the desired values
Related
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;
}
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;
}
}
Hi I want convert from rgb to hsv and I have been following the algorithm from easyRGB.com. But doesn't work it show more red than normal. I rewrite the same algorithm a few time and revised, but I can't find the error. Any idea? There is the algorithm.
public static double[] RGB2HSV(double[] tmp){
double R = tmp[0] / 255.0;
double G = tmp[1] / 255.0;
double B = tmp[2] / 255.0;
double min = Math.min(Math.min(R, G), B);
double max = Math.max(Math.max(R, G), B);
double delta = max - min;
double H = max;
double S = max;
double V = max;
if(delta == 0){
H = 0;
S = 0;
}else{
S = delta / max;
double delR = ( ( ( max - R ) / 6 ) + ( delta / 2 ) ) / delta;
double delG = ( ( ( max - G ) / 6 ) + ( delta / 2 ) ) / delta;
double delB = ( ( ( max - B ) / 6 ) + ( delta / 2 ) ) / delta;
if(R == max){
H = delB - delG;
}else if(G == max){
H = (1/3) + delR - delB;
}else if(B == max){
H = (2/3) + delG - delR;
}
if(H < 0) H += 1;
if(H > 1) H -= 1;
}
double[] hsv = new double[3];
hsv[0] = H;
hsv[1] = S;
hsv[2] = V;
return hsv;
}
The values of 1/3 and (2/3) are 0, because you are operating with two integers, so the result is the integer too.
Use 1.0 / 3.0 and 2.0 / 3.0 instead.
I am trying to implement a simple RGB to HSB routine, I've closely followed instruction from wikipedia reference article.
The code I have written is:
#include <iostream>
#include <cmath>
struct RGB { float red, green, blue; };
struct HSB { float hue, saturation, brightness; }; // aka HSV
// https://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB
static void HSBToRGB(HSB const & hsb, RGB & rgb )
{
/*
* Given a color with hue H ∈ [0°, 360°), saturation SHSV ∈ [0, 1], and value
* V ∈ [0, 1], we first find chroma:
*/
const float H = hsb.hue;
const float S_HSV = hsb.saturation;
const float V = hsb.brightness;
const float C = V * S_HSV;
/*
* Then we can find a point (R1, G1, B1) along the bottom three faces of the RGB
* cube, with the same hue and chroma as our color (using the intermediate value X
* for the second largest component of this color):
*/
const float H_prime = H / 60.;
const float X = C * ( 1 - std::abs( (int)H_prime % 2 - 1) );
float R1, G1, B1;
if( isnan( H ) )
{
R1 = G1 = B1 = 0;
}
else if( 0 <= H_prime && H_prime < 1 )
{
R1 = C; G1 = X; B1 = 0;
}
else if( 1 <= H_prime && H_prime < 2 )
{
R1 = X; G1 = C; B1 = 0;
}
else if( 2 <= H_prime && H_prime < 3 )
{
R1 = 0; G1 = C; B1 = X;
}
else if( 3 <= H_prime && H_prime < 4 )
{
R1 = 0; G1 = X; B1 = C;
}
else if( 4 <= H_prime && H_prime < 5 )
{
R1 = X; G1 = 0; B1 = C;
}
else if( 5 <= H_prime && H_prime < 6 )
{
R1 = C; G1 = 0; B1 = X;
}
/*
* Finally, we can find R, G, and B by adding the same amount to each component,
* to match value:
*/
const float m = V - C;
rgb.red = R1 + m;
rgb.green = G1 + m;
rgb.blue = B1 + m;
}
int main()
{
HSB const hsb = { 251.1f, 0.887f, 0.918f };
RGB rgb = { 0.255f , 0.104f , 0.918f };
std::cout << "Reference: " << rgb.red << "," << rgb.green << "," << rgb.blue << std::endl;
HSBToRGB(hsb, rgb);
std::cout << "Computed: " << rgb.red << "," << rgb.green << "," << rgb.blue << std::endl;
return 0;
}
When compiled and run on linux debian/jessie amd64 here is what I get:
$ ./ref
Reference: 0.255,0.104,0.918
Computed: 0.103734,0.103734,0.918
Now if you lookup the correct reference values (same article, line 11 in the table) it should have been the same. What I do not understand is the large difference value for Hue (0.255 != 0.103734).
I've been double checking my implementation with what the wikipedia article describe and could not spot any difference.
Using java as reference, I could check that the RGB values should be correct, eg:
import java.awt.Color;
public class HSBToRGBExample {
public static void main(String[] args) {
float hue = 251.1f / 360;
float saturation = 0.887f;
float brightness = 0.918f;
int rgb = Color.HSBtoRGB(hue, saturation, brightness);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
System.out.println((float)red / 255 + " " + (float)green / 255 + " " + (float)blue / 255);
}
}
Is there a typo in the wikipedia article ? Or what is missing in my implementation ?
You want the floating point modulus, but you're casting to an int (possibly to silence the compiler's complaints that % only works with integers).
In C++ it would be
std::abs(std::fmod(H_prime, 2) - 1);
(You will still see discrepancies, due to floating point imprecision.)
I am trying to implement window level functionality( To apply bone, brain, lung etc on CT) for DICOM images in my application and implemented formula as per the DICOM specification.
I am changing pixel values based on below formula and creating a new image, but images are becoming blank. What am doing wrong and is this correct way to do this. Please help :(:( Thanks
BufferedImage image = input image;
double w = 2500; // Window width
double c = 500; // window Center
double ymin = 0;
double ymax = 255;
double x = 0;
double y = 0;
double slope = dicomObject.get(Tag.RescaleSlope).getFloat(true);
double intercept = dicomObject.get(Tag.RescaleIntercept).getFloat(true);
int width = image.getWidth();
int height = image.getHeight();
double val = c - 0.5 - (w - 1) / 2;
double val2 = c - 0.5 + (w - 1) / 2;
for (int m = 0; m < height; m++) {
for (int n = 0; n < width; n++) {
int rgb = image.getRGB(n, m);
int valrgb = image.getRGB(n, m);
int a = (0xff000000 & valrgb) >>> 24;
int r = (0x00ff0000 & valrgb) >> 16;
int g = (0x0000ff00 & valrgb) >> 8;
int b = (0x000000ff & valrgb);
x = a + r + g + b;
if (x <= val)
y = ymin;
else if (x > val2)
y = ymax;
else {
y = ((x - (c - 0.5)) / (w - 1) + 0.5) * (ymax - ymin)+ ymin;
}
y = y * slope + intercept;
rgb = (int) y;
image.setRGB(n, m, rgb);
}
}
String filePath = "out put fileName";
ImageIO.write(image, "jpeg", new File(filePath));
First of all whats in your BufferedImage image ?
There are three steps you want to take from raw (decopressed) pixel data:
Get stored values - apply BitsAllocated, BitsStored, HighBit transformation. (I guess you image already passed that level)
Get modality values - thats your Slope, Intercept transformation. Ofter this transformation, your data will be in Hounsfield Units for CT.
Then you apply WW/WL (Value Of Interest) transformation, which will transform this window of walues into grayscale color space.
EDIT:
You've got to tell me where did you get "input image" from? After decompression pixel data should be in a byte array of size byte[width*height*2] (for CT Image BitsAllocated is always 16, thus *2). You can get stored values like this:
ushort code = (ushort)((pixel[0] + (pixel[1] << 8)) & (ushort)((1<<bitsStored) - 1));
int value = TwosComplementDecode(code);
I am trying to find the color that most suits a loaded image and apply it in the background. To adapt to the image and make the UI feel more natural.
i have so far found 2 schemes :
1> averaging the pixels(Code Below) :
final Color acclimatizeAverage(BufferedImage img) {
long avgr = 0, avgb = 0, avgg = 0;
for (int i = 0; i < img.getWidth(); i++) {
for (int j = 0; j < img.getHeight(); j++) {
Color c = new Color(img.getRGB(i, j));
avgr += c.getRed(); avgb += c.getBlue(); avgg += c.getGreen();
}
}
avgr = (avgr/(img.getHeight()*img.getWidth()));
avgg = (avgg/(img.getHeight()*img.getWidth()));
avgb = (avgb/(img.getHeight()*img.getWidth()));
Color c = new Color((int)avgr, (int)avgg, (int)avgb);
return c;
}
2> Grouping the pixels into fixed bins of Colors(Code Below) :
Map<Color, Integer> createBins() {
Map<Color, Integer> bins = new HashMap<>();
bins.put(Color.red, 0);
bins.put(Color.magenta, 0);
bins.put(Color.orange, 0);
bins.put(Color.PINK, 0);
bins.put(Color.yellow, 0);
bins.put(Color.LIGHT_GRAY, 0);
bins.put(Color.GREEN, 0);
bins.put(Color.GRAY, 0);
bins.put(Color.DARK_GRAY, 0);
bins.put(Color.CYAN, 0);
bins.put(Color.BLUE, 0);
bins.put(Color.BLACK, 0);
return bins;
}
int compare(Color a, Color b) {
return (int)Math.sqrt((a.getRed() - b.getRed())*(a.getRed() - b.getRed())
+ (a.getBlue() - b.getBlue())*(a.getBlue() - b.getBlue())
+ (a.getGreen()- b.getGreen())*(a.getGreen()- b.getGreen()));
}
BufferedImage acclimatizeGrouping(BufferedImage img) {
Map<Color, Integer> bins = createBins();
for (int i = 0; i < img.getWidth(); i++) {
int min = Integer.MAX_VALUE; Color minC = null;
for (int j = 0; j < img.getHeight(); j++) {
Color c = new Color(img.getRGB(i, j));
for (Map.Entry<Color, Integer> entry : bins.entrySet()) {
Integer integer = compare(entry.getKey(), c);
if(integer < min) {
min = integer;
minC = entry.getKey();
}
}
bins.put(minC, bins.get(minC)+1);
}
}
int max = -1, n = 1; Color c = null;
for (Map.Entry<Color, Integer> entry : bins.entrySet()) {
Integer integer = entry.getValue();
if(integer > max) {
max = integer;
c = entry.getKey();
}
}
return c;
}
But the grouping is producing weird results....
left side is the Color produced as a result of grouping and right side is image
Why is it producing such results ???
averaing is producing more correct results :
I think the problem is that RGB is not human euclidean space. You use euclidean distance to compare colors, but it is not good for human color sense. See this link for more information.
EDIT: More precise, you should use this algorithm:
typedef struct {
unsigned char r, g, b;
} RGB;
double ColourDistance(RGB e1, RGB e2)
{
long rmean = ( (long)e1.r + (long)e2.r ) / 2;
long r = (long)e1.r - (long)e2.r;
long g = (long)e1.g - (long)e2.g;
long b = (long)e1.b - (long)e2.b;
return sqrt((((512+rmean)*r*r)>>8) + 4*g*g + (((767-rmean)*b*b)>>8));
}
This issue is, your compare(Color a, Color b) method is not implemented correctly and can use some basic refactoring using the Math.pow() method.
The basic formula to find similar colors programatically is
((r2 - r1)2 + (g2 - g1)2 + (b2 - b1)2)1/2
Applied to Java, that results in the modified compare(Color a, Color b)
int compare(Color a, Color b){
return Math.sqrt(( Math.pow( b.getRed() - a.getRed() )
+ ( Math.pow( b.getGreen() - a.getGreen() )
+ ( Math.pow( b.getBlue() - a.getBlue() ));
}