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

📄 ltkpreprocessor.cpp

📁 An open source handwriting recongnition package!!!
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	float initialYCoord, finalYCoord;

	float deltaX;

	float deltaY;
	
	float sweptAngle;

	int errorCode;

	vector<LTKTrace> tracesVec;

	tracesVec = inTraceGroup.getAllTraces();

	numTraces = tracesVec.size();

	vector<string> channelNames;

	channelNames.push_back(X_CHANNEL_NAME);

	channelNames.push_back(Y_CHANNEL_NAME);

	floatVector maxValues, minValues;

	vector<LTKTrace> tempTraceVector;

	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<<   "numTraces = " <<  numTraces <<endl;

	for(traceIndex=0; traceIndex < numTraces; ++traceIndex)
	{
		const LTKTrace& trace = tracesVec.at(traceIndex);
		
	    if( (errorCode = LTKInkUtils::computeChannelMaxMin(trace, channelNames, maxValues, minValues)) != SUCCESS)
        {
		    LOG(LTKLogger::LTK_LOGLEVEL_ERR)
            <<"Error: LTKPreprocessor::normalizeOrientation"<<endl;
            LTKReturnError(errorCode);
	    }

		bboxDiagonalLength = calculateEuclidDist(minValues[0], maxValues[0], minValues[1], maxValues[1]);

		minValues.clear();

		maxValues.clear();

		if(bboxDiagonalLength == 0)
		{
			tempTraceVector.push_back(trace);
			continue;
		}

		floatVector xVec, yVec;
		
		if( (errorCode = trace.getChannelValues(X_CHANNEL_NAME, xVec))!= SUCCESS)
        {
		    LOG(LTKLogger::LTK_LOGLEVEL_ERR)
            <<"Error: LTKPreprocessor::normalizeOrientation"<<endl;
            LTKReturnError(errorCode);
	    }
		

		if( (errorCode = trace.getChannelValues(Y_CHANNEL_NAME, yVec)) != SUCCESS)
        {
		    LOG(LTKLogger::LTK_LOGLEVEL_ERR)
            <<"Error: LTKPreprocessor::normalizeOrientation"<<endl;
            LTKReturnError(errorCode);
	    }

		if(xVec.size()==0)
        {
            LOG(LTKLogger::LTK_LOGLEVEL_ERR)
                  <<"Error : "<< EEMPTY_VECTOR <<":"<< getErrorMessage(EEMPTY_VECTOR)
                  <<"LTKPreprocessor::normalizeOrientation" <<endl;

            LTKReturnError(EEMPTY_VECTOR);
        }

		if(yVec.size()==0)
        {
            LOG(LTKLogger::LTK_LOGLEVEL_ERR)
                  <<"Error : "<< EEMPTY_VECTOR <<":"<< getErrorMessage(EEMPTY_VECTOR)
                  <<"LTKPreprocessor::normalizeOrientation" <<endl;

            LTKReturnError(EEMPTY_VECTOR);
        }


		initialXCoord = xVec[0];

		finalXCoord = xVec[xVec.size() - 1];

		initialYCoord = yVec[0];

		finalYCoord = yVec[yVec.size() - 1];

		deltaX = fabs((finalXCoord - initialXCoord) / bboxDiagonalLength);

		deltaY = fabs((finalYCoord - initialYCoord) / bboxDiagonalLength);

		if( (errorCode = calculateSweptAngle(trace, sweptAngle)) != SUCCESS)
		{
			LOG(LTKLogger::LTK_LOGLEVEL_ERR)
			<<"Error: LTKPreprocessor::normalizeOrientation"<<endl;
			LTKReturnError(errorCode);
		}

		if((deltaX > m_loopThreshold && deltaY < m_loopThreshold && finalXCoord < initialXCoord) ||
		   (deltaY > m_loopThreshold && deltaX < m_loopThreshold && finalYCoord < initialYCoord) ||
		   (deltaX > m_loopThreshold && deltaY > m_loopThreshold && finalYCoord < initialYCoord) )
		{

			LTKTrace revTrace;

			if( (errorCode = reverseTrace(trace, revTrace)) != SUCCESS)
			{
				LOG(LTKLogger::LTK_LOGLEVEL_ERR)
				<<"Error: LTKPreprocessor::normalizeOrientation"<<endl;
				LTKReturnError(errorCode);
			}
	
			tempTraceVector.push_back(revTrace);
		}
		else if(sweptAngle < 0.0)
		{
			LTKTrace revTrace;

			if( (errorCode = reverseTrace(trace, revTrace)) != SUCCESS)
			{
				LOG(LTKLogger::LTK_LOGLEVEL_ERR)
				<<"Error: LTKPreprocessor::normalizeOrientation"<<endl;
				LTKReturnError(errorCode);
			}
	
			tempTraceVector.push_back(revTrace);
		}
		else
		{
			tempTraceVector.push_back(trace);
		}
	}
	outTraceGroup.setAllTraces(tempTraceVector, 
                                 inTraceGroup.getXScaleFactor(),
                                 inTraceGroup.getYScaleFactor());

	return SUCCESS;
}


/**********************************************************************************
* AUTHOR		: Balaji R.
* DATE			: 23-DEC-2004
* NAME			: resampleTrace
* DESCRIPTION	: spatially resamples the given trace to a given number of points
* ARGUMENTS		: inTrace - trace to be resampled
*				  resamplePoints - number of points required after resampling
*				  outTrace - resampled trace
* RETURNS		: SUCCESS on successful resample operation
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/

int LTKPreprocessor::resampleTrace(const LTKTrace& inTrace, int resamplePoints, LTKTrace& outTrace)
{
	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<<   "Entered LTKPreprocessor::resampleTrace" <<endl;

    int pointIndex;							//	a variable to loop over the points of a trace

	int currentPointIndex =0;				//	index of the current point

	float xSum = 0.0f;								//	sum of the x coordinate values

	float ySum = 0.0f;								//	sum of the y coordinate values

	int numTracePoints;						//	number of points in a trace

	int ptIndex = 0;						//	index of point in a trace

	float x;								//	an x coord value

	float y;								//	an y coord value

	float xDiff;							//	differenc in x direction

	float yDiff;							//	diference in y direction

	float xTemp;							//	temp x storage

	float yTemp;							//	temp y storage

	float unitLength = 0;					//	estimated length of each segment after resampling

	float pointDistance;

	float balanceDistance = 0;				//	distance between the last resampled point and
											//	the next raw sample point

	float measuredDistance;

	float m1,m2;

	int errorCode;

	floatVector xVec, yVec;

	floatVector resampledXVec;

	floatVector resampledYVec;

	floatVector distanceVec;

	numTracePoints = inTrace.getNumberOfPoints();

	if(numTracePoints==0)
    {
        LOG(LTKLogger::LTK_LOGLEVEL_ERR)
              <<"Error : "<< EEMPTY_TRACE <<":"<< getErrorMessage(EEMPTY_TRACE)
              <<"LTKPreprocessor::resampleTrace" <<endl;

        LTKReturnError(EEMPTY_TRACE);
    }

	if( (errorCode = inTrace.getChannelValues(X_CHANNEL_NAME, xVec))!= SUCCESS)
    {
	    LOG(LTKLogger::LTK_LOGLEVEL_ERR)
        <<"Error: LTKPreprocessor::resampleTrace"<<endl;
        LTKReturnError(errorCode);
    }
	

	if( (errorCode = inTrace.getChannelValues(Y_CHANNEL_NAME, yVec)) != SUCCESS)
    {
	    LOG(LTKLogger::LTK_LOGLEVEL_ERR)
        <<"Error: LTKPreprocessor::resampleTrace"<<endl;
        LTKReturnError(errorCode);
    }

	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<<   "resamplePoints = " <<  resamplePoints <<endl;

	if(resamplePoints < 1)
	{
		resamplePoints = 1;
	}

	if(resamplePoints == 1)
	{
        xSum = accumulate(xVec.begin(), xVec.end(), 0.0f);
		ySum = accumulate(yVec.begin(), yVec.end(), 0.0f);

		x = xSum/ numTracePoints; //As only 1 point is required, this ratio is computed.

		y = ySum/ numTracePoints;

		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<<   "x mean = " <<  x <<endl;

		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<<   "y mean = " <<  y <<endl;

		resampledXVec.push_back(x);

		resampledYVec.push_back(y);

		float2DVector allChannelValuesVec;
		allChannelValuesVec.push_back(resampledXVec);
		allChannelValuesVec.push_back(resampledYVec);

		if( (errorCode = outTrace.setAllChannelValues(allChannelValuesVec)) != SUCCESS)
		{
			LOG(LTKLogger::LTK_LOGLEVEL_ERR)
			<<"Error: LTKPreprocessor::resampleTrace"<<endl;
			LTKReturnError(errorCode);
		}

		/*if( (errorCode = outTrace.setChannelValues(X_CHANNEL_NAME, resampledXVec)) != SUCCESS)
		{
			LOG(LTKLogger::LTK_LOGLEVEL_ERR)
			<<"Error: LTKPreprocessor::resampleTrace"<<endl;
			LTKReturnError(errorCode);
		}
		if( (errorCode = outTrace.setChannelValues(Y_CHANNEL_NAME, resampledYVec)) != SUCCESS)
		{
			LOG(LTKLogger::LTK_LOGLEVEL_ERR)
			<<"Error: LTKPreprocessor::resampleTrace"<<endl;
			LTKReturnError(errorCode);
		}*/

	}
	else if(numTracePoints <= 1)
	{
		x = xVec.at(0);

		y = yVec.at(0);

		for(pointIndex = 0; pointIndex < resamplePoints; ++pointIndex)
		{
			resampledXVec.push_back(x);

			resampledYVec.push_back(y);

			LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<<   "resampled point " << x << y <<endl;
		}

		float2DVector allChannelValuesVec;
		allChannelValuesVec.push_back(resampledXVec);
		allChannelValuesVec.push_back(resampledYVec);

		if( (errorCode = outTrace.setAllChannelValues(allChannelValuesVec)) != SUCCESS)
		{
			LOG(LTKLogger::LTK_LOGLEVEL_ERR)
			<<"Error: LTKPreprocessor::resampleTrace"<<endl;
			LTKReturnError(errorCode);
		}

		/*if( (errorCode = outTrace.setChannelValues(X_CHANNEL_NAME, resampledXVec)) != SUCCESS)
		{
			LOG(LTKLogger::LTK_LOGLEVEL_ERR)
			<<"Error: LTKPreprocessor::resampleTrace"<<endl;
			LTKReturnError(errorCode);
		}
		if( (errorCode = outTrace.setChannelValues(Y_CHANNEL_NAME, resampledYVec)) != SUCCESS)
		{
			LOG(LTKLogger::LTK_LOGLEVEL_ERR)
			<<"Error: LTKPreprocessor::resampleTrace"<<endl;
			LTKReturnError(errorCode);
		}*/
	}
	else
	{
		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<<   "Point distances" <<endl;

		for( pointIndex = 0; pointIndex < (numTracePoints-1); ++pointIndex)
		{
			xDiff = xVec.at(pointIndex) - xVec.at(pointIndex+1);

			yDiff = yVec.at(pointIndex) - yVec.at(pointIndex+1);

			pointDistance = (float) (sqrt(xDiff*xDiff + yDiff*yDiff)); //distance between 2 points.

			unitLength += pointDistance; // finding the length of trace.

			distanceVec.push_back(pointDistance); //storing distances between every 2 consecutive points.

			LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<<   "point distance: " <<  pointDistance <<endl;
		}

		unitLength /= (resamplePoints -1);

		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<<   "unitLength = " <<  unitLength <<endl;

		x = xVec.at(0); // adding x of first point;

		y = yVec.at(0); // adding y of first point;

		resampledXVec.push_back(x);

		resampledYVec.push_back(y);

		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<<    "resampled point " <<  x <<  y <<endl;

		for(pointIndex = 1; pointIndex < (resamplePoints-1); ++pointIndex)
		{
			measuredDistance = balanceDistance;

			while(measuredDistance < unitLength)
			{
				measuredDistance += distanceVec.at(ptIndex++);

				if(ptIndex == 1)
				{
					currentPointIndex = 1;
				}
				else
				{
					currentPointIndex++;
				}
			}

			if(ptIndex < 1) ptIndex = 1;

			m2 = measuredDistance - unitLength;

			m1 = distanceVec.at(ptIndex -1) - m2;

			if( fabs(m1+m2) > EPS)
			{
				xTemp =  (m1* xVec.at(currentPointIndex) + m2* xVec.at(currentPointIndex -1))/(m1+m2);

				yTemp =  (m1* yVec.at(currentPointIndex) + m2* yVec.at(currentPointIndex -1))/(m1+m2);
			}
			else
			{
				xTemp = xVec.at(currentPointIndex);

				yTemp = yVec.at(currentPointIndex);
			}

			resampledXVec.push_back(xTemp);

			resampledYVec.push_back(yTemp);

			LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<<   "resampled point " << xTemp << yTemp  <<endl;

			balanceDistance = m2;
		}

		x = xVec.at(xVec.size() - 1); // adding x of last point;

		y = yVec.at(yVec.size() - 1); // adding y of last point;

		resampledXVec.push_back(x);

		resampledYVec.push_back(y);

		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG)<<    "resampled point " <<  x <<  y  <<endl;

		float2DVector allChannelValuesVec;
		allChannelValuesVec.push_back(resampledXVec);
		allChannelValuesVec.push_back(resampledYVec);

⌨️ 快捷键说明

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