Chapter 5 Color Image Processing
- Representation of Color Image in MATLAB
- RGB image
- index image
- Functions for working with RGB and indexed images
- color space conversion
- NTSC color space
- YCbCr color space
- CMY and CMYK color spaces
- HSI color space
- Device-independent color space
- Color Image Processing Fundamentals
- color transform
- Spatial filtering of color images
- Color image smoothing
- Color image sharpening
- Processing directly in RGB vector space
- Colored edge detection using gradients
- Image segmentation in RGB vector space
1. Representation of color images in MATLAB
RGB image
An RGB image is an MxNx3 group of colored pixels. Think of it as a "stack" of three grayscale images.
- Stacked images
rgb_image = cat(3, fR, fG, fB)
Extraction of three component images
fR = rgb_image(:, :, 1);
fG = rgb_image(:, :, 2);
fB = rgb_image(:, :, 3);
>> fR = f(:, :, 1);
>> fG = f(:, :, 2);
>> fB = f(:, :, 3);
>> subplot(221), imshow(f);
>> subplot(222), imshow(fR);
>> subplot(223), imshow(fG);
>> subplot(224), imshow(fB);
- The custom function rgbcube
can view the color cube from any angle
rgbcube (vx, you, vz)
function rgbcube(vx, vy, vz)
vertices_matrix = [0 0 0; 0 0 1; 0 1 0; 0 1 1; 1 0 0; 1 0 1; 1 1 0; 1 1 1];
faces_matrix = [1 5 6 2; 1 3 7 5; 1 2 4 3; 2 4 8 6; 3 7 8 4; 5 6 8 7];
colors = vertices_matrix;
patch('Vertices', vertices_matrix, 'Faces', faces_matrix, 'FaceVertexCData', colors, 'FaceColor', 'interp', 'EdgeAlpha', 0)
if nargin == 0
vx = 10;
vy = 10;
vz = 4;
elseif nargin ~= 3
error('Wrong number of inputs.')
end
axis off
view([vx, vy, vz])
axis square
index image
The index plot has two components: an integer array matrix X and a colormap matrix map. In my own understanding, it seems that the RGB image is stored in two parts. Each line of the map specifies the red, green, and blue components of a single color.orimshow(X, map)
There are more applications for indexed images later. - Specify the background color of the imageimage(X);
colormap(map);
>> whitebg('g');
>> whitebg('green');
>> whitebg([0 1 0]);
Functions for working with RGB and indexed images
- "Dither" - The dither function is
used for grayscale and color images. This technique often gives a visual impression of tonal changes on a printed page consisting of dots.
g = dither(gray_image)
g is the dithered binary image (logical class)
>> bw = dither(g);
>> subplot(121), imshow(g);
>> subplot(122), imshow(bw);
Functions for working with indexed images
- Process grayscale images into indexed images
[X, map] = gray2ind(gray_image, n)
- Process grayscale images into indexed images
Functions can display integer data matrices and colormap matrices
>> [X, map] = gray2ind(g);
>> subplot(131), imshow(g);
>> subplot(132), imshow(X);
>> subplot(133), imshow(map);
The same is true for processing color images as indexed images.
[X, map] = rgb2ind(rgb_image, n, dither_option)
dither_option has two values: 'dither' (default), which achieves better color resolution at the expense of spatial resolution; 'nodither' maps each color in the original image to the closest color in the new map, not Perform dithering.
>> [X1, map1] = rgb2ind(g, 4, 'nodither');
>> imshow(X1, map1);
In this way, the number of 4 colors in the map can be clearly counted from the figure.
- Integer data matrix and color map matrix are integrated into grayscale or color images
rgb_ image = ind2rgb(X, map)
- Convert RGB image to grayscale image
gray_image = rgb2gray(rgb_image)
>> f1 = rgb2gray(f);
>> imshow(f1);
>> subplot(121), imshow(f);
>> subplot(122), imshow(f1);
- Summarize using functions
>> [X1, map1] = rgb2ind(g, 8, 'nodither');
>> subplot(231),imshow(g);
>> subplot(232),imshow(X1, map1);
>> [X2, map2] = rgb2ind(g, 8, 'dither');
>> subplot(233),imshow(X2, map2);
>> g1 = rgb2gray(g);
>> subplot(234),imshow(g1);
>> g2 = dither(g1);
>> subplot(235),imshow(g2);
2. Color space conversion
NTSC color space
In the NTSC format, image data consists of three components: luminance (Y), hue (I), and saturation (Q).
Y: Luminance, the brightness of the color is the brightness, which is actually the gray value of the image (Gray value); I and Q carry color information, the In-phase is from orange to cyan, and the Quadrature-phase is from purple to yellow-green.
mutual conversion process
yiq_image = rgb2ntsc(rgb_image)
rgb_image = ntsc2rgb(yiq_image)
>> y = rgb2ntsc(g);
>> subplot(131), imshow(g);
>> subplot(132), imshow(y);
>> y = ntsc2rgb(y);
>> subplot(133), imshow(y);
YCbCr color space
Cb is the difference between the blue component and the reference value, and Cr is the difference between the red component and the reference value.
ycbcr_image = rgb2ycbcr(rgb_image)
rgb_image = ycbcr2rgb(ycbcr_image)
>> y = rgb2ycbcr(f);
>> subplot(131), imshow(f);
>> subplot(132), imshow(y);
>> y = ycbcr2rgb(y);
HSV color space
HSV is closer to the way people describe the perception of color, namely hue, saturation, and value.
hsv_image = rgb2hsv(rgb_image)
rgb_image = hsv2rgb(hsv_image)
>> y = rgb2hsv(g);
>> subplot(131), imshow(g);
>> subplot(132), imshow(y);
>> y = hsv2rgb(y);
>> subplot(133), imshow(y);
CMY and CMYK color spaces
Most devices that deposit material onto paper require CMY data to be entered, such as color printers and copiers.
cmy_image = imcomplement(rgb_image)
rgb_image = imcomplement(cmy_image)
This function is very familiar, in fact, it is the processing of == image to negative ==
>> y = imcomplement(g);
>> subplot(131), imshow(g);
>> subplot(132), imshow(y);
>> y = imcomplement(y);
>> subplot(133), imshow(y);
HSI color space
The function for converting between custom HSI and RGB is used in the same way as above
>> y = rgb2hsi(g);
>> imshow(y);
Device-independent color space
- CIE and sRGB color space conversion
cform = makecform(type)
g = applycform(f, cform)
applycform uses the cform structure to convert the color
The above functions can be used to convert between several device-independent color spaces
>> cform = makecform('srgb2xyz');
>> g3 = applycform(g, cform);
>> subplot(121), imshow(g);
>> subplot(122), imshow(g3);
- Build a color ruler that can be used for color and grayscale publishing
>> L = linspace(40, 80, 1024);
>> radius = 70;
>> theta = linspace(0, pi, 1024);
>> a = radius * cos(theta);
>> b = radius * sin(theta);
>> L = repmat(L, 100, 1);
>> a = repmat(a, 100, 1);
>> b = repmat(b, 100, 1);
>> lab_scale = cat(3, L, a, b);
>> cform = makecform('lab2srgb');
>> rgb_scale = applycform(lab_scale, cform);
>> imshow(rgb_scale);
- ICC Color Profile
To reproduce colors with high quality across input, output, and display devices, a transform needs to be created to map colors from one device to another.
Before doing the exercise, I downloaded a new .icc profile from the ICC profile website as output for comparison.
Putting a white frame around the image makes it easier to show how the image has changed.
>> ff = Fig0612;
>> ff = padarray(f, [40 40], 255, 'both');
>> ff = padarray(ff, [4 4], 230, 'both');
>> subplot(121), imshow(ff);
>> p_srgb = iccread('sRGB.icm');
>> p_snap = iccread('PSOcoated_v3.icc');
>> cform1 = makecform('icc', p_srgb, p_snap);
>> fp_newsprint = applycform(ff, cform1);
>> cform2 = makecform('icc', p_snap, p_srgb, 'SourceRenderingIntent',
'AbsoluteColorimetric', 'DestRenderingIntent', 'AbsoluteColorimetric');
>> fp_proof = applycform(fp_newsprint, cform2);
>> subplot(122), imshow(fp_proof);
This profile is for sheetfed and heatset web offset printing according to ISO 12647-2:2013 standard coated paper (print substrate 1).
The picture display is the change after printing the original picture.
3. Basics of Color Image Processing
Color image processing is subdivided into three areas:
1. Color transformation (also known as color mapping)
2. Spatial processing of each color plane
3. Color vector processing
4. Color transformation
This section introduces a method that uses the graphical method to manipulate control points interactively, and displays the processed graphical results in real time.
Let's talk about the development of the function ice first. ice is a custom function. During the learning process, we can use the ice.mat package and ice.fig display in the DIPUM toolbox.
g = ice(‘Property Name’, ‘Property Value’)
- Image and interface display
>> g = ice('image', f);
>> g = ice('image', f, 'space', 'hsi');
The negative of this image is shown below
- Color contrast enhancement
- Monochrome contrast enhancement
- Pseudo color map
When a monochrome image of clothing is represented in the RGB color space and the resulting components are mapped separately, the result of the transformation is a pseudocolor image in which the gray levels of the input image have been replaced by arbitrary colors.
- color balance
An important application is the enhancement of photos
>> f2 = ice('image', f, 'space', 'CMY');
Modify the excess red in the picture.
- Histogram Mapping
HSI processes RGB images in space, the parameter values in ice use 'space'/'hsi'
5. Color space filtering
Color image smoothing
Linear filter to smooth an RGB image:
1. Extract three component images
>> fR = f(:, :, 1);
>> fG = f(:, :, 2);
>> fB = f(:, :, 3);
>> subplot(221), imshow(f), title('RGB');
>> subplot(222), imshow(fR), title('R');
>> subplot(223), imshow(fG), title('G');
>> subplot(224), imshow(fB), title('B');
2. Filter each component image separately, w represents the smoothing filter generated by special
fR_filtered = imfilter(fR, w, ‘replicate’)
>> w = [0.2 0.3 0.3; 0.3 0.2 0.4; 0.2 1 0.5];
>> fR_filtered = imfilter(fR, w, 'replicate');
>> fG_filtered = imfilter(fG, w, 'replicate');
>> fB_filtered = imfilter(fB, w, 'replicate');
>> fc_filtered = cat(3, fR_filtered, fG_filtered, fB_filtered);
>> subplot(221), imshow(fR_filtered), title('R');
>> subplot(222), imshow(fG_filtered), title('G');
>> subplot(223), imshow(fB_filtered), title('B');
>> subplot(224), imshow(fc_filtered), title('fc');
3. Reconstruct the filtered RGB image
fc_filtered = cat(3, fR_filtered, fG_filtered, fB_filtered)
You can also combine == the first three steps into one step ==
fc_filtered = imfilter(fc, w, ‘replicate’)
>> fc_filtered = imfilter(f, w, 'replicate');
>> imshow(fc_filtered);
Same as fc in the picture above.
- Color image smoothing
The ==fspecial function== is used to establish a predefined filter operator. The fspecial('average', 25) used in the following code indicates that the same filter with a size of 25 x 25 pixels is used to filter the luminance component. The averaging filter is large enough to produce meaningful blur.
>> h = rgb2hsi(f);
>> H = h(:, :, 1);
>> S = h(:, :, 2);
>> I = h(:, :, 3);
>> w = fspecial('average', 25);
>> hsi_filter = imfilter(h, w, 'replicate');
>> fH_imfiltered = imfilter(H, w, 'replicate');
>> Hsi_filtered = cat(3, fH_imfiltered, S, I);
>> subplot(231), imshow(H), title('H');
>> subplot(232), imshow(S), title('S');
>> subplot(233), imshow(I), title('I');
>> subplot(234), imshow(hsi_filter), title('HSI都平滑');
>> subplot(235), imshow(Hsi_filtered), title('H平滑');
Color image sharpening
We use the Laplacian filter when processing grayscale images, and we can also use the Laplacian operator to sharpen images when sharpening color images.
>> f = Fig0625;
>> w = [1 1 1; 1 -8 1; 1 1 1];
>> fdouble = tofloat(f);
>> g = fdouble - imfilter(fdouble, w, 'replicate');
>> subplot(121), imshow(f), title('原');
>> subplot(122), imshow(g), title('锐化');
The picture can clearly show the contrast, and the clarity of the image has been significantly enhanced.
6. Processing directly in RGB vector space
The processing of each color plane is not equal to the processing directly in the RGB vector space, so vector processing of color images is required.
Colored edge detection using gradients
- Color gradient for RGB image
[VG, A, PPG] = colorgrad(f, T)
colorgrad is a custom function that can calculate gradients in RGB image space.
VG is the RGB vector gradient Fθ(x, y) ; A is the angle image θ(x, y) in radians ; PPG is the gradient graph formed by summing the two-dimensional gradient images of each color plane.
- Example of detecting edges in RGB images
The three components of the RGB image are given to form the RGB image, and the edges are observed.
>> g = cat(3, a, b, c);
>> subplot(231), imshow(a);
>> subplot(232), imshow(b);
>> subplot(233), imshow(c);
>> subplot(234), imshow(g), title('合成');
>> [VG, A, PPG] = colorgrad(g);
>> subplot(235), imshow(VG), title('VG');
>> subplot(236), imshow(PPG), title('PPG');
The use of squares in the above image may not be obvious to the display, so we process a more colorful image
>> f = Fig0604;
>> subplot(141), imshow(f), title('原图');
>> [VG, A, PPG] = colorgrad(f);
>> subplot(142), imshow(VG), title('RGB空间中梯度');
>> subplot(143), imshow(PPG), title('合成梯度');
>> subplot(144), imshow(abs(VG - PPG)), title('绝对差');
Image segmentation in RGB vector space
- split function
s = colorseg(method, f, T, parameters)
- RGB color image segmentation example
>> mask = roipoly(f);
>> red = immultiply(mask, f(:, :, 1));
>> green = immultiply(mask, f(:, :, 2));
>> blue = immultiply(mask, f(:, :, 3));
>> g = cat(3, red, green, blue);
>> figure, imshow(g);
roipoly is a binary template that forms an interactive selection area, which can be cut into arbitrary polygonal patterns.