iLMS知識社群(Sites)師生部落格(Blogs)朝陽首頁(Homepage)TronClass教學平台登入
位置: 陳鉞勳 > 未分類
Matlab
by 陳鉞勳 2017-09-22 15:45:51, 回應(16), 人氣(630)
ctrl + R 整段註解
ctrl + T 還原註解
回應(16)
imshow(x)
->uint8=0~255
->double=0~1
x=imread('bean.jpg');
imshow(x)
figure
y=rgb2gray(x);
imshow(y)
figure
imhist(y)
figure
c=(0:255);
for iq=81:150
    c(iq)=c(iq-1)+10;
end
for iq=151:256
    c(iq)=c(iq-1)+1;
end
plot(c) 
c=c/c(256);
yy=c(y);
figure
imshow(yy);
figure;
imhist(yy);
%使用power-law將暗的圖轉亮變清楚
x=imread('HIDE.tif');

c=(0:255);
c=c/255;
c=c.^0.1;% Here can change the power.
subplot(3,2,4);
b=(1:256);
plot(b,c);
title('transformation function');
subplot(3,2,1)
imshow(x);
%subplot跟imshow有關
subplot(3,2,5)
imshow(x);
title('gray image');
bw=im2uint8(c(x+1));
subplot(3,2,3)
imshow(bw);
title('result');
subplot(3,2,2)
imhist(x);
title('histogram');
subplot(3,2,6);
[y,i]=imhist(bw);
bar(b,y,'r');
title('result histogram');

低通濾波(使用Mask)

x=imread('imadj.jpg');
%c=[1 1 1 1 1;1 1 1 1 1 ;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1];
%c=[1 1 1 ;1 1 1  ;1 1 1 ];
c=[1 1  ;1 1 ];
c=c/25;
bw=uint8(filter2(c,double(x)));
subplot(2,1,1)
imshow(x);
subplot(2,1,2)
imshow(bw);
%高通 Sobel Laplacian

g1(1:15,1:15)=0
g2(1:15,1:15)=0
g3(1:15,1:15)=0
g1(7,7)=1
g2(:,7)=1
g3(:,7:15)=1
h=fspecial('laplacian')
s=fspecial('sobel')
L1=imfilter(g1,-h)
L2=imfilter(g2,-h)
L3=imfilter(g3,-h)
gx=imfilter(g1,s)
gy=imfilter(g1,s')
s1=(gx.*gx+gy.*gy).^0.5
gx=imfilter(g2,s)
gy=imfilter(g2,s')
s2=(gx.*gx+gy.*gy).^0.5
gx=imfilter(g3,s)
gy=imfilter(g3,s')
s3=(gx.*gx+gy.*gy).^0.5
subplot(3,3,1)
imshow(g1)
subplot(3,3,2)
imshow(g2)
subplot(3,3,3)
imshow(g3)
subplot(3,3,4)
imshow(L1)
subplot(3,3,5)
imshow(L2)
subplot(3,3,6)
imshow(L3)
subplot(3,3,7)
imshow(s1)
subplot(3,3,8)
imshow(s2)
subplot(3,3,9)
imshow(s3)
%Mesh用法 Gaussian

g1(1:15,1:15)=0
g2(1:15,1:15)=0
g3(1:15,1:15)=0
g1(7,7)=1
g2(:,7)=1
g3(:,7:15)=1
hfu=fspecial('gaussian',5)
gu1=imfilter(g1,hfu)
gu2=imfilter(g2,hfu)
gu3=imfilter(g3,hfu)
mesh(g3)
figure
mesh(L3)
figure
mesh(gu3)
clear 
c=[1:100];
c=8*3.1415926*c/100;
bc=sin(c);
plot(bc);
fbc=fft(bc);
fbc=(fbc.*conj(fbc));
figure
plot(fbc)
figure
c=[1:100];
tc=14*3.1415926*c/100;
tbc=sin(tc);
plot(tbc);
tfbc=fft(tbc);
tfbc=(tfbc.*conj(tfbc));
figure
plot(tfbc)
total=tbc+bc;
figure 
plot(total)
%傅立葉
ftotal=fft(total);
gg=ftotal;
ftotal=(ftotal.*conj(ftotal));
figure
plot(ftotal)

%反傅立葉
gg(8)=0;
gg(94)=0;
%共顎 沒相位角
fgg=(gg.*conj(gg));
figure
plot(fgg)
figure
plot(ifft(gg))
%單週期 cos 0~2(3.14)
clear
c(1,1)=0.1;
for i=1:512
    for j=1:512
        c(i,j)=0.5+0.25*cos(2*3.1415926*double(i)/512)+0.25*cos(2*3.1415926*double(j)/512);
    end
end
mesh(c);
%多週期 cos 0~4(3.14)
clear
c(1,1)=0.1;
for i=1:512
    for j=1:512
        c(i,j)=0.5+0.25*cos(2*3.1415926*double(i)/256)+0.25*cos(2*3.1415926*double(j)/256);
    end
end
mesh(c);
clear;
c=imread('slop.jpg');
%轉灰階
[p1,p2,p3]=size(c);
if (p3>1)
    c=rgb2gray(c);
end
%傅立葉轉換
g=fft2(c);
imshow(c);
figure
g=g.*conj(g);
mesh(g);
%取log
g=log1p(g);
% imshow(g/(max(max(g))))
figure
%對角交換
g1=[g(p1/2+1:p1,p2/2+1:p2) g(p1/2+1:p1,1:p2/2); g(1:p1/2,p2/2+1:p2) g(1:p1/2,1:p2/2)];
mesh(g1);
%結合理想濾波器.butterworth.Gaussian
%總共 五張圖 原.fft2(共顎).原H.用H濾完(共顎).濾完轉回原圖(不能用共顎完的)
clear;
c=imread('img1.jpg');
[p1,p2,p3]=size(c);
if (p3>1)
    c=rgb2gray(c);
end
%imshow
imshow(c);
figure
g1=fft2(c);
g=[g1(p1/2+1:p1,p2/2+1:p2) g1(p1/2+1:p1,1:p2/2); g1(1:p1/2,p2/2+1:p2) g1(1:p1/2,1:p2/2)];
%power spectrum
g2=g.*conj(g);
g2=log1p(g2);
%mesh
mesh(g2);
figure

%H=filt(p1,p2,100);
%H=Bwh(p1,p2,40,1);
H=Gss(p1,p2,30);
%mesh
mesh(H);
figure

g=g.*H;
%power spectrum
g2=g.*conj(g);
g2=log1p(g2);
%mesh
mesh(g2)
figure

g3=[g(p1/2+1:p1,p2/2+1:p2) g(p1/2+1:p1,1:p2/2); g(1:p1/2,p2/2+1:p2) g(1:p1/2,1:p2/2)];
ic=ifft2(g3);
ic=uint8(real(ic));
imshow(ic);

---filt.m 理想濾波器

function H=filt(p1,p2,D0)
cx=p2/2;
cy=p1/2;
H(1:p1,1:p2)=0;
for y=1:p1
    for x=1:p2
        if (((y-cy)^2+(x-cx)^2)^0.5<D0)
            H(y,x)=1;
        end
    end
end

---Bwh.m 

function H=Bwh(p1,p2,D0,n)
cx=p2/2;
cy=p1/2;
H(1:p1,1:p2)=0;
for y=1:p1
    for x=1:p2
        duv=((y-cy)^2+(x-cx)^2)^0.5;
        H(y,x)=1/(1+((duv/D0)^(2.*n)));       
    end
end

----Gss.m

function H=Gss(p1,p2,D0)
cx=p2/2;
cy=p1/2;
H(1:p1,1:p2)=0;
for y=1:p1
    for x=1:p2
        duv=((y-cy)^2+(x-cx)^2)^0.5;
        H(y,x)=exp(-(duv^2)/(2*(D0^2)));       
    end
end

-----
呼叫時
H=Gss(p1,p2,30);
mesh(H);
% 1027小考 把car雜訊弄掉並清晰
clear;
c=imread('car.tif');
[p1,p2,p3]=size(c);
if (p3>1)
    c=rgb2gray(c);
end
%imshow
imshow(c);
figure

g1=fft2(c);
g=[g1(p1/2+1:p1,p2/2+1:p2) g1(p1/2+1:p1,1:p2/2); g1(1:p1/2,p2/2+1:p2) g1(1:p1/2,1:p2/2)];
%power spectrum
% g2=g.*conj(g);
% g2=log1p(g2);
% %mesh
% mesh(g2);
% figure


H=Gss(p1,p2,150);
%mesh
% mesh(H);
% figure

g=g.*H;
g(160:170,50:61)=g(91,30);
g(80:90,50:61)=g(91,30);
g(160:170,110:120)=g(91,30);
g(80:90,110:120)=g(91,30);
 

%power spectrum
g2=g.*conj(g);
g2=log1p(g2);
%mesh
mesh(g2)
figure

g3=[g(p1/2+1:p1,p2/2+1:p2) g(p1/2+1:p1,1:p2/2); g(1:p1/2,p2/2+1:p2) g(1:p1/2,1:p2/2)];
ic=ifft2(g3);
ic=uint8(real(ic));
imshow(ic);

--Gss.m

function H=Gss(p1,p2,D0)
cx=p2/2;
cy=p1/2;
H(1:p1,1:p2)=0;
for y=1:p1
    for x=1:p2
        duv=((y-cy)^2+(x-cx)^2)^0.5;
        H(y,x)=exp(-(duv^2)/(2*(D0^2)));       
    end
end
%加入雜訊
c=imread('rt.jpg');
[p1,p2,p3]=size(c);
if (p3>1)
    c=rgb2gray(c);
end
imshow(c);figure;
imhist(c);figure;
yz=imnoise(c,'gaussian',0,0.01);
imshow(yz);figure;
imhist(yz);figure;
g1=fft2(c);
g=[g1(p1/2+1:p1,p2/2+1:p2) g1(p1/2+1:p1,1:p2/2); g1(1:p1/2,p2/2+1:p2) g1(1:p1/2,1:p2/2)];

g2=g.*conj(g);
g2=log1p(g2);

mesh(g2);figure;
g1=fft2(yz);
g=[g1(p1/2+1:p1,p2/2+1:p2) g1(p1/2+1:p1,1:p2/2); g1(1:p1/2,p2/2+1:p2) g1(1:p1/2,1:p2/2)];

g2=g.*conj(g);
g2=log1p(g2);

mesh(g2);

%salt & pepper
sz=imnoise(c,'salt & pepper',0.01);
imshow(sz);
figure;
imhist(sz);
%rgb2hsv
c=imread('rt.jpg');
imshow(c);
figure;
c=rgb2hsv(c);

c(:,:,1)=0.2; %H 顏色
c(:,:,2)=0.8; %S 深淺
% c(:,:,3)=0.2; %V 亮度
c=hsv2rgb(c);
imshow(c);
luminance=[16,11,10,16,24,40,51,61
           12,12,14,19,26,58,60,55
           14,13,16,24,40,57,69,56
           14,17,22,29,51,87,80,62
           18,22,37,56,68,109,103,77
           24,35,55,64,81,104,113,92
           49,64,78,87,103,121,120,101
           72,92,95,98,112,100,103,99];
img=imread('m2.jpg');
ny = rgb2ycbcr(img);
r = img(:,:,1); g = img(:,:,2); b = img(:,:,3);
%y=0.299*r+0.587*g+0.114*b;
y=ny(:,:,1);
% cb=ny(:,:,2);
% cr=ny(:,:,3);
Cr=(r-y)/1.402;
Cb=(b-y)/1.772;
A=y(1:8,1:8);
yy = dct2(A);
luminance=[16,11,10,16,24,40,51,61;12,12,14,19,26,58,60,55;14,13,16,24,40,57,69,56;14,17,22,29,51,87,80,62;18,22,37,56,68,109,103,77;24,35,55,64,81,104,113,92;49,64,78,87,103,121,120,101;72,92,95,98,112,100,103,99];
A2=round(yy./luminance);
A2=A2.*luminance;
B=uint8(idct2(A2));
ps=psnr(A,B)
回應