介绍MATLAB一种算法实现方式

介绍MATLAB一种算法实现方式

本篇文章要分享的是基于MATLAB的腐蚀膨胀算法实现,腐蚀膨胀是形态学

图像处理的基础,腐蚀在二值图像的基础上做“收缩”或“细化”操作,膨胀在二值图像的基础上做“加长”或“变粗”的操作。

什么是二值图像呢?把一幅图片看做成一个二维的数组,那么二值图像是一个只有0和1的逻辑数组,我们前面Sobel边缘检测后的图像输出边缘效果,设置个阈值,大于阈值输出为1,小于阈值输出为0,最后输出就是一幅二值图像了。

腐蚀腐蚀是一种消除边界点,使边界向内部收缩的过程。可以用来消除小且无意义的物体。用3X3的结构元素,扫描图像的每一个像素,用结构元素与其覆盖的二值图像做“与”操作,如果都为1,结果图像的该像素为1。否则为0。结果会使二值图像小一圈。 有一个形象的比喻来可以说明该运算,用0表示蛀虫,1表示大米。蛀虫腐蚀大米的过程便是腐蚀运算, 腐蚀

如图所示,对于一个像素矩阵而言,只要有蛀虫(0)的存在,大米(1)就会被腐蚀掉了,即使只存在一个蛀虫(0),但是还是会被蛀虫腐蚀完毕,最后一幅图上面由于没有蛀虫(0)所以大米完好无损。

关于算法的实现,可以用下式子来表示,即3x3像素的运算:

P = P11 3 clear all; 4 close all; 5 6 RGB_data = imread(lena.jpg); 7 8 R_data = RGB_data(:,:,1); 9 G_data = RGB_data(:,:,2); 10 B_data = RGB_data(:,:,3); 11 12 %imshow(RGB_data); 13 14 [ROW,COL, DIM] = size(RGB_data); 15 16 Y_data = zeros(ROW,COL); 17 Cb_data = zeros(ROW,COL); 18 Cr_data = zeros(ROW,COL); 19 Gray_data = RGB_data; 20 21 for r = 1:ROW 22 for c = 1:COL 23 Y_data(r, c) = 0.299*R_data(r, c) + 0.587*G_data(r, c) + 0.114*B_data(r, c); 24 Cb_data(r, c) = -0.172*R_data(r, c) - 0.339*G_data(r, c) + 0.511*B_data(r, c) + 128; 25 Cr_data(r, c) = 0.511*R_data(r, c) - 0.428*G_data(r, c) - 0.083*B_data(r, c) + 128; 26 end 27 end 28 29 Gray_data(:,:,1)=Y_data; 30

Gray_data(:,:,2)=Y_data; 31 Gray_data(:,:,3)=Y_data; 32 33 figure; 34 imshow(Gray_data); 35 36 %Median Filter 37 imgn = imnoise(Gray_data,salt ,0.02); 38 39 figure; 40 imshow(imgn); 41 42 Median_Img = Gray_data; 43 for r = 2:ROW-1 44 for c = 2:COL-1 45 median3x3 =[imgn(r-1,c-1) imgn(r-1,c) imgn(r-1,c+1) 46 imgn(r,c-1) imgn(r,c) imgn(r,c+1) 47 imgn(r+1,c-1) imgn(r+1,c) imgn(r+1,c+1)]; 48 sort1 = sort(median3x3, 2, descend); 49 sort2 = sort([sort1(1), sort1(4), sort1(7)], descend); 50 sort3 = sort([sort1(2), sort1(5), sort1(8)], descend); 51 sort4 = sort([sort1(3), sort1(6), sort1(9)], descend); 52 mid_num = sort([sort2(3), sort3(2), sort4(1)], descend); 53 Median_Img(r,c) = mid_num(2); 54 end 55 end 56 57 figure; 58 imshow(Median_Img); 59 60 %Sobel_Edge_Detect 61 62 Median_Img = double(Median_Img); 63 Sobel_Threshold = 150; 64 Sobel_Img = zeros(ROW,COL); 65 66 for r = 2:ROW-1 67 for c = 2:COL-1 68 Sobel_x = Median_Img(r-1,c+1) + 2*Median_Img(r,c+1) + Median_Img(r+1,c+1) - Median_Img(r-1,c-1) - 2*Median_Img(r,c-1) - Median_Img(r+1,c-1); 69 Sobel_y = Median_Img(r-1,c-1) + 2*Median_Img(r-1,c) + Median_Img(r-1,c+1) - Median_Img(r+1,c-1) - 2*Median_Img(r+1,c) - Median_Img(r+1,c+1); 70 Sobel_Num = abs(Sobel_x) + abs(Sobel_y); 71 %Sobel_Num = sqrt(Sobel_x?? + Sobel_y??); 72 if(Sobel_Num > Sobel_Threshold) 73 Sobel_Img(r,c)=255; 74 else 75 Sobel_Img(r,c)=0; 76 end 77 end 78 end 79 80 figure; 81 imshow(Sobel_Img); 82 83 %imopen Erosion_Dilation 84 %Erosion 85 % Erosion_img = zeros(ROW,COL); 86 % for r = 2:ROW-1 87 % for c = 2:COL-1 88 % and1 = bitand(Sobel_Img(r-1, c-1), bitand(Sobel_Img(r-1, c), Sobel_Img(r-1, c+1))); 89 % and2 = bitand(Sobel_Img(r, c-1), bitand(Sobel_Img(r, c), Sobel_Img(r, c+1))); 90 % and3 = bitand(Sobel_Img(r+1, c-1), bitand(Sobel_Img(r+1, c), Sobel_Img(r+1, c+1))); 91 % Erosion_img(r, c) = bitand(and1, bitand(and2, and3)); 92 % end 93 % end 94 95 % figure; 96 % imshow(Erosion_img); 97 98 % %Dilation 99 % Dilation_img = zeros(ROW,COL);100 % for r = 2:ROW-1101 % for c = 2:COL-1102 % or1 = bitor(Erosion_img(r-1, c-1), bitor(Erosion_img(r-1, c), Erosion_img(r-1, c+1)));103 % or2 = bitor(Erosion_img(r, c-1), bitor(Erosion_img(r, c), Erosion_img(r, c+1)));104 % or3 = bitor(Erosion_img(r+1, c-1), bitor(Erosion_img(r+1, c), Erosion_img(r+1, c+1)));105 % Dilation_img(r, c) = bitor(or1, bitor(or2, or3));106 % end107 % end108 109 % figure;110 % imshow(Dilation_img);111

112 %imclose Erosion_Dilation113 %Dilation114 Dilation_img = zeros(ROW,COL);115 for r = 2:ROW-1116 for c = 2:COL-1117 or1 = bitor(Sobel_Img(r-1, c-1), bitor(Sobel_Img(r-1, c), Sobel_Img(r-1, c+1)));118 or2 = bitor(Sobel_Img(r, c-1), bitor(Sobel_Img(r, c), Sobel_Img(r, c+1)));119 or3 = bitor(Sobel_Img(r+1, c-1), bitor(Sobel_Img(r+1, c), Sobel_Img(r+1, c+1)));120 Dilation_img(r, c) = bitor(or1, bitor(or2, or3));121 end122 end123 124 figure;125 imshow(Dilation_img);126 127 %Erosion128 Erosion_img = zeros(ROW,COL);129 for r = 2:ROW-1130 for c = 2:COL-1131 and1 = bitand(Dilation_img(r-1, c-1), bitand(Dilation_img(r-1, c), Dilation_img(r-1, c+1)));132 and2 = bitand(Dilation_img(r, c-1), bitand(Dilation_img(r, c), Dilation_img(r, c+1)));133 and3 = bitand(Dilation_img(r+1, c-1), bitand(Dilation_img(r+1, c), Dilation_img(r+1, c+1)));134 Erosion_img(r, c) = bitand(and1, bitand(and2, and3));135 end136 end137 138 figure;139 imshow(Erosion_img);处理后结果对比 为了可以清晰的看到图像边缘的变化,我们把黑色作为背景,白色作为边缘。

Sobel边缘检测后的lena

腐蚀后的lena

膨胀后的lena

先腐蚀后膨胀开运算lena

先膨胀后腐蚀闭运算lena

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4