本文主要是介绍matlab自编腐蚀算法,区域增长,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 自实现腐蚀操作
temp1 = ImgMask;
se = strel('disk',4);
temp1 = imerode(temp1, se);
imtool(temp1)temp2 = temp2.*label;
temp2 = ImgMask;
my_strel = double(se.Neighborhood);
R = floor(size(my_strel,1)/2);
label = zeros(imgSize,imgSize);
for i = R+1:imgSize - Rfor j = R+1:imgSize - RsubImg = temp2(i-R:i+R,j-R:j+R);if isequal(subImg.* my_strel,my_strel)label(i,j) = 1;endend
end
temp2 = temp2.*label;
2 自实现二值图区域增长,完成分割任务
多次循环,实现任何大小的分割任务
WINDOW_SIZE_Standard = 240; %最大膨胀窗口的大小
threshhold_Standard = 0.3; %区域增长的判定条件,每次新增长像素>待判定像素的 30%
[output,WINDOW_ARRAY_fn,WINDOW_SIZE,seed_x,seed_y] = RegionGrowingStepInitial(tempMaskimg,imgSize,WINDOW_SIZE_Standard,threshhold_Standard);WINDOW_SIZE_Standard = 40; threshhold_Standard = 0.4;
[output,WINDOW_SIZE] = RegionGrowingStep2(tempMaskimg,output,WINDOW_SIZE_Standard,WINDOW_SIZE,threshhold_Standard,imgSize);WINDOW_SIZE_Standard = 20; threshhold_Standard = 0.4;
[output,WINDOW_SIZE] = RegionGrowingStep2(tempMaskimg,output,WINDOW_SIZE_Standard,WINDOW_SIZE,threshhold_Standard,imgSize);WINDOW_SIZE_Standard = 20; threshhold_Standard = 0.4;
[output,WINDOW_SIZE] = RegionGrowingStep2(tempMaskimg,output,WINDOW_SIZE_Standard,WINDOW_SIZE,threshhold_Standard,imgSize);
function [output,WINDOW_SIZE] = RegionGrowingStep2(tempMaskimg,output,WINDOW_SIZE_Standard,WINDOW_SIZE,threshhold_Standard,imgSize)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%3.选取多个种子点,限定增长窗口,区域增长
%Select multiple seed points, limit the growth window, and expand the region
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% 3.1.存储多个种子点P0 = tempMaskimg;% % [posx,posy] = find (WINDOW_ARRAY_fn==1);
% % seedArray_x = [];
% % seedArray_y = [];
% % for i = seed_x - floor(WINDOW_SIZE/2):floor(WINDOW_SIZE/10-1):seed_x + floor(WINDOW_SIZE/2)
% % for j = seed_y - floor(WINDOW_SIZE/2):floor(WINDOW_SIZE/10-1):seed_y + floor(WINDOW_SIZE/2)
% % seedArray_x(end+1) = i;
% % seedArray_y(end+1) = j;
% % end
% % endoutput(output > 0) = 1;edges = edge(output, 'Canny');[posx,posy] = find(edges ~= 0);seedArray_x = [];seedArray_y = [];for i = 1:23:size(posx,1)seedArray_x(end+1) = posx(i);seedArray_y(end+1) = posy(i);end
% figure(1);plot(seedArray_x,seedArray_y,'*');
% figure(2);plot(posx,posy,'*');%%%%%%%%%% 3.2 指定滑动窗大小,%%%%%%%%%占比0.4以上,增长fprintf("************** seed point length= %d \n",size(seedArray_x,2));for kk = 1:size(seedArray_x,2)WINDOW_SIZE = 3;WINDOW_ARRAY_fn = zeros(imgSize,imgSize);seed_x = seedArray_x(kk);seed_y = seedArray_y(kk);for i = seed_x - floor(WINDOW_SIZE/2):1:seed_x + floor(WINDOW_SIZE/2)for j = seed_y - floor(WINDOW_SIZE/2):1:seed_y + floor(WINDOW_SIZE/2)WINDOW_ARRAY_fn(i,j) = 1;endendWINDOW_ARRAY_fn(seed_x,seed_y) = 0;threshhold = threshhold_Standard;
% while threshhold >= 0.4 && WINDOW_SIZE < 50while threshhold >= threshhold_Standard && WINDOW_SIZE < WINDOW_SIZE_StandardTOTAL = WINDOW_SIZE*2 + (WINDOW_SIZE - 2)*2;result = P0.*WINDOW_ARRAY_fn; %imtool(result)[posx,posy] = find (result==1);SATISFIED_pixel = length(posx); threshhold = SATISFIED_pixel/TOTAL;%%%%%%% old windowWINDOW_ARRAY_fn = zeros(imgSize,imgSize);for i = seed_x - floor(WINDOW_SIZE/2):1:seed_x + floor(WINDOW_SIZE/2)for j = seed_y - floor(WINDOW_SIZE/2):1:seed_y + floor(WINDOW_SIZE/2)WINDOW_ARRAY_fn(i,j) = 1;endend%%%%% new windowWINDOW_SIZE = WINDOW_SIZE+ 2;WINDOW_ARRAY_fn0 = zeros(imgSize,imgSize);for i = seed_x - floor(WINDOW_SIZE/2):1:seed_x + floor(WINDOW_SIZE/2)for j = seed_y - floor(WINDOW_SIZE/2):1:seed_y + floor(WINDOW_SIZE/2)WINDOW_ARRAY_fn0(i,j) = 1;endendWINDOW_ARRAY_fn0(WINDOW_ARRAY_fn == 1) = 0;WINDOW_ARRAY_fn0(seed_x,seed_y) = 0;WINDOW_ARRAY_fn = WINDOW_ARRAY_fn0;% fprintf("WINDOW_SIZE = %d \n",WINDOW_SIZE);endWINDOW_ARRAY_out = zeros(imgSize,imgSize);for i = seed_x - floor(WINDOW_SIZE/2):1:seed_x + floor(WINDOW_SIZE/2)for j = seed_y - floor(WINDOW_SIZE/2):1:seed_y + floor(WINDOW_SIZE/2)WINDOW_ARRAY_out(i,j) = 1;endendoutput = output + WINDOW_ARRAY_out;% imtool(output);
% % fprintf("************** kk = %d \n",kk);
% % fprintf("WINDOW_SIZE = %d \n",WINDOW_SIZE);endfprintf("************** finish \n");
end
function [output,WINDOW_ARRAY_fn,WINDOW_SIZE,seed_x,seed_y] = RegionGrowingStepInitial(tempMaskimg,imgSize,WINDOW_SIZE_Standard,threshhold_Standard)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 1.在图像的中心区域的一定范围内,选取一个像素值不为0的种子点%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% R_seed = 30;seed_x = 0;seed_y = 0;for i = floor(imgSize/2 - R_seed/2):1:floor(imgSize/2 + R_seed/2)for j = floor(imgSize/2 - R_seed/2):1:floor(imgSize/2 + R_seed/2)if(tempMaskimg(i,j) ==1)seed_x = i;seed_y = j;break;endendend%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2.区域增长%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%% 初始化P0 = tempMaskimg;WINDOW_SIZE = 3; WINDOW_ARRAY_fn = zeros(imgSize,imgSize);for i = seed_x - floor(WINDOW_SIZE/2):1:seed_x + floor(WINDOW_SIZE/2)for j = seed_y - floor(WINDOW_SIZE/2):1:seed_y + floor(WINDOW_SIZE/2)WINDOW_ARRAY_fn(i,j) = 1;endendWINDOW_ARRAY_fn(seed_x,seed_y) = 0;threshhold = threshhold_Standard;%%%%%%%%%%%% 获得初始连通区域
% while threshhold >= 0.3 && WINDOW_SIZE < 240while threshhold >= threshhold_Standard && WINDOW_SIZE < WINDOW_SIZE_StandardTOTAL = WINDOW_SIZE*2 + (WINDOW_SIZE - 2)*2;result = P0.*WINDOW_ARRAY_fn; %imtool(result)[posx,posy] = find (result==1);SATISFIED_pixel = length(posx); threshhold = SATISFIED_pixel/TOTAL;%%%%%%% old windowWINDOW_ARRAY_fn = zeros(imgSize,imgSize);for i = seed_x - floor(WINDOW_SIZE/2):1:seed_x + floor(WINDOW_SIZE/2)for j = seed_y - floor(WINDOW_SIZE/2):1:seed_y + floor(WINDOW_SIZE/2)WINDOW_ARRAY_fn(i,j) = 1;endend%%%%% new windowWINDOW_SIZE = WINDOW_SIZE+ 2;WINDOW_ARRAY_fn0 = zeros(imgSize,imgSize);for i = seed_x - floor(WINDOW_SIZE/2):1:seed_x + floor(WINDOW_SIZE/2)for j = seed_y - floor(WINDOW_SIZE/2):1:seed_y + floor(WINDOW_SIZE/2)WINDOW_ARRAY_fn0(i,j) = 1;endendWINDOW_ARRAY_fn0(WINDOW_ARRAY_fn == 1) = 0;WINDOW_ARRAY_fn0(seed_x,seed_y) = 0;WINDOW_ARRAY_fn = WINDOW_ARRAY_fn0;% fprintf("WINDOW_SIZE = %d \n",WINDOW_SIZE);endWINDOW_ARRAY_out = zeros(imgSize,imgSize);for i = seed_x - floor(WINDOW_SIZE/2):1:seed_x + floor(WINDOW_SIZE/2)for j = seed_y - floor(WINDOW_SIZE/2):1:seed_y + floor(WINDOW_SIZE/2)WINDOW_ARRAY_out(i,j) = 1;endendoutput = WINDOW_ARRAY_out;
end
这篇关于matlab自编腐蚀算法,区域增长的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!