- 将索引图像转换为RGB图像
%% 将索引图像转换为RGB图像
clc;
[X,map]=imread('trees.tif','tif'); %读取索引图像,X是数据,map是调色板
RGB=ind2rgb(X,map); %将索引图像转换为RGB图像
figure;
imshow(RGB); %显示RGB图像
- 将RGB图像转换为索引图像
clc;
RGB=imread('peppers.png'); %读取RGB图像
[X,map]=rgb2ind(RGB,12); %将RGB图像转换为索引图像 调色板大小是12行 该值越大越清晰
figure;
imshow(X,map);
- 将RGB图像转换为灰度图像
clc;
RGB=imread('peppers.png','png');
I=rgb2gray(RGB);
figure;
imshow(I);
- 将索引图像转换为灰度图像方法1
clc;
[X,map]=imread('trees.tif','tif');
newmap=rgb2gray(map); %只将调色板转换为灰度
figure;
imshow(X,newmap); %新的调色板,数据不变
- 将灰度图像转换为索引图像
clc;
X=imread('rice.png'); %读取一个灰度图像
Y=grayslice(X,12); %将灰度图像转换为索引图像
figure;
imshow(Y,jet(12)); %通过调色板jet给图像上颜色
- 将索引图像转换为灰度图像方法2
clc;
[X,map]=imread('trees.tif','tif');
I=ind2gray(X,map);
figure;
imshow(I);
- 将RGB图像转换为二值图像
clc;
RGB=imread('peppers.png');
bw=im2bw(RGB,0.4); % 归一化阈值是0.4 二值图像只有黑色和白色
figure;
imshow(bw);
- 将数据矩阵转换为灰度图像
clc;
A=rand(400,400); %建立一个数据矩阵
I=mat2gray(A); %将数据矩阵转换为灰度图像
figure;
imshow(I);