您的位置:首页 > 编程语言 > MATLAB

MATLAB学习笔记 彩色图像处理

2017-06-21 16:34 525 查看
以下内容以书中内容为基础,加上自己的理解的表述,如有错误,望请批评指正

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), imsh
a3c4
ow(f), title('原图');
subplot(1, 2, 2), imshow(g), title('抖动出来的二值图');


输入:



输出:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: