【影象處理】MATLAB:點、線、邊緣檢測
阿新 • • 發佈:2019-01-26
點檢測
程式碼示例
f = imread('test_pattern_with_single_pixel.tif');
w = [-1 -1 -1;-1 8 -1;-1 -1 -1]; % 點檢測掩模
g = abs(imfilter(double(f),w));
T = max(g(:));
g = g>=T;
subplot(1,2,1);imshow(f);title('原影象');
subplot(1,2,2);imshow(g);title('點檢測');
執行結果
線檢測
程式碼示例
f = imread('wirebond_mask.tif' ); % 影象大小:486×486
w = [2 -1 -1;-1 2 -1;-1 -1 2]; % -45°方向檢測線
g = imfilter(double(f),w);
gtop = g(1:120,1:120); % 左上角區域
gtop = pixeldup(gtop,4); % 通過複製畫素將影象擴大gtop*4倍
gbot = g(end-119:end,end-119:end); % 右下角區域
gbot = pixeldup(gbot,4);
g1 = abs(g); % 檢測圖的絕對值
T = max(g1(:));
g2 = g1>=T;
subplot(3,2,1);imshow(f);title('原影象');
subplot(3,2,2);imshow(g,[]);title('線檢測-45°方向');
subplot(3,2,3);imshow(gtop,[]);title('左上角區域');
subplot(3,2,4);imshow(gbot,[]);title('右下角區域');
subplot(3,2,5);imshow(g1,[]);title('影象絕對值');
subplot(3,2,6);imshow(g2);title('g>=T' );
執行結果
補充
使用Hough變換作線檢測:
Hough變換是一種尋找並連結影象中線段的處理方式,用其進行線檢測和連結的第一步是峰值檢測。
邊緣檢測
程式碼示例
f = imread('bld.tif');
[g_sobel_default,ts] = edge(f,'sobel');
[g_log_default,tlog] = edge(f,'log');
[g_canny_default,tc] = edge(f,'canny');
g_sobel_best = edge(f,'sobel',0.05);
g_log_best = edge(f,'log',0.003,2.25);
g_canny_best = edge(f,'canny',[0.04 0.10],1.5);
subplot(3,2,1);imshow(g_sobel_default);title('預設sobel');
subplot(3,2,2);imshow(g_sobel_best);title('預設log');
subplot(3,2,3);imshow(g_log_default);title('預設canny');
subplot(3,2,4);imshow(g_log_best);title('最佳sobel');
subplot(3,2,5);imshow(g_canny_default);title('最佳log');
subplot(3,2,6);imshow(g_canny_best);title('最佳canny');
執行結果