MATLAB学习笔记 彩色图像处理

以下内容以书中内容为基础,加上自己的理解的表述,如有错误,望请批评指正

MATLAB中的彩色图像的表示(图像格式)

RGB图像

所谓RGB图像,即为R(Red 红) G(Green绿) B(Blue蓝) 的灰度图(三个长宽一样的二维矩阵)组成的图像比如以下这段代码

r = zeros(300, 300);
g = zeros(300, 300);
b = zeros(300, 300);
r(1:100, 1:100) = 1;
g(101:200, 101:200) = 1;
b(201:300, 201:300) = 1;
img = cat(3, r, g, b);
 subplot(2, 2, 1), imshow(r), title('R');
 subplot(2, 2, 2), imshow(g), title('G');
 subplot(2, 2, 3), imshow(b), title('B');
 subplot(2, 2, 4), imshow(img), title('RGB');
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

可以看出 其实RGB 三张分别代表红绿蓝的灰度图 叠加在一起的图像。

索引图像

索引图像有两个分量: 一个整数数据矩阵X 和 一个彩色银蛇矩阵map。矩阵map是一个大小为m*3的double类数组,其值是区间[0, 1]上的浮点数。map的长度m等于其定义的颜色的个数。map存的是RGB三个分量。

f  = imread('onion.png');
[X, map] = rgb2ind(f, 2);
[X2, map2] = rgb2ind(f, 256);
[X3, map3] = rgb2ind(f, 65535);
subplot(2, 2, 1), imshow(f), title('原图');
subplot(2, 2, 2), imshow(X, map), title('索引图 2种颜色');
subplot(2, 2, 3), imshow(X2, map2), title('索引图 256种颜色');
subplot(2, 2, 4), imshow(X3, map3), title('索引图 65535种颜色');
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

处理RGB和索引图像的函数

* ind2rgb(X, map) 将索引图变为RGB图*
rgb2ind(image, n) 将RGB转为索引图,索引颜色数规定为n个,可以指定转换时运用什么方式进行颜色处理

有二种方式,一种为dither 抖动,另一种为nodither 不抖动。
抖动是什么意思呢,就是为了让图片看起来更加自然,每个像素的点会中和其邻域的点的颜色,这样就会显得这个点的像素颜色不那么突兀。

f = imread('onion.png');
subplot(2, 3, 1), imshow(f), title('原图');
subplot(2, 3, 4), bar([imhist(f(:, :, 1), 10), imhist(f(:, :, 2), 10), imhist(f(:, :, 3), 10)]), axis tight, title('原图的三色直方图');
[X, map] = rgb2ind(f, 256); % 默认为dither
subplot(2, 3, 2), imshow(ind2rgb(X, map)), title('dither');
f = ind2rgb(X, map);
subplot(2, 3, 5), bar([imhist(f(:, :, 1), 10), imhist(f(:, :, 2), 10), imhist(f(:, :, 3), 10)]), axis tight, title('dither的三色直方图');
[X, map] = rgb2ind(f, 256, 'nodither');
subplot(2, 3, 3), imshow(ind2rgb(X, map)), title('nodither');
f = ind2rgb(X, map);
subplot(2, 3, 6), bar([imhist(f(:, :, 1), 10), imhist(f(:, :, 2), 10), imhist(f(:, :, 3), 10)]), axis tight, title('nodither的三色直方图');
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述
dither(image) 该方法即为上面rgb2ind函数的dither方法,该函数处理灰度图效果最明显
f = imread('coins.png');
g = dither(f);
subplot(1, 2, 1), imshow(f), title('原图');
subplot(1, 2, 2), imshow(g), title('抖动出来的二值图');
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述
  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值