彩色图像处理

*第五章 彩色图像处理
MATLAB中彩色图像的表示
1RGB图像
2索引图像
3处理RGB和索引图像的函数
彩色空间转换
1NTSC彩色空间
2YCbCr彩色空间
3CMY和CMYK彩色空间
4HSI彩色空间
5与设备无关的彩色空间
彩色图像处理基础
彩色变换

彩色图像的空间滤波
彩色图像平滑
彩色图像锐化
直接在RGB向量空间的处理
使用梯度进行彩色边缘检测
在RGB向量空间中进行图像分割

一.MATLAB中彩色图像的表示
RGB图像
一幅RGB图像是一个MxNx3的彩色像素组。可视为三幅灰度图像形成的“堆叠”。
堆叠图像:

rgb_image = cat(3, fR, fG, fB)

进行三幅分量图像的提取:
fR = rgb_image(:, :, 1);

fG = rgb_image(:, :, 2);

fB = rgb_image(:, :, 3);

自定义函数rgbcube
可以从任意角度查看彩色立方体:
rgbcube(vx, vy, 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
在这里插入图片描述
索引图像
索引图线有两个分量:一个整型数组矩阵X和一个彩色映射矩阵map。在我自己的理解看来就是把RGB图像分成了两部分存储。 map的每一行都指定单一颜色的红、绿、蓝分量。

imshow(X, map)

或者
`image(X);
colormap(map);

后面对索引图像有更多的应用。 - 指定图像的背景色

>> whitebg('g');
>> whitebg('green');
>> whitebg([0 1 0]);

处理RGB和索引图像的函数

“抖动”——dither函数
使用于灰度图像和彩色图像。这种技术常在由点组成的印刷页上给出色调变化的直观印象。

g = dither(gray_image)
在这里插入图片描述
二.彩色空间转换
NTSC彩色空间

Y:Luminance,颜色的明视度即亮度,其实就是图像的灰度值(Gray value);I和Q携带颜色信息,In-phase从橙色到青色,Quadrature-phase从紫色到黄绿色。

相互转换过程

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彩色空间
Cb是蓝色分量和参考值的差,Cr是红色分量和参考值的差。

>> y = rgb2ycbcr(f);
>> subplot(131), imshow(f);
>> subplot(132), imshow(y);
>> y = ycbcr2rgb(y);

HSV彩色空间
HSV是更接近于人们描述彩色感觉时所用的方式, 也就是色调、饱和度和数值。

>> y = rgb2hsv(g);
>> subplot(131), imshow(g);
>> subplot(132), imshow(y);
>> y = hsv2rgb(y);
>> subplot(133), imshow(y);

在这里插入图片描述
CMY和CMYK彩色空间
大多数要将材料淀积与纸上的设备都要求输入CMY数据, 比如彩色打印机和复印机等。
这个函数很熟悉,其实就是图像转负片的处理

>> y = imcomplement(g);
>> subplot(131), imshow(g);
>> subplot(132), imshow(y);
>> y = imcomplement(y);
>> subplot(133), imshow(y);

在这里插入图片描述
例题5.2
在这里插入图片描述
与设备无关的彩色空间
CIE和sRGB彩色空间转换
cform = makecform(type)
g = applycform(f, cform)

applycform使用cform结构来转换颜色
以上函数可用于几个设备无关的彩色空间之间进行转换

>> cform = makecform('srgb2xyz');
>> g3 = applycform(g, cform);
>> subplot(121), imshow(g);
>> subplot(122), imshow(g3);

构建一个可用于彩色和灰度出版的彩色标尺
在这里插入图片描述
例题5.3
在这里插入图片描述

ICC彩色剖面
为了在输入、输出和显示设备间高质量地再现颜色,需要创建一个变换,来将颜色从一种设备映射到另一种设备中。

在进行练习之前,我先从ICC剖面网站上下载了一个新的.icc剖面作为输出,这样进行对比。

在图片周围加上白框更易于显示图片产生的变化。

>> 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);

例题5.4

在这里插入图片描述

三.彩色图像处理基础
彩色图像处理细分为三个领域:

1.彩色变换(也称彩色映射)

2.各个彩色平面的空间处理

3.彩色向量处理

四.彩色变换
这一节引入一种使用图形法操作控制点的方法交互产生,并实时显示被处理的图形结果。

先说一下函数ice的开发,ice是一种自定义的函数,在学习过程中我们可以使用DIPUM工具箱里已有的ice.mat包和ice.fig显示。
图像及界面显示

>> g = ice('image', f);

图像及界面显示
例题5.5
彩色对比增强

在这里插入图片描述
在这里插入图片描述

>> g = ice('image', f);

在这里插入图片描述

>> g = ice('image', f, 'space', 'hsi');

在这里插入图片描述

负片
在这里插入图片描述
单色对比增强

在这里插入图片描述
伪彩色映射
在这里插入图片描述

在这里插入图片描述
彩色平衡

>> f2 = ice('image', f, 'space', 'CMY');

在这里插入图片描述
在这里插入图片描述
直方图的映射
在这里插入图片描述
在这里插入图片描述
HSI在空间中处理RGB图像,ice中的参数值使用‘space’/‘hsi’
五.彩色空间滤波
彩色图像平滑

线性滤波器平滑一幅RGB图像:
1.抽取三幅分量图像

>> 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.分别对每幅分量图像滤波,w表示使用special生成的平滑滤波器

>> 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.重建滤波后的RGB图像`

fc_filtered = cat(3, fR_filtered, fG_filtered, fB_filtered)

也可以将前面三个步骤合并为一步

>> fc_filtered = imfilter(f, w, 'replicate');
>> imshow(fc_filtered);

在这里插入图片描述
与上面图片中的fc是同样的。

彩色图像平滑
fspecial函数==用于建立预定义的滤波算子,下面代码中用到的fspecial(‘average’, 25), 表示使用大小25 x 25像素的同一滤波器来对亮度分量滤波。平均滤波器大到足以产生有意义的模糊度。

>> 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平滑');

在这里插入图片描述
彩色图像锐化
在处理灰度图像时我们使用了拉普拉斯滤波器, 在进行彩色图像锐化时,我们也可以使用拉普拉斯算子来锐化图像。

>> 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('锐化');

在这里插入图片描述
六.直接在RGB向量空间的处理
各个彩色平面的处理不等于直接在RGB向量空间的处理,所以需要有彩色图片的向量处理。

使用梯度进行彩色边缘检测
RGB图像的彩色梯度

[VG, A, PPG] = colorgrad(f, T)

colorgrad是自定义函数,可以实现计算RGB图像空间中的梯度。

VG是RGB向量梯度Fθ(x, y);A是以弧度计的角度图像θ(x, y);PPG是通过对各个彩色平面的二维梯度图像求和形成的梯度图形。

检测RGB图像边缘示例
给出RGB图像的三个分量来形成RGB图像,并观察边缘。

>> 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');

在这里插入图片描述
我们处理一幅颜色更丰富的图片

>> 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('绝对差');

在这里插入图片描述
在RGB向量空间中进行图像分割
分割函数
s = colorseg(method, f, T, parameters)
RGB彩色图像分割示例

>> 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);

在这里插入图片描述
所有例题结果如下图
例5.1在这里插入图片描述
5.2
在这里插入图片描述

5.3
在这里插入图片描述
5.4
在这里插入图片描述
5.5在这里插入图片描述
在这里插入图片描述
5.6在这里插入图片描述
5.7在这里插入图片描述
5.8在这里插入图片描述
5.9在这里插入图片描述
5.10在这里插入图片描述
5.11在这里插入图片描述
5.12
在这里插入图片描述
5.13
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值