%==========================讀檔=============================
X = imread('lenna.bmp','bmp');
img_h = size(X,1)
img_w = size(X,2);
I = double(rgb2gray(X)); %rgb轉灰階
img = zeros(img_h,img_w);
whos img
%=====================sobel mask============================
sob_x = [1,2,1;0,0,0;-1,-2,-1];
sob_y = [-1,0,1;-2,0,2;-1,0,1];
%Q1
%================Gaussian size 3x3 sigma 1================
%可以在此調整參數產生不同的結果
threshold = 70;
mask_size = 3; %gaussian mask 的 size
s = 1; %gaussian mask 的 標準差
%=================gaussian filter============================
gaussian = fspecial('gaussian',mask_size,s);
%===================Smoothing image==========================
smooth = conv2(I,gaussian,'same');
%==========smooth's image convolution with sobel operators===
dx = conv2(smooth,sob_x,'same') ;
dy = conv2(smooth,sob_y,'same') ;
dx = dx/8;
dy = dy/8;
%============compute elements of matrix A=====================
dxx = dx .* dx;
dyy = dy .* dy;
dxy = dx .* dy;
%===============Q2的a小題 使用5x5的window=================================
%==========================================================================
add5 = [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];
exx = conv2(dxx,add5,'same') ;
eyy = conv2(dyy,add5,'same') ;
exy = conv2(dxy,add5,'same') ;
k = 0.15;
R = zeros(img_h,img_w);
corner = zeros(img_h,img_w,'uint8');
%==================計算出R(A)=========================
for i= 1:img_h
for j=1:img_w
detA = exx(i,j) * eyy(i,j) - exy(i,j)^2;
trA = exx(i,j) + eyy(i,j);
R(i,j) = detA-k*trA^2;
end
end
%================threshold================
for i= 2:img_h-1
for j = 2:img_w-1
if( R(i,j) > 50000 )
corner(i,j) = 255;
end
end
end
XX = X;
%================在corner的地方標記'十'=========================
for i=1:img_h
for j=1:img_w
if( corner(i,j)==255 )
XX(i,j,1) = 255;
if( (i > 1) & (j > 1) & (i < img_h) & (j < img_w) )
XX(i-1,j,1) = 255;
XX(i+1,j,1) = 255;
XX(i,j+1,1) = 255;
XX(i,j-1,1) = 255;
if( (i > 2) && (j > 2) && i+1 < img_h && j+1 < img_w-1)
XX(i,j-2,1) = 255;
XX(i-2,j,1) = 255;
XX(i,j+2,1) = 255;
XX(i+2,j,1) = 255;
end
end
end
end
end
subplot(2,2,1);
imshow(XX);
[output,serr] = sprintf('Q2_lenna_1.bmp');
imwrite(XX,output);
%===============Q2的a小題 使用9x9的window===================================
%==========================================================================
add9 = [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,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,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,1,1,1,1,1,1;];
exx = conv2(dxx,add9,'same') ;
eyy = conv2(dyy,add9,'same') ;
exy = conv2(dxy,add9,'same') ;
k = 0.15;
R = zeros(img_h,img_w);
corner = zeros(img_h,img_w,'uint8');
%==================計算出R(A)=========================
for i= 1:img_h
for j=1:img_w
detA = exx(i,j) * eyy(i,j) - exy(i,j)^2;
trA = exx(i,j) + eyy(i,j);
R(i,j) = detA-k*trA^2;
end
end
%================threshold================
for i= 2:img_h-1
for j = 2:img_w-1
if( R(i,j) > 50000 )
corner(i,j) = 255;
end
end
end
XX = X;
%================在corner的地方標記'十'=========================
for i=1:img_h
for j=1:img_w
if( corner(i,j)==255 )
XX(i,j,1) = 255;
if( (i > 1) & (j > 1) & (i < img_h) & (j < img_w) )
XX(i-1,j,1) = 255;
XX(i+1,j,1) = 255;
XX(i,j+1,1) = 255;
XX(i,j-1,1) = 255;
if( (i > 2) && (j > 2) && i+1 < img_h && j+1 < img_w-1)
XX(i,j-2,1) = 255;
XX(i-2,j,1) = 255;
XX(i,j+2,1) = 255;
XX(i+2,j,1) = 255;
end
end
end
end
end
subplot(2,2,2);
imshow(XX);
[output,serr] = sprintf('Q2_lenna_2.bmp');
imwrite(XX,output);
%============Q2的b小題 使用5x5的window 和non-maximal supppression==========
%==========================================================================
exx = conv2(dxx,add5,'same') ;
eyy = conv2(dyy,add5,'same') ;
exy = conv2(dxy,add5,'same') ;
k = 0.15;
R = zeros(img_h,img_w);
corner = zeros(img_h,img_w,'uint8');
%==================計算出R(A)=========================
for i= 1:img_h
for j=1:img_w
detA = exx(i,j) * eyy(i,j) - exy(i,j)^2;
trA = exx(i,j) + eyy(i,j);
R(i,j) = detA-k*trA^2;
end
end
%================non-maximal suppression================
for i= 2:img_h-1
for j = 2:img_w-1
if( R(i,j) > 50000 )
if( R(i,j) > R(i-1,j-1) && R(i,j) > R(i-1,j) && R(i,j) > R(i-1,j+1) )
if( R(i,j) > R(i,j-1) && R(i,j) > R(i,j+1) )
if( R(i,j) > R(i+1,j-1) && R(i,j) > R(i+1,j) && R(i,j) > R(i+1,j+1) )
corner(i,j) = 255;
end
end
end
end
end
end
XX = X;
%================在corner的地方標記'十'=========================
for i=1:img_h
for j=1:img_w
if( corner(i,j)==255 )
XX(i,j,1) = 255;
if( (i > 1) & (j > 1) & (i < img_h) & (j < img_w) )
XX(i-1,j,1) = 255;
XX(i+1,j,1) = 255;
XX(i,j+1,1) = 255;
XX(i,j-1,1) = 255;
if( (i > 2) && (j > 2) && i+1 < img_h && j+1 < img_w-1)
XX(i,j-2,1) = 255;
XX(i-2,j,1) = 255;
XX(i,j+2,1) = 255;
XX(i+2,j,1) = 255;
end
end
end
end
end
subplot(2,2,3);
imshow(XX);
[output,serr] = sprintf('Q2_lenna_3.bmp');
imwrite(XX,output);
%============Q2的b小題 使用9x9的window 和non-maximal supppression==========
%==========================================================================
exx = conv2(dxx,add9,'same') ;
eyy = conv2(dyy,add9,'same') ;
exy = conv2(dxy,add9,'same') ;
k = 0.15;
R = zeros(img_h,img_w);
corner = zeros(img_h,img_w,'uint8');
%==================計算出R(A)=========================
for i= 1:img_h
for j=1:img_w
detA = exx(i,j) * eyy(i,j) - exy(i,j)^2;
trA = exx(i,j) + eyy(i,j);
R(i,j) = detA-k*trA^2;
end
end
%================non-maximal suppression================
for i= 2:img_h-1
for j = 2:img_w-1
if( R(i,j) > 50000 )
if( R(i,j) > R(i-1,j-1) && R(i,j) > R(i-1,j) && R(i,j) > R(i-1,j+1) )
if( R(i,j) > R(i,j-1) && R(i,j) > R(i,j+1) )
if( R(i,j) > R(i+1,j-1) && R(i,j) > R(i+1,j) && R(i,j) > R(i+1,j+1) )
corner(i,j) = 255;
end
end
end
end
end
end
XX = X;
%================在corner的地方標記'十'=========================
for i=1:img_h
for j=1:img_w
if( corner(i,j)==255 )
XX(i,j,1) = 255;
if( (i > 1) & (j > 1) & (i < img_h) & (j < img_w) )
XX(i-1,j,1) = 255;
XX(i+1,j,1) = 255;
XX(i,j+1,1) = 255;
XX(i,j-1,1) = 255;
if( (i > 2) && (j > 2) && i+1 < img_h && j+1 < img_w-1)
XX(i,j-2,1) = 255;
XX(i-2,j,1) = 255;
XX(i,j+2,1) = 255;
XX(i+2,j,1) = 255;
end
end
end
end
end
subplot(2,2,4);
imshow(XX);
[output,serr] = sprintf('Q2_lenna_4.bmp');
imwrite(XX,output);
Q2.rar_detector
版权申诉
20 浏览量
2022-09-23
08:08:42
上传
评论
收藏 1KB RAR 举报
刘良运
- 粉丝: 67
- 资源: 1万+