OTSU自适应阈值求法与粒子群算法的合作,将OTSU算法作为粒子群算法的适应值函数,来计算每个粒子的适应度与最优阈值相比较,经过3000次迭代最后取得优化后的阈值

原图:

经过联合算法优化的双阈值为90 ,140

将背景像素置0:

效果图:

利用所取得的阈值就可以将图像背景和目标区分开来,利用所得阈值二值化后

效果图:

通过效果图可知将人这个目标从背景中分割出来了

源代码:

[cpp] view plaincopy
  1. #include "stdafx.h"
  2. #include "cv.h"
  3. #include "highgui.h"
  4. #include "cxcore.h"
  5. #include "time.h"
  6. using namespace std;
  7. #define rnd( low,uper) ((int)(((double)rand()/(double)RAND_MAX)*((double)(uper)-(double)(low))+(double)(low)+0.5))
  8. /*************************************************************8888
  9. 粒子群算法变量的说明
  10. ******************************************************************************/
  11. const int number = 20;
  12. int antThreshold[number][2];//以阈值作为粒子
  13. int vect[number][2];//更新的速度
  14. float pbest[number] = {0.0};;//每个粒子历史最优解
  15. float gbest = 0.0;//全局历史最优解
  16. int pbestThreshold[number][2];//每个粒子的最优历史阈值
  17. int gbestThreshold[2];//全局粒子的最优阈值
  18. float w = 0.9;//惯性因子
  19. float c1 = 2.0;//加速因子1
  20. float c2 = 2.0;//加速因子2
  21. //histogram
  22. float histogram[256]={0};
  23. /*********************************************************************8888
  24. 函数名:GetAvgValue
  25. 参数类型:IplImage* src
  26. 实现功能:获得灰度图像的总平均灰度值
  27. *****************************************************************************/
  28. float GetAvgValue(IplImage* src)
  29. {
  30. int height=src->height;
  31. int width=src->width;
  32. for(int i=0;i<height;i++)
  33. {
  34. unsigned char* p=(unsigned char*)src->imageData+src->widthStep*i;
  35. for(int j=0;j<width;j++)
  36. {
  37. histogram[*p++]++;
  38. }
  39. }
  40. //normalize histogram
  41. int size=height*width;
  42. for(int i=0;i<256;i++) {
  43. histogram[i]=histogram[i]/size;
  44. }
  45. //average pixel value
  46. float avgValue=0;
  47. for(int i=0;i<256;i++) {
  48. avgValue+=i*histogram[i];
  49. }
  50. return avgValue;
  51. }
  52. /*****************************************************************************
  53. 函数名:ThresholdOTSU
  54. 参数类型:int threshold1 , int threshold2 , float avgValue
  55. 功能:求得最大类间方差
  56. **********************************************************************************/
  57. float  ThresholdOTSU(int threshold1 , int threshold2 , float avgValue)
  58. {
  59. int threshold;
  60. float maxVariance=0;
  61. float w=0,u=0;
  62. for(int i=threshold1;i< threshold2 ;i++)
  63. {
  64. w+=histogram[i];
  65. u+=i*histogram[i];
  66. }
  67. float t=avgValue*w-u;
  68. float variance=t*t/(w*(1-w));
  69. /* if(variance>maxVariance)
  70. {
  71. maxVariance=variance;
  72. threshold=i;
  73. }
  74. */
  75. return variance;
  76. }
  77. /*****************************************************************
  78. 函数名:Init
  79. 参数类型:void
  80. 功能:初始化粒子群算法的粒子与速度
  81. ************************************************************************/
  82. void Init()
  83. {
  84. for(int index=0;index<number;index++)
  85. {
  86. antThreshold[index][0] = rnd(10 , 50);
  87. antThreshold[index][1] = antThreshold[index][0] + 50;
  88. if(antThreshold[index][1]>255)
  89. antThreshold[index][1] = 255;
  90. vect[index][0] = rnd(3 ,5);
  91. vect[index][1] = rnd(3 ,5);
  92. }
  93. }
  94. /******************************************************************
  95. 函数名:Pso
  96. 参数类型:void
  97. 功能:粒子群算法的实现
  98. ***************************************************************************/
  99. void Pso(float value)
  100. {
  101. for(int index=0;index<number;index++)
  102. {
  103. float variance;
  104. variance = ThresholdOTSU(antThreshold[index][0] , antThreshold[index][1] , value);
  105. if(variance>pbest[index])
  106. {
  107. pbest[index] = variance;
  108. pbestThreshold[index][0] = antThreshold[index][0];
  109. pbestThreshold[index][1] = antThreshold[index][1];
  110. }
  111. if(variance>gbest)
  112. {
  113. gbest = variance;
  114. gbestThreshold[0] = antThreshold[index][0];
  115. gbestThreshold[1] = antThreshold[index][1];
  116. }
  117. }
  118. }
  119. /***************************************************************************************88
  120. 函数名:updateData
  121. 参数类型:void
  122. 功能:更新粒子数据与速度
  123. **************************************************************************************************/
  124. void updateData()
  125. {
  126. for(int index=0;index<number;index++)
  127. {
  128. for(int i=0;i<2;i++)
  129. {
  130. vect[index][i] = w*vect[index][i] + c1*((double)(rand())/(double)RAND_MAX)*(pbestThreshold[index][i]-antThreshold[index][i])+
  131. c2*c1*((double)(rand())/(double)RAND_MAX)*(gbestThreshold[i]-antThreshold[index][i]);
  132. if(vect[index][i]>5)
  133. vect[index][i] = 5;
  134. if(vect[index][i]<3)
  135. vect[index][i] = 3;
  136. antThreshold[index][i] = vect[index][i] + antThreshold[index][i];
  137. }
  138. if(antThreshold[index][0]>antThreshold[index][1])
  139. antThreshold[index][1] = antThreshold[index][0] + 50;
  140. if(antThreshold[index][1]>255)
  141. antThreshold[index][1] = 255;
  142. if(antThreshold[index][0]<0)
  143. antThreshold[index][0] = 0;
  144. }
  145. }
  146. /**************************************************************8
  147. 函数名:Threshold
  148. 参数类型:IplImage *src , int lower , int higher
  149. 功能:利用算法得到的双阈值对图像进行阈值分割
  150. ***********************************************************************/
  151. void Threshold(IplImage *src , int lower , int higher)
  152. {
  153. assert(src->nChannels==1);
  154. for(int h=0;h<src->height;h++)
  155. for(int w=0;w<src->width;w++)
  156. {
  157. if(*(src->imageData+h*src->widthStep+w)<higher&&*(src->imageData+h*src->widthStep+w)>lower)
  158. //*(src->imageData+h*src->widthStep+w) = 255;
  159. ;
  160. else
  161. *(src->imageData+h*src->widthStep+w) = 0;
  162. }
  163. }
  164. int _tmain(int argc, _TCHAR* argv[])
  165. {
  166. srand((unsigned)time(NULL));
  167. IplImage *img =0;
  168. IplImage *ycrcb = 0;
  169. IplImage *cb = 0;
  170. cvNamedWindow("cb" , 1);
  171. img = cvLoadImage("1.jpg" , 1);
  172. ycrcb = cvCreateImage(cvGetSize(img) , 8 ,3);
  173. cb = cvCreateImage(cvGetSize(img) , 8 , 1);
  174. cvCvtColor(img , ycrcb , CV_BGR2YCrCb);
  175. cvSplit(ycrcb , 0 ,0,cb , 0);
  176. cvSmooth(cb , cb , CV_MEDIAN , 3 , 0,0,0);
  177. float avgValue = 0.0;
  178. avgValue = GetAvgValue(cb);
  179. Init();
  180. for(int i=0;i<3000;i++)
  181. {
  182. Pso(avgValue);
  183. updateData();
  184. }
  185. //cvThreshold(cb , cb , gbestThreshold[0] , gbestThreshold[1] , CV_THRESH_BINARY);
  186. Threshold(cb , gbestThreshold[0] , gbestThreshold[1]);
  187. printf("%d , %d\n" ,  gbestThreshold[0] , gbestThreshold[1]);
  188. cvShowImage("cb" , cb);
  189. cvSaveImage("cb1.jpg" ,cb);
  190. cvWaitKey(0);
  191. return 0;
  192. }

网址:http://blog.csdn.net/qinjianganying/article/details/6747980

基于OTSU算法和基本粒子群优化算法的双阈值图像分割相关推荐

  1. 智能优化算法:蛾群优化算法-附代码

    智能优化算法:蛾群优化算法 摘要:蛾群算法 (Moth Swarm Algorithm, MSA)是 Al-Attar AliMohamed 于2016年提出的新兴元启发式智能优化算法,它模拟自然界蛾 ...

  2. 【优化算法】猫群优化算法(CSO)【含Matlab源码 1071期】

    ⛄一.获取代码方式 获取代码方式1: 完整代码已上传我的资源:[优化算法]猫群优化算法(CSO)[含Matlab源码 1071期] 点击上面蓝色字体,直接付费下载,即可. 获取代码方式2: 付费专栏M ...

  3. matlab——智能算法之粒子群优化算法、模拟退火算法、遗传算法

    智能算法之粒子群优化算法: %% 初始化种群 f= % 函数表达式 % 求这个函数的最大值 figure(1);ezplot(f,[0,0.01,20]); N = 50; % 初始种群个数 d = ...

  4. 【优化算法】粒子群优化算法

    粒子群优化算法 粒子群优化算法简介 粒子群优化算法原理 粒子群优化算法的数学描述 粒子群优化算法框架 PySwarms:Python中粒子群优化的研究工具包 PySwarms快速使用 示例:编写自己的 ...

  5. 2018-3-28 基本粒子群优化算法

    学习资源: [图文]群智能理论及粒子群优化算法_百度文库 https://wenku.baidu.com/view/c855e622a5e9856a56126075.html 书本<智能算法以及 ...

  6. 【计算智能】——群体智能算法(蚁群优化算法ACO、粒子群优化算法PSO)

    群体智能算法 与大多数基于梯度的优化算法不同,群体智能算法依靠的是概率搜索算法. 与梯度算法及传统演化算法相比优点: 没有集中控制约束,不会因为个体的故障影响整个问题的求解. 以非直接信息交流的方式确 ...

  7. 揽货最短路径解决方案算法 - C# 蚁群优化算法实现

    需求为(自己编的,非实际项目): 某配送中心进行揽货,目标客户数为50个客户,配送中心目前的运力资源如下: 现有车辆5台 单台运力最大行驶距离200千米 单台运力最大载重公斤1吨 问:运力怎样走法才能 ...

  8. 遗传算法 差分进化算法 粒子群优化算法区别

    一 遗传算法 遗传算法(GA)作为一种经典的进化算法,自 Holland提出之后在国际上已经形成了一个比较活跃的研究领域. 人们对 GA 进行了大量的研究,提出了各种改进算法用于提高算法的收敛速度和精 ...

  9. 优化算法(四)——粒子群优化算法(PSO)

    粒子群算法(Particle Swarm Optimization,PSO)是一种模仿鸟群.鱼群觅食行为发展起来的一种进化算法.其概念简单易于编程实现且运行效率高.参数相对较少,应用非常广泛.粒子群算 ...

最新文章

  1. 4、jquery表格操作
  2. 传感器c语言开发,Android环境传感器开发教程
  3. torch view view_as
  4. 真正理解 MySQL 的四种隔离级别
  5. 2021-10-11 二叉树,二叉搜索树及其相关23个操作 C++实现笔记(复习用,含C指针复习)
  6. python环境_python环境搭建教程
  7. 浅谈域名发散与域名收敛
  8. JQuery中的ID选择器
  9. 超越MobileNet v3!华为诺亚、北大提出GhostNet​,使用线性变换生成特征图
  10. win10键盘全部没反应_Win10的键盘失灵解决办法
  11. Intel收购半导体设计公司eASIC
  12. 第一部分 第五章 数组 1102-1149
  13. WPF从零到1教程详解,适合新手上路
  14. Apache Ranger KMS 部署文档
  15. Reflex WMS入门系列二十四:拆托(HD Breakdown)
  16. 利用华硕路由器实现创维电视广告屏蔽
  17. Buffon投针实验
  18. 大学应用计算机二级,大学计算机二级ps考试试题内容(3)
  19. 正则看这三个网站就够了
  20. 智能分拣解决方案市场现状研究分析报告 -

热门文章

  1. xpath爬取当当网
  2. 三维重建3:旋转矩阵-病态矩阵、欧拉角-万向锁、四元数
  3. 用jsp的mvc模式的新闻发布系统_网易内部倡导用昵称代替哥姐总等称呼;TCL大股东误操作卖出500万股;Ant Design 4.6.2 发布| 极客头条...
  4. ubuntu 20.04 安装录屏软件 OBS 及卸载
  5. 利用Java反射机制调用含数组参数的方法
  6. 手机电商营销模式探讨
  7. 美国高考能不能带计算机,这件“神器”在美国被高中允许却被美国大学禁止
  8. Ubuntu Core 将支持物联网 Matter
  9. Flutter版讯飞语音识别demo
  10. springboot整合redis、mybatis、@EnableScheduling定时任务,实现日访问量与日活量的统计与记录