How to Implement Color Space Conversion in Matlab? Converting Images from RGB Space to HSI, HSV, Lab, NTSC, and Other Color Spaces

Image color modes correspond to various color spaces, and the conversion and analysis of color spaces are crucial. This example demonstrates various color space conversions and related image processing operations. Estimated reading time: 5 minutes.

1. Code

%% rgbcubergbcube;rgbcube(10,10,10);
%% colorcloudrgb=imread('peppers.png');colorcloud(rgb,'rgb');pause;colorcloud(rgb,'lab');
%% RGB color channelsim1=imread('Fig0617.tif');im1=im2double(im1); [m,n,q]=size(im1);[R,G,B]=imsplit(im1);
figure,set(gcf,'outerposition',get(0,'screensize'));set(gcf,'NumberTitle','off','Name','Change Channels to Observe Image');subplot(2,2,1),imshow(im1),title('Original Image');subplot(2,2,2),imshow(cat(3,R*0.5,G,B)),title('R Channel Halved');subplot(2,2,3),imshow(cat(3,R,G*2,B)),title('G Channel Doubled');subplot(2,2,4),imshow(cat(3,R,G,zeros(m,n))),title('B Channel Set to 0');
%% RGB to HSI conversionim3=im1;[H,S,I]=imsplit(rgb2hsi(im3));
figure,set(gcf,'outerposition',get(0,'screensize')); % Maximize windowset(gcf,'NumberTitle','off','Name','HSI Channel Image'); % Window titlesubplot(2,2,1),imshow(im3),title('Original Image');subplot(2,2,2),imshow(H,[]),title('Hue Image H');subplot(2,2,3),imshow(S),title('Saturation Image S');subplot(2,2,4),imshow(I),title('Intensity Image I');
%% RGB to HSV conversionRGB=reshape(ones(64,1)*reshape(jet(64),1,192),[64,64,3]);HSV=rgb2hsv(RGB);[h,s,v]=imsplit(HSV);
figure,subplot(121),imshow(RGB);title("RGB")subplot(122),imshow(HSV);title("HSV")figure,montage({HSV,h,s,v},'bordersize',10);title("HSV,h,s,v")
%% RGB to Lab conversionrgb2lab([.2 .3 .4],'ColorSpace','adobe-rgb-1998'),rgb2lab([.2 .3 .4],'whitepoint','D50'),
I=imread('peppers.png');Lab=rgb2lab(I);figure,subplot(2,2,1),imshow(I),title('Original Image');subplot(2,2,2),imshow(Lab(:,:,1),[0 100]); title("Display L Component")subplot(2,2,3),imshow(Lab(:,:,2),[0 100]); title("Display a Component")subplot(2,2,4),imshow(Lab(:,:,3),[0 100]); title("Display b Component")
%% RGB to NTSCf=imread('Fig0604(a)(iris).tif');ntsc_image=rgb2ntsc(f);
figure,subplot(221),imshow(f),title("Original Image")subplot(222),imshow(ntsc_image(:,:,1)); title("Display Luminance Information")subplot(223),imshow(ntsc_image(:,:,2)); title("Display Chrominance 1 Information")subplot(224),imshow(ntsc_image(:,:,3)); title("Display Chrominance 2 Information")
%% RGB color image to grayscale, then to binary imageg=rgb2gray(f);g1=dither(g);
figure,imshowpair(g,g1,'montage');
%% RGB to indexed color[X1,map1]=rgb2ind(f,8,'nodither');[X2,map2]=rgb2ind(f,8,'dither');
figure,subplot(221),imshow(f),title("Original Image")subplot(222),imshow(X1,map1);title("nodither")subplot(223),imshow(X2,map2);title("dither")
%% imapprox: reduce the number of colors in indexed imageload mandrill;figure, image(X);colormap(map);axis off;axis image
[Y,newmap]=imapprox(X,map,16);figure, image(Y);colormap(newmap);axis off;axis image
%% HSI to RGBim1=imread('Fig0617.tif');[H,S,I]=imsplit(rgb2hsi(im1));[r,g,b]=imsplit(hsi2rgb(cat(3,H,S,I)));
figure,set(gcf,'outerposition',get(0,'screensize'));set(gcf,'NumberTitle','off','Name','Comparison of Original Image and HSI to RGB Channel Image');subplot(1,2,1),imshow(im1),title('Original Image');subplot(1,2,2),imshow(cat(3,r,g,b)),title('HSI to RGB Channel Image');

2. Analysis

This code includes: displaying the RGB cube using the rgbcube function; visualizing the color distribution of images using the colorcloud function; manipulating the RGB channels of images; converting images from RGB space to HSI, HSV, Lab, NTSC, and other color spaces; converting RGB images to grayscale, binary images, and indexed color images; reducing the number of colors in indexed images; and operations for converting HSI to RGB.

(1) RGB Cube Display

The rgbcube function is used to create and display the RGB color cube.

Calling rgbcube(10,10,10) specifies the viewpoint for observing the RGB color space as (10,10,10).

How to Implement Color Space Conversion in Matlab? Converting Images from RGB Space to HSI, HSV, Lab, NTSC, and Other Color Spaces

(2) Color Cloud Visualization

The colorcloud function is used to display the color distribution of images in a three-dimensional color cloud.

colorcloud(rgb,’rgb’) displays the color distribution of the image in the RGB color space, allowing users to intuitively see the distribution of various colors in the RGB space.

How to Implement Color Space Conversion in Matlab? Converting Images from RGB Space to HSI, HSV, Lab, NTSC, and Other Color Spaces

colorcloud(rgb,’lab’) displays the color distribution of the same image in the Lab color space, facilitating the comparison of color distribution characteristics under different color spaces.

How to Implement Color Space Conversion in Matlab? Converting Images from RGB Space to HSI, HSV, Lab, NTSC, and Other Color Spaces

(3) RGB Color Channel Operations

The imsplit function splits the multi-channel RGB image im1 into separate R, G, and B channels, stored in variables R, G, and B respectively.

The cat function concatenates arrays along the third dimension (channel dimension). By performing different operations on the R, G, and B channels (such as halving the R channel, doubling the G channel, and setting the B channel to 0), and using imshow to display the processed images, one can observe the impact of different channel values on the overall color of the image.

How to Implement Color Space Conversion in Matlab? Converting Images from RGB Space to HSI, HSV, Lab, NTSC, and Other Color Spaces

(4) RGB to HSI Mode Conversion

The rgb2hsi function converts the RGB image im3 to the HSI color space, and the imsplit function splits the converted HSI image into three channels: H (hue), S (saturation), and I (intensity).

How to Implement Color Space Conversion in Matlab? Converting Images from RGB Space to HSI, HSV, Lab, NTSC, and Other Color Spaces

(5) RGB to HSV Conversion

The jet function generates a color mapping matrix with 64 rows, and the reshape function reshapes the matrix, ultimately generating a 64x64x3 RGB image.

How to Implement Color Space Conversion in Matlab? Converting Images from RGB Space to HSI, HSV, Lab, NTSC, and Other Color Spaces

The rgb2hsv function converts the RGB image RGB to the HSV color space, stored in HSV. The imsplit function splits the HSV image into three channels: h (hue), s (saturation), and v (brightness).

How to Implement Color Space Conversion in Matlab? Converting Images from RGB Space to HSI, HSV, Lab, NTSC, and Other Color Spaces

(6) RGB to Lab Conversion

Two calls to the rgb2lab function: the first converts the RGB color value [.2 .3 .4] to the Lab color space, specifying the color space as adobe-rgb-1998; the second similarly converts the RGB color value, specifying the white point as D50, displaying the conversion results under different parameter settings.

How to Implement Color Space Conversion in Matlab? Converting Images from RGB Space to HSI, HSV, Lab, NTSC, and Other Color Spaces

The rgb2lab function converts the image to the Lab color space, stored in Lab.

How to Implement Color Space Conversion in Matlab? Converting Images from RGB Space to HSI, HSV, Lab, NTSC, and Other Color Spaces

(7) RGB to NTSC Conversion

The rgb2ntsc function converts it to the NTSC color space. In the NTSC color space, the three components are: luminance information (Y), which reflects the brightness of the image; chrominance 1 (I), which indicates the color difference in the orange-cyan direction; and chrominance 2 (Q), which indicates the color difference in the purple-yellow-green direction.

How to Implement Color Space Conversion in Matlab? Converting Images from RGB Space to HSI, HSV, Lab, NTSC, and Other Color Spaces

(8) RGB Color Image to Grayscale, then to Binary Image

The rgb2gray function converts it to a grayscale image; the dither function applies dithering to the grayscale image g, generating a binary image.

How to Implement Color Space Conversion in Matlab? Converting Images from RGB Space to HSI, HSV, Lab, NTSC, and Other Color Spaces

(9) RGB to Indexed Color

The rgb2ind function converts the RGB image to an indexed color image. The first call specifies the number of colors as 8 without dithering, resulting in the indexed image X1 and color mapping table map1; the second call also specifies the number of colors as 8 but with dithering, resulting in the indexed image X2 and color mapping table map2.

How to Implement Color Space Conversion in Matlab? Converting Images from RGB Space to HSI, HSV, Lab, NTSC, and Other Color Spaces

(10) Reducing the Number of Colors in Indexed Images

[Y,newmap]=imapprox(X,map,16) reduces the number of colors in the indexed image X to 16, resulting in a new indexed image Y and a new color mapping table newmap.

How to Implement Color Space Conversion in Matlab? Converting Images from RGB Space to HSI, HSV, Lab, NTSC, and Other Color Spaces

(11) HSI to RGB

The cat function combines the previously obtained H, S, and I channels into an HSI image, and the hsi2rgb function converts it to an RGB image. The imsplit function splits the converted RGB image into r, g, and b channels.

How to Implement Color Space Conversion in Matlab? Converting Images from RGB Space to HSI, HSV, Lab, NTSC, and Other Color Spaces

3. Summary of Matlab Functions

  • rgbcube: Displays the RGB color cube.

  • colorcloud: Displays color distribution in a three-dimensional cloud.

  • imsplit: Splits the image into individual channels.

  • cat: Concatenates arrays along the specified dimension.

  • rgb2hsi: Converts RGB images to HSI space.

  • rgb2hsv: Converts RGB images to HSV space.

  • rgb2lab: Converts RGB colors or images to Lab space.

  • rgb2ntsc: Converts RGB images to NTSC space.

  • rgb2gray: Converts RGB images to grayscale images.

  • dither: Applies dithering to grayscale images to generate binary images.

  • rgb2ind: Converts RGB images to indexed color images.

  • imapprox: Reduces the number of colors in indexed images.

This code primarily functions to perform color space conversions and related image processing operations, helping to understand the characteristics of different color spaces and their inter-conversion relationships.

Color space conversion is often used to assist in color adjustment and special effects addition, and in the visual field for feature extraction; in video encoding and printing, it can enhance efficiency and ensure color reproduction. However, color information loss may occur during conversion. Different color spaces have different gamut ranges, and during conversion, gamut mismatches may affect color presentation.

If you need the code, please send a message.

Leave a Comment