读入matlab自带彩色图像canoe.tif,将此彩色图像转换为灰度图像;分别在彩色图像和灰度图像上+30,观察增加数值后的图像维度,并显示修改后的图像(注意修改后的图像调整到double[0-1]或uint8[0-255]显示)。
附代码:
[X,map]=imread('canoe.tif');
figure(1);subplot(1,3,1);
imshow(X,map);title('原始图片','fontsize',8);
[M,N]=size(map);
map1=zeros(M,N);
for i=1:M
for j=1:N
if map(i,j)==1
map1(i,j)=1.0;
else
map1(i,j)=map(i,j)+30/255;
if map1(i,j)>1
map1(i,j)=1;
end
end
end
end
figure(1);subplot(1,3,2);
imshow(X,map1);title('色彩图增量','fontsize',8);
I=ind2gray(X,map);
I1=double(I)+30;
I2=uint8(I1);
figure(1);subplot(1,3,3);
imshow(I2);title('»ÒɫͼÔöÁ¿','fontsize',8);
(2)读入matlab自带图像 office_1.jpg office_2.jpg, 利用函数imdivide(),用第2副图像除以第一幅图像,第二副图像除0.5,并分别显示这两个操作的结果。(注意修改后的图像调整到double[0-1]或uint8[0-255]显示)
附代码:
I=imread('office_1.jpg');
X=imread('office_2.jpg');
Y=imdivide(I,Y);
Z=imdivide(X,0.5);
figure(1)
subplot(1,2,1)
imshow(I)
title('office_1.jpg')
subplot(1,2,2)
imshow(X);
title('office_2.jpg')
figure(2)
imshow(Y)
title('office_1.jpg/office_2.jpg')
figure(3)
imshow(X);
title('office_2.jpg/0.5')
(3)图像的逻辑运算,读入matlab 自带图像pillsetc.png,利用函数im2bw()函数转为2值图像(自行查阅im2bw()函数的功能),利用此2值图像作为掩模与原始图像进行逻辑乘,取出目标图像;利用2值图像取出图像背景;显示取出的目标图像和背景图像。
附代码:
I=imread('pillsetc.png');
figure(1);subplot(1,2,1);
imshow(I);title('原始图片','fontsize',8);
x=graythresh(I);
BW=im2bw(I,x);
BW1=uint8(BW);
figure(1);subplot(1,2,2);
imshow(BW);title('二值图像','fontsize',8);
J=I.*BW1;
figure(2);subplot(1,2,1);
imshow(J);title('目标图','fontsize',8);
BW2=not(BW);
BW22=uint8(BW2);
K=I.*BW22;
figure(2);subplot(1,2,2);
imshow(K);title('背景图','fontsize',8);
(4)读入matlab自带图像pout.tif, 实现对此图像按比例缩小(纵横向缩小效果一样),不按比例缩小(纵横不同),以及图像成倍放大和不按比例放大效果。
附代码。。。
I=imread('pout.tif');
F=imresize(I,0.5,'nearest');
J=imresize(I,[190,400],'nearest');
M=imresize(I,2.0,'nearest');
N=imresize(I,[800,400],'nearest');
figure(1)
subplot(1,3,1); imshow(I); title('原始图')
subplot(1,3,2); imshow(F); title('缩小0.5')
subplot(1,3,3); imshow(J); title('不等比缩放');
figure(2);
subplot(1,3,1); imshow(I); title('原始图')
subplot(1,3,2); imshow(M); title('放大2倍')
subplot(1,3,3); imshow(N); title('不等比放大');
(5)读入matlab自带图像pout.tif, 选择任何一种方式实现对图像的平移,旋转,尺度缩放,水平镜像,垂直镜像,或者自选任意组合实现对图像的几何变换。并显示几何变换的结果。
附代码。。。
I = imread('pout.tif');
se = translate(strel(1), [50 140]);
J = imdilate(I,se);
I1=imrotate(I,25);
A=imresize(I,0.5,'nearest');
B=imresize(I,2,'nearest');
figure(1);
subplot(121);imshow(I), title('原图')
subplot(122), imshow(J), title('移动后的图像');
figure(2)
imshow(I1);
title('旋转25度图')
figure(3)
imshow(A)
figure(4)
imshow(B)
[M,N]=size(I);
I_shuiping=I;
for i=1:M
for j=1:N
I_shuiping(i,j)=I(i,N-j+1);
end
end
figure(5)
subplot(2,2,1);
imshow(I);
title('原图')
subplot(2,2,2);
imshow(I_shuiping);
title('放大2倍')
I_chuizhi=I;
for i=1:M
for j=1:N
I_chuizhi(i,j)=I(M-i+1,j);
end
end
subplot(2,2,3);
imshow(I_chuizhi);
title('不等比放大')