To achieve rgb to hsv conversion in matlab we can see that MATLAB Image processing toolbox provides functions for converting between different color spaces. Like to convert from RGB color space into HSV color space we have function called rgb2hsv() we need to pass RGB Image to this function and this function will return a HSV Image. Value of HSV will be between 0 and 1.

If we want to change or adjust Hue of the Image to correctly perceive some color adjustment with some factor, then after rgb to hsv conversion in matlab we can simply multiply Hue matrix with some factor and then reform the Image and finally make sure the resultant is not exceeding value from 0 and 1. We can then reconvert the Image back to RGB Color space by simply calling the function hsv2rgb() and passing the new HSV image to this function and will get back the hue adjusted RGB Image Like follows.

Understanding HSV Color Space

According to Wikipedia we can see the definition of HSV color space as this color space was designed to explain how human perceive colors. Here is quote form the Wikipedia

HSV (hue, saturation, value) are alternative representations of the RGB color model, designed in the 1970s by computer graphics researchers to more closely align with the way human vision perceives color-making attributes. In these models, colors of each hue are arranged in a radial slice, around a central axis of neutral colors which ranges from black at the bottom to white at the top.

According to Wikipedia

basically the HSV color wheel goes from RED color to back to RED color. By traversing orange, yellow, green, cyan, blue, magenta colors.

Example Code

First of all we need to read image and we are using the peppers.png image which comes along with Image processing toolbox from MATLAB. Then we convert the image int hsv color domain. After that, we parse all 3 matrix seperately and then change the hue with factor 1.5 (you can change this). Then we make sure to lie between 0 and 1 boundery by calling mod function.

clear all; close all; clc;
figure,imshow(x),title('original RGB Image');

% Convert to HSV
hsv_x = rgb2hsv(x);
figure,imshow(hsv_x),title('HSV version of Peppers Image');

%Change  the Hue 
h_x = hsv_x(:,:,1);
h_x_new = mod(h_x*1.5,1);  %to make sure range 

%Regenerate HSV Image 
new_hsv_x = cat(3,h_x_new,hsv_x(:,:,2),hsv_x(:,:,3));

% Convert HSV back to RGB 
new_x = hsv2rgb(new_hsv_x);

%finally display modified hue image
figure,imshow(new_x);title('modified Image')Code language: JavaScript (javascript)


By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.