matlab自编腐蚀算法,区域增长

2024-09-02 06:20

本文主要是介绍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自编腐蚀算法,区域增长的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1129240

相关文章

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

Python中的随机森林算法与实战

《Python中的随机森林算法与实战》本文详细介绍了随机森林算法,包括其原理、实现步骤、分类和回归案例,并讨论了其优点和缺点,通过面向对象编程实现了一个简单的随机森林模型,并应用于鸢尾花分类和波士顿房... 目录1、随机森林算法概述2、随机森林的原理3、实现步骤4、分类案例:使用随机森林预测鸢尾花品种4.1

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

综合安防管理平台LntonAIServer视频监控汇聚抖动检测算法优势

LntonAIServer视频质量诊断功能中的抖动检测是一个专门针对视频稳定性进行分析的功能。抖动通常是指视频帧之间的不必要运动,这种运动可能是由于摄像机的移动、传输中的错误或编解码问题导致的。抖动检测对于确保视频内容的平滑性和观看体验至关重要。 优势 1. 提高图像质量 - 清晰度提升:减少抖动,提高图像的清晰度和细节表现力,使得监控画面更加真实可信。 - 细节增强:在低光条件下,抖