views:

671

answers:

3

Hi,

I'm trying to convert an rgb image to the equivalent hsv one in C#. I found several algorithms to do the conversion but couldn't find how to save these values of an image after calculating it.

For example after the calculation h = 287, s = 0.5, v = 0.34 . Where should I save these values in the image file to convert it to the equivalent hsv image ?

+1  A: 

In general systems deal with images in RGB - if you're converting to HSV to do some processing in HSV space then what you want to do is convert the pixels to HSV, do your processing, convert BACK to RGB and save the final RGB values.

Converting an image from RGB to HSV without changing it at all would IMHO be fairly useless (you just end up with exactly the same image).

Aaron
+1  A: 

If you're interested in reusing the HSV values every time you need to process the image, you could write a binary file that holds them. That involves however duplication of your image in some form, but it may be useful if you've got multiple processes that need those values and due to a large image size, would like to avoid the conversion step. In general, however, you read the RGB values, convert them to HSV, process them, convert them back to RGB, and save the processed image.

If you want to store your HSV values in your image file and be able to display it as a "map" of these channels, you can probably think of it in terms of mapping the HSV values onto the RGB interval. RGB values are in the 0-255 interval each, while in HSV, Hue is in 0 - 360, S and V in 0.0 - 0.1. This means that you could store the S and V values in G and B by multiplying them with 255:

G = S * 255;
B = V * 255;

As for the H value, you must map it in the 0 - 255 space, so you multiply it with (255 / 360) and store it in the R component. So there it is, the last piece:

R = H * 255 / 360;
luvieere
I'm reading the RGB values and converting them to HSV but I want to save the image as it's HSV form. How can I do that ? For example if we consider the example I gave in the first post, where should I assign these values in order to get the image as it's HSV form ?
Can Doralp
The question you should be asking is if there is an image format that stores its pixel values as HSV triples - I personally have never heard of such a format. Perhaps you can tell me what image format do you use? Is it PNG, JPEG, GIF? If you use a common image format like these and many others, chances are you won't be able to store your HSV values directly in the image without you corrupting it and making it unreadable for any other program that expects it to be RGB .
luvieere
In fact, I've an hw to do and what's expected from me is loading an rgb image to the 1st picture box and select one of the color space conversion options(rgb -> hsv and hsv -> rgb is included) and show the converted image in the 2nd picture box. I' ve done all of them except rgb -> hsv and hsv -> rgb but if it isn't possible to save the image as HSV like you said why my instructor has added this option to the hw ? I'm really confused now...
Can Doralp
Could you paste here the exact words of your assignment? I think it'll help me understand what your instructor meant and what approach you could take.
luvieere
Here is my hw http://bit.ly/48e3U5
Can Doralp
I've looked at your homework, and indeed it's a bit confusing. Anyway, I'll edit my answer to include a possible solution.
luvieere
+2  A: 

Visit OpenCV documentation and take a close look inside cvCvtColor function. There you will see the "tricks" used in OpenCV to store HSV values. For example, to store 8 bits images: V gets 255 * V, S gets 255 * S and H gets H/2.

TH
Yes but where should I assign these values in order to get the image as it's hsv form ? For example when I'm converting an image to the CMY I'm doing C = 1-R, M = 1- G, Y = 1-B and R=C,G=M,B=Y so where should I assign the H,S,V values ?
Can Doralp
If I'm understanding your problem, that depends of the image representation on memory. For example, OpenCV can use three 2D arrays, one per channel. So, for pixel at row 10, column 20, you have something like: V[10][20] = max(R[10][20], G[10][20], B[10][20]), according the color conversion equation for V. The image representation on memory depends of the library you are using.
TH
This could help you: http://thsant.blogspot.com/search/label/opencv. Keep in your mind the following: monitors and (probably) the graphic environment of your system are able to show RGB images (because the monitor *is* a RGB device). Suppose you applied the color space transformation. Your image has values for the 1st channel, for the 2nd channel and for the 3rd channel. The system probably don't know if these channels are RGB, HSV or YCrCb channels. Most of systems will try to display the image as a RGB one, producing curious "wrong coloured" images.
TH