%% 学习目标:图像类型的转换
%% 将索引图像转化为RGB图像
clear all;
close all;
[X,map] = imread('trees.tif','tif'); %读取索引图像
RGB = ind2rgb(X,map);%把索引图像转换成RGB格式。
figure;
imshow(RGB);
%set(gcf,'position',[500,500,600,600]);
%% 将RGB图像转换成索引图像
clear all;
close all;
RGB = imread('peppers.png'); %读取RGB图像
[X,map] = rgb2ind(RGB,12); %将RGB图像转换为索引图像, 调色板大小是12行,行越大越清晰。
figure;
imshow(X,map);
%% 将RGB图像转换为灰度图像
clear all;
close all;
RGB = imread('peppers.png','png');
I = rgb2gray(RGB);
figure;
imshow(I);
%% 将索引图像转换为灰度图像
clear all;
close all;
[X,map] = imread('trees.tif','tif');
newmap = rgb2gray(map);%只将调色板转换为灰度,实际上map中存的就是rgb。
figure;
imshow(X,newmap);
%% 将索引图像转换为灰度图像
clear all;
close all;
[X,map] = imread('trees.tif','tif');
I = ind2gray(X,map);
figure;
imshow(I);
%% 将灰度图像转换为索引图像
clear all;
close all;
X = imread('rice.png'); %读取一个灰度图像
Y = grayslice(X,12); %将灰度图像转换为索引图像
figure;
imshow(Y,jet(12)); %通过调色板jet给图像上颜色
%% 将RGB图像转换为二值图像
clear all;
close all;
RGB = imread('peppers.png','png');
bw = im2bw(RGB,0.4); % 归一化阀值是0.4 (大于0.4为1,否则为0) 二值图像只有黑色和白色
figure;
imshow(bw);
%% 将数据矩阵转换为灰度图像
clear all;
close all;
A = rand(400,400); % 建立一个数据矩阵
I = mat2gray(A); %将数据矩阵转换为灰度图像
figure;
imshow(I);