>> I=imread('football.jpg');
>> X=rgb2gray(I);
>> figure;
>> subplot(121);imshow(I);
>> subplot(122);imshow(X);
2、RGB图像转换成索引图像
>>RGB=imread('football.jpg');
>>[X1,map1]=rgb2ind(RGB,64);%最小方差量化法 颜色数n=64
>>[X2,map2]=rgb2ind(RGB,0.2);%均匀量化法 n取0.2 n至少能娶到216种
>> map3=colorcube(128);%近似法 /RGB颜色映射表
>> X3=rgb2ind(RGB,map3);
>> figure;
>>subplot(131);imshow(X1,map1);
>>subplot(132);imshow(X2,map2);
>>subplot(133);imshow(X3,map3);
I=imread('cameraman.tif');
>> [X,map]=gray2ind(I,8);
>> figure;imshow(I);
>> figure;imshow(X,map);
4、索引图像转换成灰度图像
>>[X,map]=imread('forest.tif');
>> I=ind2gray(X,map);
>> figure;imshow(X,map);
>> figure;imshow(I);
5、索引图像转换成RGB图像
>> [X,map]=imread('kids.tif');
>> RGB=ind2rgb(X,map);
>> figure;imshow(X,map);
>> figure;imshow(RGB);
6、灰度图像转换为二值图像
>> I=imread('rice.png');
>> EW1=im2bw(I,0.4);%阈值较小,背景区域与目的区域混淆
>> EW2=im2bw(I,0.6);% 阈值较大,丢失部分目标信息
>> figure;
>> subplot(131);imshow(I);
>> subplot(132);imshow(EW1);
>> subplot(133);imshow(EW2);
7、数值矩阵转化为灰度图像
>> X=magic(256);
>> I=mat2gray(X);
>> figure;imshow(I);
本文详细介绍了图像处理中的各种类型转换,包括真彩色图像转灰度图像、RGB图像转索引图像、灰度图像转索引图像、索引图像转灰度及RGB图像、灰度图像转二值图像。通过实例代码展示了转换过程,如使用rgb2gray、rgb2ind、gray2ind、im2bw等函数。
400

被折叠的 条评论
为什么被折叠?



