Image Color
IPT Matlab
Write a program to convert an RGB image to CMYK
format and display the 4 color channels individually (17)
clear; clc; T = makecform('cmyk2srgb');
I = imread('peppers.png'); N = applycform(M, T);
S = makecform('srgb2cmyk'); subplot(251), imshow(I); title('RGB');
J = applycform(I, S); subplot(252), imshow(r); title('R');
r = I(:,:,1); g = I(:,:,2); b = I(:,:,3); subplot(253), imshow(g); title('G');
imwrite(J, 'test.tiff'); subplot(254), imshow(b); title('B');
pause (5); subplot(256), imshow(cmy); title('CMY');
imfinfo('test.tiff') subplot(257), imshow(c); title('C');
M = imread('test.tiff'); subplot(258), imshow(m); title('M');
c = M(:,:,1); m = M(:,:,2); y = subplot(259), imshow(y); title('Y');
M(:,:,3); k = M(:,:,4); subplot(2,5,10), imshow(k); title('K');
cmy = M(:,:,1:3); subplot(255), imshow(N); title('RGB recovered');
• The IPT function makecform creates a color transformation
structure for color space conversion defined by the specified
argument, in this case, srgb2cmyk option is used for RGB to CMYK
conversion.
• A reverse transformation of CMYK to RGB can be specified by the
argument cmyk2srgb. The IPT function applycform converts the color
values in the specified image to the specified color space defined by
the color transformation structure.
• The BM function pause pauses program execution for 5 seconds
to allow the TIFF file to be written down to disk.
• The BM function imfinfo can be used to verify that the TIFF file has a
color type of CMYK.
• Since MATLAB can only display a three channel color image, the first
three channels are used for displaying the CMYK image
Write a program to convert an RGB image to HSV
and Lab formats and display the channels individually. (18)
clear; clc; subplot(441), imshow(rgb1); title('RGB');
rgb1 = imread('peppers.png'); subplot(442), imshow(r, []); title('R');
r = rgb1(:,:,1); subplot(443), imshow(g, []); title('G');
g = rgb1(:,:,2); subplot(444), imshow(b, []); title('B');
b = rgb1(:,:,3);
subplot(445), imshow(hsv); title('HSV');
hsv = rgb2hsv(rgb1);
subplot(446), imshow(h, []); title('H');
h = hsv(:,:,1);
subplot(447), imshow(s, []); title('S');
s = hsv(:,:,2);
v = hsv(:,:,3); subplot(448), imshow(v, []); title('V');
lab = rgb2lab(rgb1); subplot(449), imshow(lab); title('LAB');
l = lab(:,:,1); subplot(4,4,10), imshow(l, []); title('L');
a = lab(:,:,2); subplot(4,4,11), imshow(a, []); title('a');
b = lab(:,:,3); subplot(4,4,12), imshow(b, []); title('b');
rgb2 = hsv2rgb(hsv); subplot(4,4,13), imshow(rgb2, []); title('rgb2');
rgb3 = lab2rgb(lab); subplot(4,4,14), imshow(rgb3, []); title('rgb3');
• The IPT functions rgb2lab and lab2rgb convert colors from RGB to
L*a*b* color spaces and vice versa.
• The BM functions rgb2hsv and hsv2rgb convert colors from RGB color
space to HSV color space and vice versa.
• The example shows an RGB image being converted to HSV and
L*a*b* color spaces and displays the individual channels.