Reputation: 107
I'm new at working with Matlab and I am trying to get the HSV values of an image, but I need the result to be an uint8. Currently, when I read in an image, I received the following matrix:
whos im
Name Size Bytes Class Attributes
im 120x100x3 36000 uint8
When I use the function rgb2hsv() i get:
hsv = rgb2hsv(im);
whos hsv
Name Size Bytes Class Attributes
hsv 120x100x3 288000 double
Is there any way to either convert this to a matrix of uint8s instead of doubles?
Upvotes: 2
Views: 1032
Reputation: 36710
To convert the result back to uint8, simply use hsv = im2uint8(rgb2hsv(im))
. It not only does the data type conversion, but also scales your image to the 0...255 range of an uint8.
When storing an image in double format, only the range 0...1 is used, while storing an image in any integer (or fixed point) format the full range between intmin
and intmax
is used. To convert the image back to the original range you have to use im2uint8
or do it manually, first multiplying by 255 and then converting the data type. I prefer im2uint8
because it is more robust, e.g. im2uint8(im2uint8(I))
returns a valid image. The previous is true for all colour spaces I know except HDR images.
I know that the documentation says it's for grey scale and rgb images only, but it's basically a data type conversion with automatic rescaling.
Upvotes: 3