Esempio n. 1
0
static void initDistGlobals(		// initialize distortion globals
    KMfilterCenters& ctrs)			// the centers
{
    initBasicGlobals(ctrs.getDim(), ctrs.getNPts(), ctrs.getDataPts());
    kcKCtrs	= ctrs.getK();
    kcCenters	= ctrs.getCtrPts();		// get ptrs to KMcenter arrays
    kcWeights	= ctrs.getWeights(false);
    kcSums	= ctrs.getSums(false);
    kcSumSqs	= ctrs.getSumSqs(false);
    kcDists	= ctrs.getDists(false);
    kcBoxMidpt  = kmAllocPt(kcDim);

    for (int j = 0; j < kcKCtrs; j++) {		// initialize sums
	kcWeights[j] = 0;
	kcSumSqs[j] = 0;
	for (int d = 0; d < kcDim; d++) {
    	    kcSums[j][d] = 0;
	}
    }
}
Esempio n. 2
0
//------------------------------------------------------------------------
//  Print summary of execution
//------------------------------------------------------------------------
void printSummary(const KMlocal&		theAlg,		// the algorithm
				  const KMdata&		dataPts,	// the points
				  KMfilterCenters&		ctrs)		// the centers
{
	double dbval, xbval;
	dbval = ctrs.getDBIndex();
	xbval = ctrs.getXBIndex();

	FILE* logfp = fopen("kmean.log", "wt");
	fprintf(logfp, "Number of stages: %d\n", theAlg.getTotalStages());
	fprintf(logfp, "Average distortion: %g\n", ctrs.getDist(false)/double(ctrs.getNPts()));
	fprintf(logfp, "DB-index: %g\n", dbval);
	fprintf(logfp, "XB-index: %g\n", xbval);	

	cout << "Number of stages: " << theAlg.getTotalStages() << "\n";
	cout << "Average distortion: " <<
		ctrs.getDist(false)/double(ctrs.getNPts()) << "\n";
	cout << "DB-index = " << dbval << "\n";
	cout << "XB-index = " << xbval << "\n";

	KMctrIdxArray closeCtr = new KMctrIdx[dataPts.getNPts()];
	double* sqDist = new double[dataPts.getNPts()];
	ctrs.getAssignments(closeCtr, sqDist);

	int*  hist = new int[ctrs.getK()];
	memset(hist, 0, sizeof(int) * ctrs.getK() );
	for(int i=0; i < dataPts.getNPts(); i++ )
	{
		int k = closeCtr[i];
		hist[k] ++;
	}

	KMpoint kpt;
	for(int i=0; i< ctrs.getK(); i++ )
	{
		fprintf(logfp, "%3d-th, #%5d ", i, hist[i]);

		// print final center points
		kpt = ctrs[i];
		fprintf(logfp, " [");
		for (int j = 0; j < ctrs.getDim(); j++) 
		{
			fprintf(logfp, "%7g ", kpt[j]);
		}
		fprintf(logfp, " ]\n");
	}
	fclose(logfp);

	// write to fea-file
	CFeaFileWriter theFeaWriter;
	char saveName[256];
	sprintf(saveName, "kmain_%s", infname.c_str() );
	theFeaWriter.openFile(saveName);

	FEA_FILE_HEADER feaHeader;
	feaHeader.nVersion = FEA_FILE_VERSION;
	feaHeader.nRecords = ctrs.getK();
	feaHeader.nFeaDim = ctrs.getDim();
	feaHeader.nElemType = ELEM_TYPE_FLOAT;
	feaHeader.nElemSize = sizeof(float);
	feaHeader.bIndexTab = 0;
	feaHeader.bVarLen = 0;
	sprintf(feaHeader.szFeaName, "kmean codebook");
	theFeaWriter.setFileHeader(feaHeader);

	float* pFea = new float[ctrs.getDim()];
	for(int i=0; i< ctrs.getK(); i++ )
	{
		// save final center points
		kpt = ctrs[i];
		for (int j = 0; j < ctrs.getDim(); j++) 
			pFea[j] = kpt[j];

		theFeaWriter.writeRecordAt(pFea, i);
	}
	theFeaWriter.flush2Disk();
	theFeaWriter.closeFile();
	theFeaWriter.releaseMemory();
	delete [] pFea;

	delete [] closeCtr;
	delete [] sqDist;
	delete [] hist;
}