开发者

Converting an rgb image to hsv

开发者 https://www.devze.com 2022-12-10 14:31 出处:网络
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.

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 aft开发者_运维百科er 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 ?


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


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号