OTSUアルゴリズムと基本粒子群最適化アルゴリズムに基づく二重しきい値画像分割
OTSU適応しきい値求法と粒子群アルゴリズムとの連携により,OTSUアルゴリズムを粒子群アルゴリズムの適応値関数として,各粒子の適応度を最適しきい値と比較し,3000回の反復を経て最適化後のしきい値を取得した.
原図:
結合アルゴリズムによって最適化された二重しきい値は90140である.
背景ピクセルを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;iimageData+src->widthStep*i;
for(int j=0;jmaxVariance)
{
maxVariance=variance;
threshold=i;
}
*/
return variance;
}
/*****************************************************************
:Init
:void
:
************************************************************************/
void Init()
{
for(int index=0;index255)
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;indexpbest[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;index5)
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;hheight;h++)
for(int w=0;wwidth;w++)
{
if(*(src->imageData+h*src->widthStep+w)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
" , gbestThreshold[0] , gbestThreshold[1]);
cvShowImage("cb" , cb);
cvSaveImage("cb1.jpg" ,cb);
cvWaitKey(0);
return 0;
}