Bahar Jalali

Bahar Jalali

  • NA
  • 185
  • 301.8k

how can i change my function to a faster function?

Jul 4 2012 2:39 AM
hi
i have a function that it's very slow, about 300 milisecond!

i need to change it to a faster way!

how can i use thread to change it to a higher function?

private void FindStopSign(Image<Bgr, byte> img, List<Image<Gray, Byte>> stopSignList, List<Rectangle> boxList, Contour<Point> contours)
      {
         for (; contours != null; contours = contours.HNext)
         {
            contours.ApproxPoly(contours.Perimeter * 0.02, 0, contours.Storage);
            if (contours.Area > 500)
            {
               double ratio = CvInvoke.cvMatchShapes(_octagon, contours, Emgu.CV.CvEnum.CONTOURS_MATCH_TYPE.CV_CONTOURS_MATCH_I3, 0);
             
               if (ratio > 0.1) //not a good match of contour shape
               {
                 
                  Contour<Point> child = contours.VNext;
                  if (child != null)
                  {
                      FindStopSign(img, stopSignList, boxList, child);
                  }
                  continue;
               }
               
               Rectangle box = contours.BoundingRectangle;
               
               Image<Gray, Byte> candidate;
               using (Image<Bgr, Byte> tmp = img.Copy(box))
               {
                   candidate = tmp.Convert<Gray, byte>();
               }
               //set the value of pixels not in the contour region to zero
               using (Image<Gray, Byte> mask = new Image<Gray, byte>(box.Size))
               {
                 
                  mask.Draw(contours, new Gray(255), new Gray(255), 0, -1, new Point(-box.X, -box.Y));
                 
                  double mean = CvInvoke.cvAvg(candidate, mask).v0;
                  candidate._ThresholdBinary(new Gray(mean), new Gray(255.0));
                  candidate._Not();
                  mask._Not();
                  candidate.SetValue(0, mask);
               }

                   ImageFeature[] features = _detector.DetectFeatures(candidate, null);
                   Features2DTracker.MatchedImageFeature[] matchedFeatures = _tracker.MatchFeature(features, 2, 20);
                   int goodMatchCount = 0;
                   foreach (Features2DTracker.MatchedImageFeature ms in matchedFeatures)
                       if (ms.SimilarFeatures[0].Distance < 0.5) goodMatchCount++;


                   if (goodMatchCount >= 10)
                   {
                       boxList.Add(box);
                       stopSignList.Add(candidate);
                   }
            }
         }
      }