一、索引图像

 

RGB图转索引图

索引图转RGB图

clear all;close all;clc
X=imread('lena1024.bmp');
[imgind, map] = rgb2ind(X, 256);   % 转换为256色的索引图像
Y= ind2rgb(imgind,map);
figure(1);
subplot(121),imshow(X),xlabel('(a) 原图');
subplot(122),imshow(imgind),xlabel('(b) 仅索引图');
title('图1.1  RGB图转索引图','position',[-150,1400],'FontSize',15);
figure(2);
subplot(121),imshow(imgind,map),xlabel('(a) 带色索引图');
subplot(122),imshow(Y),xlabel('(b) 恢复图');  
title('图1.2  索引图转RGB图','position',[-150,1400],'FontSize',15);

 二roicolor函数

对于某些特殊的图像处理,我们不希望将整张图都进行图像处理。这个时候就用到了roicolor、roipoly、roifill、fspecial、roifilt2函数。代码实现过程如下

I = imread('squirrel.jpg');
I = rgb2gray(I);
BW = roicolor(I,128,255);
subplot(121);imshow(I);xlabel('(a) 原图');
subplot(122);imshow(BW);xlabel('(b) 通过roicolor函数变换后的图像');
title('图2  RGB图转索引图','position',[-150,750],'FontSize',15);

close all;                  %关闭当前所有图形窗口,清空工作空间变量,清除工作空间所有变量
clear all;
clc;
I=imread('pout.tif');           %输入原图像
BW1=roicolor(I,55,100);                   %基于灰度图像ROI区域选取
c=[87 171 201 165 79 32 87];
r=[133 133 205 259 259 209 133];%定义ROI顶点位置
BW=roipoly(I,c,r); %根据c和r选择ROI区域
I1=roifill(I,BW); %根据生成BW掩膜图像进行区域填充
h=fspecial('motion',20,45); %创建motion滤波器并说明参数
I2=roifilt2(h,I,BW); %进行区域滤波
set(0,'defaultFigurePosition',[100,100,1000,500]);%修改图形图像位置的默认设置
set(0,'defaultFigureColor',[1 1 1])%修改图形背景颜色的设置
figure
subplot(121),imshow(BW1); %显示处理结果
subplot(122),imshow(BW); %显示ROI区域
figure
subplot(121),imshow(I1);%显示填充效果
subplot(122),imshow(I2); %显示区域滤波效果

更多推荐

MATLAB超级实用的简单图像处理代码大全