⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gacalculate.cpp

📁 利用遗传算法对图像进行分类处理
💻 CPP
字号:
// GaCalculate.cpp: implementation of the CGaCalculate class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "classify.h"
#include "GaCalculate.h"
#include <time.h>
#include <math.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
int CGaCalculate::random(int a, int b)		//随机产生[a,b]内的一个整数
{
	double rnd=rand()/(RAND_MAX + 1.0);
	int c=a+(int)((b-a+1)*rnd);
	return c;
}


int* CGaCalculate::random2(int a)			//随机产生一个0~a的整数列
{
	int* array;
	array=new int[a];
	int i,j;
	for (i=0;i<a;i++)
		array[i]=i;
	for (i=0;i<a;i++)
	{
		int p=random(i,a-1);
		int temp=array[p];
		for (j=p;j>0;j--)
			array[j]=array[j-1];
		array[0]=temp;
	}
	return array;
}

void CGaCalculate::FitCalculate()
{
	int i,j,k,n;
	double s[kMax],s_max;
	int s_count;
	int count[kMax];
	double sij[kMax][kMax];
	memset(count,0,sizeof(count));
	memset(s,0,sizeof(s));
	for (k=0;k<20;k++)
	{
		if (FitChange[k]==1) 
		{
			for (i=0;i<m_width;i++)
				for (j=0;j<m_height;j++)
				{			
					int x_class=GetClass(i,j,k);
					for (n=0;n<3;n++)
					{
						int x=band[n][(m_height - j -1) * cxDIBWidthBytes + i];
						s[x_class]+=(x-chromosome[k][x_class][n])*(x-chromosome[k][x_class][n]);
					}
					count[x_class]++;
				}
			for (i=0;i<kMax;i++)
				if (count[i]!=0) 
				{
					s[i]=s[i]/count[i];
					s[i]=sqrt(s[i]);
				}
			for (i=0;i<kMax;i++)
				for (j=0;j<kMax;j++)	
				{
					if (i==j || chromosome[k][i][0]==-999) 
						sij[i][j]=0;
					else 
					{
						double dist_out=0;
						for (n=0;n<3;n++)
							dist_out+=(chromosome[k][i][n]-chromosome[k][j][n])
										*(chromosome[k][i][n]-chromosome[k][j][n]);
						dist_out=sqrt(dist_out);
						sij[i][j]=(s[i]+s[j])/dist_out;
					}

				}
			s_max=0;
			s_count=0;
			for (i=0;i<kMax;i++)
			{
				int max=0;
				for (j=1;j<kMax;j++)
					if (sij[i][j]>sij[i][max])
						max=j;
				s_max+=sij[i][max];
				if (sij[i][max]!=0) s_count++;
			}
			if (s_max==0) fitness[k]=0;
			else 
				fitness[k]=s_count/s_max;
			FitChange[k]=0;
		}
	}

	
}

CGaCalculate::CGaCalculate()				//获取图像指针,并初始化染色体
{
	m_hDIB4  = NULL;

	CFile file1("d:\\gaclassify\\band1.bmp", CFile::modeRead);
	CFile file2("d:\\gaclassify\\band2.bmp", CFile::modeRead);
	CFile file3("d:\\gaclassify\\band3.bmp", CFile::modeRead);

	m_hDIB1 = ReadDIBFile(file1);
	m_hDIB2 = ReadDIBFile(file2);
	m_hDIB3 = ReadDIBFile(file3);

//	CreateDIBPalette(m_hDIB, m_palDIB);

	file1.Close();
	file2.Close();	
	file3.Close();
	
	LPSTR lpDIB1 = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB1);
	LPSTR lpDIB2 = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB2);
	LPSTR lpDIB3 = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB3);

	// 获取DIB宽度
	m_width = (int) ::DIBWidth(lpDIB1);
	cxDIBWidthBytes = WIDTHBYTES(m_width*8);
	// 获取DIB高度
	m_height = (int) ::DIBHeight(lpDIB1);

	band[0] = (BYTE*)lpDIB1 + sizeof(BITMAPINFOHEADER) + 1024;
	band[1] = (BYTE*)lpDIB2 + sizeof(BITMAPINFOHEADER) + 1024;
	band[2] = (BYTE*)lpDIB3 + sizeof(BITMAPINFOHEADER) + 1024;

/*	for(i = 0; i < m_width; i++)
	{
		for(j = 0; j < m_height; j++)
		{
			m[0][i][j]=pImgData1[(m_height - j -1) * cxDIBWidthBytes + i];
			m[1][i][j]=pImgData2[(m_height - j -1) * cxDIBWidthBytes + i];
			m[2][i][j]=pImgData3[(m_height - j -1) * cxDIBWidthBytes + i];
		}
	}
*/	
	int i,j,k;
	srand( (unsigned)time( NULL ) );
	for (k=0;k<20;k++)
	{
		for (i=0;i<kMax;i++)
			for (j=0;j<3;j++)
				chromosome[k][i][j]=-999;
		int ki=random(kMin,kMax);
		int* m_array;
		m_array=new int[kMax];
		m_array=random2(kMax);				
		for (i=0;i<ki;i++)
		{
			int p=m_array[i];
			for (j=0;j<3;j++)
			{		
				chromosome[k][p][j]=random(0,255);
			}
		}
		FitChange[k]=1;
	}

}


CGaCalculate::~CGaCalculate()
{
}

void CGaCalculate::ReFitCalculate()
{
	int i;
	sfitness[0]=fitness[0];
	for (i=1;i<20;i++)
	{
		sfitness[i]+=fitness[i];
	}
	for (i=0;i<20;i++)
	{
		sfitness[i]=sfitness[i]/sfitness[19];
	}
}

void CGaCalculate::Cross()
{
	int i,j,k;
	int p1,p2;
	for (k=0;k<5;k++)
	{
		p1=1;
		p2=1;
		double rnd=rand()/(RAND_MAX + 1.0);
		while (rnd<sfitness[p1])
			p1++;
		rnd=rand()/(RAND_MAX + 1.0);
		while (rnd<sfitness[p2])
			p2++;
		if (p1!=p2)
		{	int temp[8][3];
			for (i=0;i<8;i++)
				for (j=0;j<3;j++)
				{
					temp[i][j]=chromosome[p1][i][j];
					chromosome[p1][i][j]=chromosome[p2][i][j];
					chromosome[p2][i][j]=temp[i][j];
				}
		}
		FitChange[p1]=1;
		FitChange[p2]=1;
	}

}

void CGaCalculate::Mutate()
{
	int p1,p2,i,k;
	for (k=0;k<10;k++)
	{
		p1=0;
		double rnd=rand()/(RAND_MAX + 1.0);
		while (rnd<sfitness[p1])
			p1++;
		p2=random(0,7);
		if (chromosome[p1][p2][0]!=999)
			for (i=0;i<3;i++)
				chromosome[p1][p2][i]=random(0,255);
		FitChange[p1]=1;
	}
}

int CGaCalculate::GetClass(int i, int j, int k)
{
	int xclass,xclass2,xcount;
	int a[3];
	int m,n;
	xclass=0;
	for (n=0;n<3;n++)
	{
		a[n]=band[n][(m_height - j -1) * cxDIBWidthBytes + i];
		xclass+=(a[n]-chromosome[k][0][n])*(a[n]-chromosome[k][0][n]);
	}
	xcount=0;
	for (m=0;m<kMax;m++)
	{
		if (chromosome[k][m][0]!=-999)
		{
			xclass2=0;
			for (n=0;n<3;n++)
				xclass2+=(a[n]-chromosome[k][m][n])*(a[n]-chromosome[k][m][n]);
			if (xclass>xclass2)
			{
				xclass=xclass2;
				xcount=m;
			}
		}
	}
	return xcount;
}

void CGaCalculate::SaveResult()
{
	int i,j;
	CFile file;
	BOOL bSu = file.Open("d:\\gaclassify\\result.bmp", CFile::modeRead);

	m_hDIB4 = ReadDIBFile(file);

	file.Close();
	
	LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB4);
	lpDIB = FindDIBBits(lpDIB);

	//unsigned char* result = (BYTE*)lpDIB + sizeof(BITMAPINFOHEADER) + 1024;
	RGBQUAD* pPal = (RGBQUAD*)(lpDIB - 1024);
	pPal[0].rgbBlue = 255;
	pPal[0].rgbRed = 0;
	pPal[0].rgbGreen = 0;

	pPal[20].rgbBlue = 0;
	pPal[20].rgbRed = 255;
	pPal[20].rgbGreen = 0;
	
	int best=random(0,19);

	for (i=0;i<20;i++)
	{
		if (fitness[best]<fitness[i]) 
			best=i;
	}
	for (i=0;i<m_width;i++)
		for (j=0;j<m_height;j++)
		{
			lpDIB[(m_height - j -1) * cxDIBWidthBytes + i]=GetClass(i,j,best)*20;
		}

    
	::GlobalUnlock((HGLOBAL) m_hDIB4);
	file.Open("d:\\gaclassify\\result.bmp", CFile::modeWrite);
	SaveDIB(m_hDIB4,file);

	file.Close();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -