Bladeren bron

fastfilter commited

Michael Kemmler 13 jaren geleden
bovenliggende
commit
4ac6358c7f
2 gewijzigde bestanden met toevoegingen van 56 en 0 verwijderingen
  1. 9 0
      baselib/FastFilter.h
  2. 47 0
      baselib/FastFilter.tcc

+ 9 - 0
baselib/FastFilter.h

@@ -65,6 +65,15 @@ class FastFilter
 	template <class SrcValueType, class DstValueType>
 	static void calcGradientX ( const SrcValueType *img, int xsize, int ysize, DstValueType *d );
 
+	///lazy attempt for realizing fast histogram of oriented gradients
+	///since (neg.) double values are allowed, fast filtering based on look-up tables is no longer possible
+	template <class GrayValueType, class OrientedGradientHistogramType>
+	static void calcOrientedGradientHistogram ( const GrayValueType *gray, 
+						    int xsize, int ysize,
+						    OrientedGradientHistogramType *hog, 
+						    int numBins, 
+						    bool usesigned );
+
 };
 
 } // namespace

+ 47 - 0
baselib/FastFilter.tcc

@@ -171,3 +171,50 @@ void FastFilter::calcGradient ( const GrayValueType *gray,
     delete [] gy;
 }
 
+
+//lazy attempt for realizing fast histogram of oriented gradients
+//since (neg.) double values are allowed, fast filtering based on look-up tables is no longer possible
+template <class GrayValueType, class OrientedGradientHistogramType>
+void FastFilter::calcOrientedGradientHistogram ( const GrayValueType *gray, 
+						 int xsize, int ysize,
+						 OrientedGradientHistogramType *hog, 
+						 int numBins, 
+						 bool usesigned )
+{
+    double *gx = new double[xsize*ysize];
+    calcGradientX ( gray, xsize, ysize, gx );
+    double *gy = new double[xsize*ysize];
+    calcGradientY ( gray, xsize, ysize, gy );
+    
+    double binq = usesigned ? 2*M_PI / numBins : M_PI / numBins;
+
+    for(int i=0; i<numBins; i++) hog[i] = (OrientedGradientHistogramType) 0.0;
+
+    int k = 0;
+    for ( int y = 0 ; y < ysize ; y++ )
+	for ( int x = 0 ; x < xsize ; x++,k++ )
+	{
+	    double rx = gx[k];
+	    double ry = gy[k];
+	    //GradientMagnitudeType g  = (GradientMagnitudeType)sqrt ( rx*rx + ry*ry );
+	    GrayValueType g  = (GrayValueType) sqrt(rx*rx + ry*ry);
+
+	    double angle = atan2(ry,rx);
+
+	    if ( usesigned ) {
+		if ( angle < 0 )
+		    angle = 2*M_PI + angle;
+	    } else {
+		if ( angle < 0 )
+		    angle = M_PI + angle;
+	    }
+
+	    uint bin = (uint)(angle / binq);
+            bin = bin < numBins ? bin : numBins - 1;
+	    hog[bin]+= (OrientedGradientHistogramType) g;
+	    
+	}
+
+    delete [] gx;
+    delete [] gy;
+}