コード例 #1
0
ファイル: ocrfeatures.cpp プロジェクト: 0359xiaodong/tess-two
/*---------------------------------------------------------------------------*/
FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
/*
 **	Parameters:
 **		File		open text file to read feature from
 **		FeatureDesc	specifies type of feature to read from File
 **	Globals: none
 **	Operation: Create a new feature of the specified type and read in
 **		the value of its parameters from File.  The extra penalty
 **		for the feature is also computed by calling the appropriate
 **		function for the specified feature type.  The correct text
 **		representation for a feature is a list of N floats where
 **		N is the number of parameters in the feature.
 **	Return: New feature read from File.
 **	Exceptions: ILLEGAL_FEATURE_PARAM if text file doesn't match expected format
 **	History: Wed May 23 08:53:16 1990, DSJ, Created.
 */
  FEATURE Feature;
  int i;

  Feature = NewFeature (FeatureDesc);
  for (i = 0; i < Feature->Type->NumParams; i++) {
    if (fscanf (File, "%f", &(Feature->Params[i])) != 1)
      DoError (ILLEGAL_FEATURE_PARAM, "Illegal feature parameter spec");
#ifndef WIN32
    assert (!isnan(Feature->Params[i]));
#endif
  }
  return (Feature);

}                                /* ReadFeature */
コード例 #2
0
/*
Interpolates a scale-space extremum's location and scale to subpixel
accuracy to form an image feature.  Rejects features with low contrast.
Based on Section 4 of Lowe's paper.  

@param dog_pyr DoG scale space pyramid
@param octv feature's octave of scale space
@param intvl feature's within-octave interval
@param r feature's image row
@param c feature's image column
@param intvls total intervals per octave
@param contr_thr threshold on feature contrast

@return Returns the feature resulting from interpolation of the given
	parameters or NULL if the given location could not be interpolated or
	if contrast at the interpolated loation was too low.  If a feature is
	returned, its scale, orientation, and descriptor are yet to be determined.
*/
 feature* SiftGPU::InterpExtremum( IplImage*** dog_pyr, int octv, int intvl,
										int r, int c, int intvls, float contr_thr )
{
	feature* feat;
	struct detection_data* ddata;
	float xi, xr, xc, contr;
	int i = 0;

	if( c == 668 )
		i = 0;

	while( i < SIFT_MAX_INTERP_STEPS )
	{
		InterpStep( dog_pyr, octv, intvl, r, c, &xi, &xr, &xc );
		if( abs( xi ) < 0.5  &&  abs( xr ) < 0.5  &&  abs( xc ) < 0.5 )
			break;

		c += cvRound( xc );
		r += cvRound( xr );
		intvl += cvRound( xi );

		if( intvl < 1  ||
			intvl > intvls  ||
			c < SIFT_IMG_BORDER  ||
			r < SIFT_IMG_BORDER  ||
			c >= dog_pyr[octv][0]->width - SIFT_IMG_BORDER  ||
			r >= dog_pyr[octv][0]->height - SIFT_IMG_BORDER )
		{
			return NULL;
		}

		i++;
	}

	/* ensure convergence of interpolation */
	if( i >= SIFT_MAX_INTERP_STEPS )
		return NULL;

	contr = InterpContr( dog_pyr, octv, intvl, r, c, xi, xr, xc );
	if( abs( contr ) < contr_thr / intvls )
		return NULL;

	feat = NewFeature();
	ddata = FeatDetectionData( feat );
	feat->img_pt.x = feat->x = ( c + xc ) * pow( 2.0, octv );
	feat->img_pt.y = feat->y = ( r + xr ) * pow( 2.0, octv );
	ddata->r = r;
	ddata->c = c;
	ddata->octv = octv;
	ddata->intvl = intvl;
	ddata->subintvl = xi;

	return feat;
}
コード例 #3
0
/*---------------------------------------------------------------------------*/
void ConvertSegmentToPicoFeat(FPOINT *Start,
                              FPOINT *End,
                              FEATURE_SET FeatureSet) {
/*
 **	Parameters:
 **		Start		starting point of pico-feature
 **		End		ending point of pico-feature
 **		FeatureSet	set to add pico-feature to
 **	Globals:
 **		classify_pico_feature_length	length of a single pico-feature
 **	Operation: This routine converts an entire segment of an outline
 **		into a set of pico features which are added to
 **		FeatureSet.  The length of the segment is rounded to the
 **		nearest whole number of pico-features.  The pico-features
 **		are spaced evenly over the entire segment.
 **	Return: none (results are placed in FeatureSet)
 **	Exceptions: none
 **	History: Tue Apr 30 15:44:34 1991, DSJ, Created.
 */
  FEATURE Feature;
  FLOAT32 Angle;
  FLOAT32 Length;
  int NumFeatures;
  FPOINT Center;
  FPOINT Delta;
  int i;

  Angle = NormalizedAngleFrom (Start, End, 1.0);
  Length = DistanceBetween (*Start, *End);
  NumFeatures = (int) floor (Length / classify_pico_feature_length + 0.5);
  if (NumFeatures < 1)
    NumFeatures = 1;

  /* compute vector for one pico feature */
  Delta.x = XDelta (*Start, *End) / NumFeatures;
  Delta.y = YDelta (*Start, *End) / NumFeatures;

  /* compute position of first pico feature */
  Center.x = Start->x + Delta.x / 2.0;
  Center.y = Start->y + Delta.y / 2.0;

  /* compute each pico feature in segment and add to feature set */
  for (i = 0; i < NumFeatures; i++) {
    Feature = NewFeature (&PicoFeatDesc);
    Feature->Params[PicoFeatDir] = Angle;
    Feature->Params[PicoFeatX] = Center.x;
    Feature->Params[PicoFeatY] = Center.y;
    AddFeature(FeatureSet, Feature);

    Center.x += Delta.x;
    Center.y += Delta.y;
  }
}                                /* ConvertSegmentToPicoFeat */
コード例 #4
0
/**
 * Create a new feature of the specified type and read in
 * the value of its parameters from File.  The extra penalty
 * for the feature is also computed by calling the appropriate
 * function for the specified feature type.  The correct text
 * representation for a feature is a list of N floats where
 * N is the number of parameters in the feature.
 * @param File open text file to read feature from
 * @param FeatureDesc specifies type of feature to read from File
 * @return New #FEATURE read from File.
 */
FEATURE ReadFeature(FILE* File, const FEATURE_DESC_STRUCT* FeatureDesc) {
  FEATURE Feature;
  int i;

  Feature = NewFeature (FeatureDesc);
  for (i = 0; i < Feature->Type->NumParams; i++) {
    ASSERT_HOST(tfscanf(File, "%f", &(Feature->Params[i])) == 1);
#ifndef _WIN32
    assert (!std::isnan(Feature->Params[i]));
#endif
  }
  return Feature;
}
コード例 #5
0
ファイル: mf.cpp プロジェクト: 0ximDigital/appsScanner
/*---------------------------------------------------------------------------*/
FEATURE_SET ExtractMicros(TBLOB* Blob, const DENORM& cn_denorm) {
/*
 **	Parameters:
 **		Blob		blob to extract micro-features from
 **		denorm  control parameter to feature extractor.
 **	Globals: none
 **	Operation: Call the old micro-feature extractor and then copy
 **		the features into the new format.  Then deallocate the
 **		old micro-features.
 **	Return: Micro-features for Blob.
 **	Exceptions: none
 **	History: Wed May 23 18:06:38 1990, DSJ, Created.
 */
  int NumFeatures;
  MICROFEATURES Features, OldFeatures;
  FEATURE_SET FeatureSet;
  FEATURE Feature;
  MICROFEATURE OldFeature;

  OldFeatures = BlobMicroFeatures(Blob, cn_denorm);
  if (OldFeatures == NULL)
    return NULL;
  NumFeatures = count (OldFeatures);
  FeatureSet = NewFeatureSet (NumFeatures);

  Features = OldFeatures;
  iterate(Features) {
    OldFeature = (MICROFEATURE) first_node (Features);
    Feature = NewFeature (&MicroFeatureDesc);
    Feature->Params[MFDirection] = OldFeature[ORIENTATION];
    Feature->Params[MFXPosition] = OldFeature[XPOSITION];
    Feature->Params[MFYPosition] = OldFeature[YPOSITION];
    Feature->Params[MFLength] = OldFeature[MFLENGTH];

    // Bulge features are deprecated and should not be used.  Set to 0.
    Feature->Params[MFBulge1] = 0.0f;
    Feature->Params[MFBulge2] = 0.0f;

#ifndef _WIN32
    // Assert that feature parameters are well defined.
    int i;
    for (i = 0; i < Feature->Type->NumParams; i++) {
      ASSERT_HOST(!isnan(Feature->Params[i]));
    }
#endif

    AddFeature(FeatureSet, Feature);
  }
  FreeMicroFeatures(OldFeatures);
  return FeatureSet;
}                                /* ExtractMicros */
コード例 #6
0
ファイル: mergenf.cpp プロジェクト: 0359xiaodong/tess-two
/**
 * Compare protos p1 and p2 and return an estimate of the
 * worst evidence rating that will result for any part of p1
 * that is compared to p2.  In other words, if p1 were broken
 * into pico-features and each pico-feature was matched to p2,
 * what is the worst evidence rating that will be achieved for
 * any pico-feature.
 *
 * @param p1, p2    protos to be compared
 *
 * Globals: none
 *
 * @return Worst possible result when matching p1 to p2.
 * @note Exceptions: none
 * @note History: Mon Nov 26 08:27:53 1990, DSJ, Created.
 */
FLOAT32 CompareProtos(PROTO p1, PROTO p2) {
  FEATURE Feature;
  FLOAT32 WorstEvidence = WORST_EVIDENCE;
  FLOAT32 Evidence;
  FLOAT32 Angle, Length;

  /* if p1 and p2 are not close in length, don't let them match */
  Length = fabs (p1->Length - p2->Length);
  if (Length > MAX_LENGTH_MISMATCH)
    return (0.0);

  /* create a dummy pico-feature to be used for comparisons */
  Feature = NewFeature (&PicoFeatDesc);
  Feature->Params[PicoFeatDir] = p1->Angle;

  /* convert angle to radians */
  Angle = p1->Angle * 2.0 * PI;

  /* find distance from center of p1 to 1/2 picofeat from end */
  Length = p1->Length / 2.0 - GetPicoFeatureLength () / 2.0;
  if (Length < 0) Length = 0;

  /* set the dummy pico-feature at one end of p1 and match it to p2 */
  Feature->Params[PicoFeatX] = p1->X + cos (Angle) * Length;
  Feature->Params[PicoFeatY] = p1->Y + sin (Angle) * Length;
  if (DummyFastMatch (Feature, p2)) {
    Evidence = SubfeatureEvidence (Feature, p2);
    if (Evidence < WorstEvidence)
      WorstEvidence = Evidence;
  } else {
    FreeFeature(Feature);
    return 0.0;
  }

  /* set the dummy pico-feature at the other end of p1 and match it to p2 */
  Feature->Params[PicoFeatX] = p1->X - cos (Angle) * Length;
  Feature->Params[PicoFeatY] = p1->Y - sin (Angle) * Length;
  if (DummyFastMatch (Feature, p2)) {
    Evidence = SubfeatureEvidence (Feature, p2);
    if (Evidence < WorstEvidence)
      WorstEvidence = Evidence;
  } else {
    FreeFeature(Feature);
    return 0.0;
  }

  FreeFeature (Feature);
  return (WorstEvidence);

} /* CompareProtos */
コード例 #7
0
// Returns the cn_feature as a FEATURE_STRUCT* needed by cntraining.
FEATURE_STRUCT* TrainingSample::GetCNFeature() const {
  FEATURE feature = NewFeature(&CharNormDesc);
  for (int i = 0; i < kNumCNParams; ++i)
    feature->Params[i] = cn_feature_[i];
  return feature;
}
コード例 #8
0
/*---------------------------------------------------------------------------*/
FEATURE_SET ExtractCharNormFeatures(TBLOB *Blob, LINE_STATS *LineStats) { 
/*
 **	Parameters:
 **		Blob		blob to extract char norm feature from
 **		LineStats	statistics on text row blob is in
 **	Globals: none
 **	Operation: Compute a feature whose parameters describe how a
 **		character will be affected by the character normalization
 **		algorithm.  The feature parameters are:
 **			y position of center of mass in baseline coordinates
 **			total length of outlines in baseline coordinates
 **				divided by a scale factor
 **			radii of gyration about the center of mass in
 **				baseline coordinates
 **	Return: Character normalization feature for Blob.
 **	Exceptions: none
 **	History: Wed May 23 18:06:38 1990, DSJ, Created.
 */
  FEATURE_SET FeatureSet;
  FEATURE Feature;
  FLOAT32 Scale;
  FLOAT32 Baseline;
  LIST Outlines;
  INT_FEATURE_ARRAY blfeatures;
  INT_FEATURE_ARRAY cnfeatures;
  INT_FX_RESULT_STRUCT FXInfo;

  /* allocate the feature and feature set - note that there is always one
     and only one char normalization feature for any blob */
  FeatureSet = NewFeatureSet (1);
  Feature = NewFeature (&CharNormDesc);
  AddFeature(FeatureSet, Feature); 

  /* compute the normalization statistics for this blob */
  Outlines = ConvertBlob (Blob);
  /*---------Debug--------------------------------------------------*
  OFile = fopen ("f:/ims/debug/nfOutline.logCPP", "r");
  if (OFile == NULL)
  {
    OFile = Efopen ("f:/ims/debug/nfOutline.logCPP", "w");
    WriteOutlines(OFile, Outlines);
  }
  else
  {
    fclose (OFile);
    OFile = Efopen ("f:/ims/debug/nfOutline.logCPP", "a");
  }
  WriteOutlines(OFile, Outlines);
  fclose (OFile);
  *--------------------------------------------------------------------*/

  ExtractIntFeat(Blob, blfeatures, cnfeatures, &FXInfo);
  Baseline = BaselineAt (LineStats, FXInfo.Xmean);
  Scale = ComputeScaleFactor (LineStats);
  Feature->Params[CharNormY] = (FXInfo.Ymean - Baseline) * Scale;
  Feature->Params[CharNormLength] =
    FXInfo.Length * Scale / LENGTH_COMPRESSION;
  Feature->Params[CharNormRx] = FXInfo.Rx * Scale;
  Feature->Params[CharNormRy] = FXInfo.Ry * Scale;

  /*---------Debug--------------------------------------------------*
  File = fopen ("f:/ims/debug/nfFeatSet.logCPP", "r");
  if (File == NULL)
  {
    File = Efopen ("f:/ims/debug/nfFeatSet.logCPP", "w");
    WriteFeatureSet(File, FeatureSet);
  }
  else
  {
    fclose (File);
    File = Efopen ("f:/ims/debug/nfFeatSet.logCPP", "a");
  }
  WriteFeatureSet(File, FeatureSet);
  fclose (File);
  *--------------------------------------------------------------------*/
  FreeOutlines(Outlines); 
  return (FeatureSet);
}                                /* ExtractCharNormFeatures */
コード例 #9
0
 /*
Detects features at extrema in DoG scale space.  Bad features are discarded
based on contrast and ratio of principal curvatures.

@param dog_pyr DoG scale space pyramid
@param octvs octaves of scale space represented by dog_pyr
@param intvls intervals per octave
@param contr_thr low threshold on feature contrast
@param curv_thr high threshold on feature ratio of principal curvatures
@param storage memory storage in which to store detected features

@return Returns an array of detected features whose scales, orientations,
	and descriptors are yet to be determined.
*/
 CvSeq* SiftGPU::ScaleSpaceExtrema( IplImage*** dog_pyr, int octvs, int intvls,
								   float contr_thr, int curv_thr,
								   CvMemStorage* storage )
{
	CvSeq* features;
	float prelim_contr_thr = 0.5 * contr_thr / intvls;
	feature* feat;
	struct detection_data* ddata;
	int o, i, r, c;
	int num=0;				// Number of keypoins detected
	int numRemoved=0;		// The number of key points rejected because they failed a test

	Keys keys[1000];

	int numberExtrema = 0;
	int number = 0;
	int numberRej = 0;
	
	IplImage* img = cvCreateImage( cvGetSize(dog_pyr[0][0]), 32, 1 );

	cvZero(img);

	features = cvCreateSeq( 0, sizeof(CvSeq), sizeof(feature), storage );


	/************************ GPU **************************/
	detectExt->CreateBuffersIn(dog_pyr[0][0]->width*dog_pyr[0][0]->height*sizeof(float),4);
	detectExt->CreateBuffersOut(img->width*img->height*sizeof(float),1);
	/************************ GPU **************************/

	for( o = 0; o < octvs; o++ )
		for( i = 1; i <= intvls; i++ )
		{
			
			if(SIFTCPU)
			{

				int maxNumberKeys = 1000;
				for (int i =0 ; i < maxNumberKeys ; i++)
				{
					keys[i].x = 0.0;
					keys[i].y = 0.0;
					keys[i].intvl = 0.0;
					keys[i].octv = 0.0;
					keys[i].subintvl = 0.0;
					keys[i].scx = 0.0;
					keys[i].scy = 0.0;
					keys[i].mag = 0.0;
					keys[i].ori = 0.0;
				}

				IplImage* img = cvCreateImage( cvGetSize(dog_pyr[o][i]), 32, 1 );
				cvZero(img);
				
				int numberExtrema = 0;
				int number = 0;
				int numberRej = 0;

				for(r = SIFT_IMG_BORDER; r < dog_pyr[o][0]->height-SIFT_IMG_BORDER; r++)
				for(c = SIFT_IMG_BORDER; c < dog_pyr[o][0]->width-SIFT_IMG_BORDER; c++)
					/* perform preliminary check on contrast */
				{
					
						if( abs( pixval32f( dog_pyr[o][i], r, c ) ) > prelim_contr_thr )
						{
							if( IsExtremum( dog_pyr, o, i, r, c ) )
							{

								feat = InterpExtremum(dog_pyr, o, i, r, c, intvls, contr_thr);
								if( feat )
								{
									ddata = FeatDetectionData( feat );

									if( ! IsTooEdgeLike( dog_pyr[ddata->octv][ddata->intvl],
										ddata->r, ddata->c, curv_thr ) )
									{
										num++;
										cvSeqPush( features, feat );
									}
									else
										free( ddata );
									free( feat );
								}
							}
						}
					
				}
			}
			else 
			{
				/************************ GPU **************************/
				
				num = 0;
				detectExt->SendImageToBuffers(3,dog_pyr[o][i-1],dog_pyr[o][i],dog_pyr[o][i+1], gauss_pyr[o][i]);
				
				Keys* keys = detectExt->Process(&num, &numRemoved, prelim_contr_thr, i, o, gauss_pyr[o][i]);
				//detectExt->ReceiveImageData(img);
				
				
				number = num;

				struct detection_data* ddata;

				for(int ik = 0; ik < number ; ik++)
				{ 
					feat = NewFeature();
					ddata = FeatDetectionData( feat );
					feat->img_pt.x = feat->x = keys[ik].scx;
					feat->img_pt.y = feat->y = keys[ik].scy;
					ddata->r = keys[ik].y;
					ddata->c = keys[ik].x;
					ddata->subintvl = keys[ik].subintvl;
					ddata->octv = keys[ik].octv;
					ddata->intvl = keys[ik].intvl;
					feat->scl = keys[ik].scl;
					ddata->scl_octv = keys[ik].scl_octv;
					feat->ori = keys[ik].ori;
					feat->d = 128;

					for(int i = 0; i < 128 ; i++ )
					{
						feat->descr[i] = keys[ik].desc[i];
					}

					cvSeqPush( features, feat );
					free( feat );
				}
				
			}
			/************************ GPU **************************/
		}

	return features;
}
コード例 #10
0
ファイル: mergenf.cpp プロジェクト: mk219533/tesseract-ocr
/*---------------------------------------------------------------------------*/
FLOAT32 CompareProtos(PROTO p1, PROTO p2) {
/*
**  Parameters:
**    p1, p2    protos to be compared
**  Globals: none
**  Operation: Compare protos p1 and p2 and return an estimate of the
**    worst evidence rating that will result for any part of p1
**    that is compared to p2.  In other words, if p1 were broken
**    into pico-features and each pico-feature was matched to p2,
**    what is the worst evidence rating that will be achieved for
**    any pico-feature.
**  Return: Worst possible result when matching p1 to p2.
**  Exceptions: none
**  History: Mon Nov 26 08:27:53 1990, DSJ, Created.
*/
  FEATURE Feature;
  FLOAT32 WorstEvidence = WORST_EVIDENCE;
  FLOAT32 Evidence;
  FLOAT32 Angle, Length;

  /* if p1 and p2 are not close in length, don't let them match */
  Length = fabs (p1->Length - p2->Length);
  if (Length > MAX_LENGTH_MISMATCH)
    return (0.0);

  /* create a dummy pico-feature to be used for comparisons */
  Feature = NewFeature (&PicoFeatDesc);
  Feature->Params[PicoFeatDir] = p1->Angle;

  /* convert angle to radians */
  Angle = p1->Angle * 2.0 * PI;

  /* find distance from center of p1 to 1/2 picofeat from end */
  Length = p1->Length / 2.0 - GetPicoFeatureLength () / 2.0;
  if (Length < 0) Length = 0;

  /* set the dummy pico-feature at one end of p1 and match it to p2 */
  Feature->Params[PicoFeatX] = p1->X + cos (Angle) * Length;
  Feature->Params[PicoFeatY] = p1->Y + sin (Angle) * Length;
  if (DummyFastMatch (Feature, p2)) {
    Evidence = SubfeatureEvidence (Feature, p2);
    if (Evidence < WorstEvidence)
      WorstEvidence = Evidence;
  } else {
    FreeFeature(Feature);
    return 0.0;
  }

  /* set the dummy pico-feature at the other end of p1 and match it to p2 */
  Feature->Params[PicoFeatX] = p1->X - cos (Angle) * Length;
  Feature->Params[PicoFeatY] = p1->Y - sin (Angle) * Length;
  if (DummyFastMatch (Feature, p2)) {
    Evidence = SubfeatureEvidence (Feature, p2);
    if (Evidence < WorstEvidence)
      WorstEvidence = Evidence;
  } else {
    FreeFeature(Feature);
    return 0.0;
  }

  FreeFeature (Feature);
  return (WorstEvidence);

} /* CompareProtos */