1. 程式人生 > >MATLAB一些簡單的影象處理程式

MATLAB一些簡單的影象處理程式

1

I=imread('pout.tif');
figure,imshow(I);
[m,n]=size(I);
Inew=zeros(floor(m/2),n);
for i=1:m
    for j=1:n
    Inew(i,j)=I(2*i-1,j);
    end
end
figure,imshow(uint8(Inew))

2
clc
clear 
x=0:255;
y1=x;
figure,
plot(x,y1,'r')
hold on
y2=255:-1:0;
plot(x,y2,'b')
ylog=255*(log(x+1)/log(256));
plot(x,ylog,'g+')
yexp=255*x.^3/255^3;
plot(x,yexp,'y+')
c=255/255^3;
ynthroot=nthroot((x/c),3);
plot(x,ynthroot,'yo')
3
I=imread('moon.tif');
I=double(I);
[m,n]=size(I);
for i=1:m
    for j=1:n
        if I(i,j)<100
            Inew(i,j)=3*I(i,j);
        elseif I(i,j)>200
            Inew(i,j)=0.8*I(i,j);
        else
            Inew(i,j)=I(i,j);
        end
    end
end
figure,imshow(Inew,[])
4
clear
clc
I=imread('moon.tif');
I=im2double(I);
[m,n]=size(I);
for i=1:m
    for j=1:n
        Inew(i,j)=I(i,j)^0.2;
    end
end
figure,imshow(I,[])
figure,imshow(Inew,[])
5

每個畫素點的計算與查詢表的比較

clear
clc
I=imread('moon.tif');
I=im2double(I);
[m,n]=size(I);
%迴圈實現
tic
for i=1:m
    for j=1:n
        Inew(i,j)=255*log(I(i,j)+1)/log(256);
    end
end
toc
%查詢表
tic
x=0:255;
c=255/log(256);
y=c*log(x+1);
I_log=uint8(y(x+1));
figure,plot(x,y)
toc
figure,imshow(I_log,[])
figure,imshow(I,[])
figure,imshow(Inew,[])

6

顯示一幅影象的直方圖

I=imread('pout.tif');
figure,imshow(I)
[m,n]=size(I);
h=zeros(1,256);
for k=0:255
    for i=1:m
        for j=1:n
            if(I(i,j)==k)
               h(k+1)=h(k+1)+1;
            end
        end
    end
end
figure,bar(h)
改進後代碼:
I=imread('pout.tif');
figure,imshow(I)
[m,n]=size(I);
h=zeros(1,256);
for i=1:m
    for j=1:n
        h(I(i,j)+1)=h(I(i,j)+1)+1;
    end
end
figure,bar(h)

將一幅圖經直方圖均衡化處理
I=imread('pout.tif');
figure,imshow(I)
[m,n]=size(I);
h=zeros(1,256);
for i=1:m
    for j=1:n
        h(I(i,j)+1)=h(I(i,j)+1)+1;
    end
end
figure,bar(h)
p=h/(m*n);
sum=0;
for i=1:256
    sum=sum+p(i);
    y(i)=round(sum*255);
end
for i=1:m
    for j=1:n
        Inew(i,j)=y(I(i,j)+1);
    end
end

figure,imshow(unit8(Inew))

7 鄰域處理

I=imread('pout.tif');
I=double(I);
%卷積模板的不同處理效果不同
%M=ones(3,3)/9;將一幅圖模糊處理; M=[0 -1 0;-1 5 -1;0 -1 0];將一幅圖銳化處理,改變中心位置的值有不同效果
%M=[1 1 1;0 0 0;-1 -1 -1];指出原始影象中水平邊緣存在位置的影象;M=[1 0 -1;1 0 -1;1 0 -1];指出原始影象中垂直邊緣存在位置的影象
M=ones(3,3)/9;
[m,n]=size(I);
Inew=zeros(m,n);
%從2開始到m-1忽略邊界
for i=2:m-1     
    for j=2:n-1
      Inew(i,j)=I(i-1,j-1)*M(1,1)+I(i-1,j)*M(1,2)+...
      I(i-1,j+1)*M(1,3)+I(i,j-1)*M(2,1)+I(i,j)*M(2,2)+...
      I(i,j+1)*M(2,3)+I(i+1,j-1)*M(3,1)+I(i+1,j)*M(3,2)+... 
      I(i+1,j+1)*M(3,3);
    end
end
figure,imshow(uint8(Inew))