数字图像处理及MATLAB实现实验二

1.像素的4连通和8连通 

bw=[1 0 0 1 0;
    1 0 1 0 1;
    0 0 1 0 1;
    1 0 0 0 1]
X8=bwlabel(bw,8)
X4=bwlabel(bw,4)

 

数字图像处理及MATLAB实现实验二

 

2.对lena图像分别进行4倍和6倍采样,查看其减采样效果

a=imread('lena.jpg');
b=rgb2gray(a);
[wid,hei]=size(b);
%4倍减采样
quartimg=zeros(wid/2+1,hei/2+1);
i1=1;
j1=1;
for i=1:2:wid
    for j=1:2:hei
        quartimg(i1,j1)=b(i,j);
        
        j1=j1+1;
    end;
    i1=i1+1;
    j1=1;
end
figure
imshow(uint8(quartimg))

%16倍减采样
quartimg=zeros(wid/4+1,hei/4+1);
i1=1;
j1=1;
for i=1:4:wid
    for j=1:4:hei
        quartimg(i1,j1)=b(i,j);
        
        j1=j1+1;
    end;
    i1=i1+1;
    j1=1;
end
figure
imshow(uint8(quartimg))

数字图像处理及MATLAB实现实验二

数字图像处理及MATLAB实现实验二

 

3.将原图像转化为256级灰度图像,64级灰度图像,32级灰度图像,8级灰度图像和2级灰度图像

a=imread('flower.jpg');
figure;
imshow(a);
b=rgb2gray(a);
figure;
imshow(b);
[wid,hei]=size(b);
img64=zeros(wid,hei);
img32=zeros(wid,hei);
img8=zeros(wid,hei);
img2=zeros(wid,hei);

for i=1:wid
    for j=1:hei
        img64(i,j)=floor(b(i,j)/4);
    end
end
figure
imshow(uint8(img64),[0,63])

for i=1:wid
    for j=1:hei
        img32(i,j)=floor(b(i,j)/8);
    end
end
figure
imshow(uint8(img32),[0,31])

for i=1:wid
    for j=1:hei
        img8(i,j)=floor(b(i,j)/32);
    end
end
figure
imshow(uint8(img64),[0,7])
for i=1:wid
    for j=1:hei
        img2(i,j)=floor(b(i,j)/128);
    end
end
figure
imshow(uint8(img64),[0,2])

 

数字图像处理及MATLAB实现实验二

 

上一篇:OpenGL教程一


下一篇:%matplotlib inline的含义