基于OTSU算法和基本粒子群优化算法的双阈值图像分割
OTSU自适应阈值求法与粒子群算法的合作,将OTSU算法作为粒子群算法的适应值函数,来计算每个粒子的适应度与最优阈值相比较,经过3000次迭代最后取得优化后的阈值
原图:
经过联合算法优化的双阈值为90 ,140
将背景像素置0:
效果图:
利用所取得的阈值就可以将图像背景和目标区分开来,利用所得阈值二值化后
效果图:
通过效果图可知将人这个目标从背景中分割出来了
源代码:
- #include "stdafx.h"
- #include "cv.h"
- #include "highgui.h"
- #include "cxcore.h"
- #include "time.h"
- using namespace std;
- #define rnd( low,uper) ((int)(((double)rand()/(double)RAND_MAX)*((double)(uper)-(double)(low))+(double)(low)+0.5))
- /*************************************************************8888
- 粒子群算法变量的说明
- ******************************************************************************/
- const int number = 20;
- int antThreshold[number][2];//以阈值作为粒子
- int vect[number][2];//更新的速度
- float pbest[number] = {0.0};;//每个粒子历史最优解
- float gbest = 0.0;//全局历史最优解
- int pbestThreshold[number][2];//每个粒子的最优历史阈值
- int gbestThreshold[2];//全局粒子的最优阈值
- float w = 0.9;//惯性因子
- float c1 = 2.0;//加速因子1
- float c2 = 2.0;//加速因子2
- //histogram
- float histogram[256]={0};
- /*********************************************************************8888
- 函数名:GetAvgValue
- 参数类型:IplImage* src
- 实现功能:获得灰度图像的总平均灰度值
- *****************************************************************************/
- float GetAvgValue(IplImage* src)
- {
- int height=src->height;
- int width=src->width;
- for(int i=0;i<height;i++)
- {
- unsigned char* p=(unsigned char*)src->imageData+src->widthStep*i;
- for(int j=0;j<width;j++)
- {
- histogram[*p++]++;
- }
- }
- //normalize histogram
- int size=height*width;
- for(int i=0;i<256;i++) {
- histogram[i]=histogram[i]/size;
- }
- //average pixel value
- float avgValue=0;
- for(int i=0;i<256;i++) {
- avgValue+=i*histogram[i];
- }
- return avgValue;
- }
- /*****************************************************************************
- 函数名:ThresholdOTSU
- 参数类型:int threshold1 , int threshold2 , float avgValue
- 功能:求得最大类间方差
- **********************************************************************************/
- float ThresholdOTSU(int threshold1 , int threshold2 , float avgValue)
- {
- int threshold;
- float maxVariance=0;
- float w=0,u=0;
- for(int i=threshold1;i< threshold2 ;i++)
- {
- w+=histogram[i];
- u+=i*histogram[i];
- }
- float t=avgValue*w-u;
- float variance=t*t/(w*(1-w));
- /* if(variance>maxVariance)
- {
- maxVariance=variance;
- threshold=i;
- }
- */
- return variance;
- }
- /*****************************************************************
- 函数名:Init
- 参数类型:void
- 功能:初始化粒子群算法的粒子与速度
- ************************************************************************/
- void Init()
- {
- for(int index=0;index<number;index++)
- {
- antThreshold[index][0] = rnd(10 , 50);
- antThreshold[index][1] = antThreshold[index][0] + 50;
- if(antThreshold[index][1]>255)
- antThreshold[index][1] = 255;
- vect[index][0] = rnd(3 ,5);
- vect[index][1] = rnd(3 ,5);
- }
- }
- /******************************************************************
- 函数名:Pso
- 参数类型:void
- 功能:粒子群算法的实现
- ***************************************************************************/
- void Pso(float value)
- {
- for(int index=0;index<number;index++)
- {
- float variance;
- variance = ThresholdOTSU(antThreshold[index][0] , antThreshold[index][1] , value);
- if(variance>pbest[index])
- {
- pbest[index] = variance;
- pbestThreshold[index][0] = antThreshold[index][0];
- pbestThreshold[index][1] = antThreshold[index][1];
- }
- if(variance>gbest)
- {
- gbest = variance;
- gbestThreshold[0] = antThreshold[index][0];
- gbestThreshold[1] = antThreshold[index][1];
- }
- }
- }
- /***************************************************************************************88
- 函数名:updateData
- 参数类型:void
- 功能:更新粒子数据与速度
- **************************************************************************************************/
- void updateData()
- {
- for(int index=0;index<number;index++)
- {
- for(int i=0;i<2;i++)
- {
- vect[index][i] = w*vect[index][i] + c1*((double)(rand())/(double)RAND_MAX)*(pbestThreshold[index][i]-antThreshold[index][i])+
- c2*c1*((double)(rand())/(double)RAND_MAX)*(gbestThreshold[i]-antThreshold[index][i]);
- if(vect[index][i]>5)
- vect[index][i] = 5;
- if(vect[index][i]<3)
- vect[index][i] = 3;
- antThreshold[index][i] = vect[index][i] + antThreshold[index][i];
- }
- if(antThreshold[index][0]>antThreshold[index][1])
- antThreshold[index][1] = antThreshold[index][0] + 50;
- if(antThreshold[index][1]>255)
- antThreshold[index][1] = 255;
- if(antThreshold[index][0]<0)
- antThreshold[index][0] = 0;
- }
- }
- /**************************************************************8
- 函数名:Threshold
- 参数类型:IplImage *src , int lower , int higher
- 功能:利用算法得到的双阈值对图像进行阈值分割
- ***********************************************************************/
- void Threshold(IplImage *src , int lower , int higher)
- {
- assert(src->nChannels==1);
- for(int h=0;h<src->height;h++)
- for(int w=0;w<src->width;w++)
- {
- if(*(src->imageData+h*src->widthStep+w)<higher&&*(src->imageData+h*src->widthStep+w)>lower)
- //*(src->imageData+h*src->widthStep+w) = 255;
- ;
- else
- *(src->imageData+h*src->widthStep+w) = 0;
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- srand((unsigned)time(NULL));
- IplImage *img =0;
- IplImage *ycrcb = 0;
- IplImage *cb = 0;
- cvNamedWindow("cb" , 1);
- img = cvLoadImage("1.jpg" , 1);
- ycrcb = cvCreateImage(cvGetSize(img) , 8 ,3);
- cb = cvCreateImage(cvGetSize(img) , 8 , 1);
- cvCvtColor(img , ycrcb , CV_BGR2YCrCb);
- cvSplit(ycrcb , 0 ,0,cb , 0);
- cvSmooth(cb , cb , CV_MEDIAN , 3 , 0,0,0);
- float avgValue = 0.0;
- avgValue = GetAvgValue(cb);
- Init();
- for(int i=0;i<3000;i++)
- {
- Pso(avgValue);
- updateData();
- }
- //cvThreshold(cb , cb , gbestThreshold[0] , gbestThreshold[1] , CV_THRESH_BINARY);
- Threshold(cb , gbestThreshold[0] , gbestThreshold[1]);
- printf("%d , %d\n" , gbestThreshold[0] , gbestThreshold[1]);
- cvShowImage("cb" , cb);
- cvSaveImage("cb1.jpg" ,cb);
- cvWaitKey(0);
- return 0;
- }
网址:http://blog.csdn.net/qinjianganying/article/details/6747980
基于OTSU算法和基本粒子群优化算法的双阈值图像分割相关推荐
- 智能优化算法:蛾群优化算法-附代码
智能优化算法:蛾群优化算法 摘要:蛾群算法 (Moth Swarm Algorithm, MSA)是 Al-Attar AliMohamed 于2016年提出的新兴元启发式智能优化算法,它模拟自然界蛾 ...
- 【优化算法】猫群优化算法(CSO)【含Matlab源码 1071期】
⛄一.获取代码方式 获取代码方式1: 完整代码已上传我的资源:[优化算法]猫群优化算法(CSO)[含Matlab源码 1071期] 点击上面蓝色字体,直接付费下载,即可. 获取代码方式2: 付费专栏M ...
- matlab——智能算法之粒子群优化算法、模拟退火算法、遗传算法
智能算法之粒子群优化算法: %% 初始化种群 f= % 函数表达式 % 求这个函数的最大值 figure(1);ezplot(f,[0,0.01,20]); N = 50; % 初始种群个数 d = ...
- 【优化算法】粒子群优化算法
粒子群优化算法 粒子群优化算法简介 粒子群优化算法原理 粒子群优化算法的数学描述 粒子群优化算法框架 PySwarms:Python中粒子群优化的研究工具包 PySwarms快速使用 示例:编写自己的 ...
- 2018-3-28 基本粒子群优化算法
学习资源: [图文]群智能理论及粒子群优化算法_百度文库 https://wenku.baidu.com/view/c855e622a5e9856a56126075.html 书本<智能算法以及 ...
- 【计算智能】——群体智能算法(蚁群优化算法ACO、粒子群优化算法PSO)
群体智能算法 与大多数基于梯度的优化算法不同,群体智能算法依靠的是概率搜索算法. 与梯度算法及传统演化算法相比优点: 没有集中控制约束,不会因为个体的故障影响整个问题的求解. 以非直接信息交流的方式确 ...
- 揽货最短路径解决方案算法 - C# 蚁群优化算法实现
需求为(自己编的,非实际项目): 某配送中心进行揽货,目标客户数为50个客户,配送中心目前的运力资源如下: 现有车辆5台 单台运力最大行驶距离200千米 单台运力最大载重公斤1吨 问:运力怎样走法才能 ...
- 遗传算法 差分进化算法 粒子群优化算法区别
一 遗传算法 遗传算法(GA)作为一种经典的进化算法,自 Holland提出之后在国际上已经形成了一个比较活跃的研究领域. 人们对 GA 进行了大量的研究,提出了各种改进算法用于提高算法的收敛速度和精 ...
- 优化算法(四)——粒子群优化算法(PSO)
粒子群算法(Particle Swarm Optimization,PSO)是一种模仿鸟群.鱼群觅食行为发展起来的一种进化算法.其概念简单易于编程实现且运行效率高.参数相对较少,应用非常广泛.粒子群算 ...
最新文章
- 4、jquery表格操作
- 传感器c语言开发,Android环境传感器开发教程
- torch view view_as
- 真正理解 MySQL 的四种隔离级别
- 2021-10-11 二叉树,二叉搜索树及其相关23个操作 C++实现笔记(复习用,含C指针复习)
- python环境_python环境搭建教程
- 浅谈域名发散与域名收敛
- JQuery中的ID选择器
- 超越MobileNet v3!华为诺亚、北大提出GhostNet​,使用线性变换生成特征图
- win10键盘全部没反应_Win10的键盘失灵解决办法
- Intel收购半导体设计公司eASIC
- 第一部分 第五章 数组 1102-1149
- WPF从零到1教程详解,适合新手上路
- Apache Ranger KMS 部署文档
- Reflex WMS入门系列二十四:拆托(HD Breakdown)
- 利用华硕路由器实现创维电视广告屏蔽
- Buffon投针实验
- 大学应用计算机二级,大学计算机二级ps考试试题内容(3)
- 正则看这三个网站就够了
- 智能分拣解决方案市场现状研究分析报告 -
热门文章
- xpath爬取当当网
- 三维重建3:旋转矩阵-病态矩阵、欧拉角-万向锁、四元数
- 用jsp的mvc模式的新闻发布系统_网易内部倡导用昵称代替哥姐总等称呼;TCL大股东误操作卖出500万股;Ant Design 4.6.2 发布| 极客头条...
- ubuntu 20.04 安装录屏软件 OBS 及卸载
- 利用Java反射机制调用含数组参数的方法
- 手机电商营销模式探讨
- 美国高考能不能带计算机,这件“神器”在美国被高中允许却被美国大学禁止
- Ubuntu Core 将支持物联网 Matter
- Flutter版讯飞语音识别demo
- springboot整合redis、mybatis、@EnableScheduling定时任务,实现日访问量与日活量的统计与记录