1. 程式人生 > >自定義結構元的膨脹,腐蝕處理

自定義結構元的膨脹,腐蝕處理

myImageED.m

function img2 = myImageED(type, binaryImage, Struct, pos) %支援正方形和長方形,奇數、偶數結構元、自定義中心點的膨脹和腐蝕 %輸入:type為型別,binaryImage為輸入圖片,Struct為結構元, pos為中心點 %支援正方形和長方形,支援奇數偶數結構元,支援自定義中心點 %作者:鍾海瀟原創 %學校:仲愷 B = Struct; x = pos(1); y = pos(2); [M,N] = size(binaryImage); [K,L] = size(Struct); down = abs(K-x);           %上下左右各加多少 up = abs(1-x); right = abs(L-y); left = abs(1-y); A = zeros(M+K-1, N+L-1);    %豎方向擴大K-1,橫方向擴大L-1 A(1+up:M+K-down-1,1+left:N+L-right-1) = binaryImage;  %補圖 img1 = A;   %存圖     switch type         case 'swell'             for i = 1+up:M+K-down-1                for j = 1+left:N+L-right-1                   tmp = A(i-up:i+down,j-left:j+right);   %擷取圖片                   C = tmp.*B;                            %點乘                   s = sum(sum(C));                       %求和                   if(s > 1)                              %擴大白色                       img1(i,j) = 1;                   else                       img1(i,j) = 0;                   end                end             end         case 'corrosion'             for i = 1+up:M+K-down-1                for j = 1+left:N+L-right-1                   tmp = A(i-up:i+down,j-left:j+right);   %擷取圖片                   C = tmp.*B;                            %點乘                   s = sum(sum(C));                       %求和                   sB = sum(sum(B));                      %模板求和                   if(s < sB)                             %減少白色                       img1(i,j) = 0;                   else                       img1(i,j) = 1;                   end                end             end     end img2 = img1(1+up:M+K-down-1,1+left:N+L-right-1);        %縮小 end

T2.m

clc; clear all; img1 = imread('EXP6-2.tif'); img2 = imread('EXP6-1.bmp');

Struct_1 = [0 0 1 0 0; 0 0 1 0 0; 1 1 1 1 1; 0 0 1 0 0; 0 0 1 0 0]; pos_1 = [3 3]; Struct_2 = [0 0 1 0; 0 0 1 0; 1 1 1 1; 0 0 1 0]; pos_2 = [2 3];

img1_1 = myOtsu(img1, 'black'); figure(1); subplot(335);imshow(img1_1.*256);title('原圖');

img1_2 = myImageED('swell', img1_1, Struct_1, pos_1); img1_3 = myImageED('corrosion', img1_1, Struct_1, pos_1); subplot(331);imshow(img1_2);title('5x5(3,3)膨脹'); subplot(333);imshow(img1_3);title('5x5(3,3)腐蝕');

img1_4 = myImageED('swell', img1_1, Struct_2, pos_2); img1_5 = myImageED('corrosion', img1_1, Struct_2, pos_2); subplot(334);imshow(img1_4);title('4x4(2,3)膨脹'); subplot(336);imshow(img1_5);title('4x4(2,3)腐蝕');

img1_6 = myImageED('swell', img1_3, Struct_1, pos_1); img1_7 = myImageED('sorrosion', img1_2, Struct_1, pos_1); subplot(337);imshow(img1_6.*255);title('5x5(3,3)開操作'); subplot(339);imshow(img1_7.*255);title('5x5(3,3)閉操作');

img2_1 = myOtsu(img2, 'while'); figure(2); subplot(335);imshow(img2_1.*256);title('原圖');

img2_2 = myImageED('swell', img2_1, Struct_1, pos_1); img2_3 = myImageED('corrosion', img2_1, Struct_1, pos_1); subplot(331);imshow(img2_2);title('5x5(3,3)膨脹'); subplot(333);imshow(img2_3);title('5x5(3,3)腐蝕');

img2_4 = myImageED('swell', img2_1, Struct_2, pos_2); img2_5 = myImageED('corrosion', img2_1, Struct_2, pos_2); subplot(334);imshow(img2_4);title('4x4(2,3)膨脹'); subplot(336);imshow(img2_5);title('4x4(2,3)腐蝕');

img2_6 = myImageED('swell', img2_3, Struct_1, pos_1); img2_7 = myImageED('sorrosion', img2_2, Struct_1, pos_1); subplot(337);imshow(img2_6.*255);title('5x5(3,3)開操作'); subplot(339);imshow(img2_7.*255);title('5x5(3,3)閉操作');