2012-03-31 2 views
-1

EMGU CV에 새로 왔습니다. 나는 SURF를 사용하여 하나 이상의 패턴을 감지하고 싶습니다. 마찬가지로 this 비디오. 하지만 지금은 시작점에 대한 하나의 패턴을 개발하려고합니다.캠이 장착 된 EmguCV SURF?

나는 EMGUCV의 SURF 예제를 검사했다. 이 코드를 캠 캡처의 예에 구현하려고하면 런타임에 오류가 발생합니다. 더 많은 것을 검색했지만 코드 예제를 찾지 못했습니다.

그래서, 제게 설명 된 코드 스 니펫이나 튜토리얼을 제안 해주십시오.

대단히 감사합니다.

아래 코드는 제가 작업하고 있습니다.

........................................... 
FrameRaw = capture.QueryFrame(); 
        CamImageBox.Image = FrameRaw; 
     Run(FrameRaw); 
...........................................  

    private void Run(Image<Bgr, byte> TempImage) 
      { 

       Image<Gray, Byte> modelImage = new Image<Gray, byte>("sample.jpg"); 
       Image<Gray, Byte> observedImage = TempImage.Convert<Gray, Byte>(); 
       // Image<Gray, Byte> observedImage = new Image<Gray,byte>("box_in_scene.png"); 

       Stopwatch watch; 
       HomographyMatrix homography = null; 

       SURFDetector surfCPU = new SURFDetector(500, false); 

       VectorOfKeyPoint modelKeyPoints; 
       VectorOfKeyPoint observedKeyPoints; 
       Matrix<int> indices; 
       Matrix<float> dist; 
       Matrix<byte> mask; 

       if (GpuInvoke.HasCuda) 
       { 
        GpuSURFDetector surfGPU = new GpuSURFDetector(surfCPU.SURFParams, 0.01f); 
        using (GpuImage<Gray, Byte> gpuModelImage = new GpuImage<Gray, byte>(modelImage)) 
        //extract features from the object image 
        using (GpuMat<float> gpuModelKeyPoints = surfGPU.DetectKeyPointsRaw(gpuModelImage, null)) 
        using (GpuMat<float> gpuModelDescriptors = surfGPU.ComputeDescriptorsRaw(gpuModelImage, null, gpuModelKeyPoints)) 
        using (GpuBruteForceMatcher matcher = new GpuBruteForceMatcher(GpuBruteForceMatcher.DistanceType.L2)) 
        { 
         modelKeyPoints = new VectorOfKeyPoint(); 
         surfGPU.DownloadKeypoints(gpuModelKeyPoints, modelKeyPoints); 
         watch = Stopwatch.StartNew(); 

         // extract features from the observed image 
         using (GpuImage<Gray, Byte> gpuObservedImage = new GpuImage<Gray, byte>(observedImage)) 
         using (GpuMat<float> gpuObservedKeyPoints = surfGPU.DetectKeyPointsRaw(gpuObservedImage, null)) 
         using (GpuMat<float> gpuObservedDescriptors = surfGPU.ComputeDescriptorsRaw(gpuObservedImage, null, gpuObservedKeyPoints)) 
         using (GpuMat<int> gpuMatchIndices = new GpuMat<int>(gpuObservedDescriptors.Size.Height, 2, 1)) 
         using (GpuMat<float> gpuMatchDist = new GpuMat<float>(gpuMatchIndices.Size, 1)) 
         { 
          observedKeyPoints = new VectorOfKeyPoint(); 
          surfGPU.DownloadKeypoints(gpuObservedKeyPoints, observedKeyPoints); 

          matcher.KnnMatch(gpuObservedDescriptors, gpuModelDescriptors, gpuMatchIndices, gpuMatchDist, 2, null); 

          indices = new Matrix<int>(gpuMatchIndices.Size); 
          dist = new Matrix<float>(indices.Size); 
          gpuMatchIndices.Download(indices); 
          gpuMatchDist.Download(dist); 

          mask = new Matrix<byte>(dist.Rows, 1); 

          mask.SetValue(255); 

          Features2DTracker.VoteForUniqueness(dist, 0.8, mask); 

          int nonZeroCount = CvInvoke.cvCountNonZero(mask); 
          if (nonZeroCount >= 4) 
          { 
           nonZeroCount = Features2DTracker.VoteForSizeAndOrientation(modelKeyPoints, observedKeyPoints, indices, mask, 1.5, 20); 
           if (nonZeroCount >= 4) 
            homography = Features2DTracker.GetHomographyMatrixFromMatchedFeatures(modelKeyPoints, observedKeyPoints, indices, mask, 3); 
          } 

          watch.Stop(); 
         } 
        } 
       } 
       else 
       { 
        //extract features from the object image 
        modelKeyPoints = surfCPU.DetectKeyPointsRaw(modelImage, null); 
        //MKeyPoint[] kpts = modelKeyPoints.ToArray(); 
        Matrix<float> modelDescriptors = surfCPU.ComputeDescriptorsRaw(modelImage, null, modelKeyPoints); 

        watch = Stopwatch.StartNew(); 

        // extract features from the observed image 
        observedKeyPoints = surfCPU.DetectKeyPointsRaw(observedImage, null); 
        Matrix<float> observedDescriptors = surfCPU.ComputeDescriptorsRaw(observedImage, null, observedKeyPoints); 

        BruteForceMatcher matcher = new BruteForceMatcher(BruteForceMatcher.DistanceType.L2F32); 
        matcher.Add(modelDescriptors); 
        int k = 2; 
        indices = new Matrix<int>(observedDescriptors.Rows, k); 
        dist = new Matrix<float>(observedDescriptors.Rows, k); 
        matcher.KnnMatch(observedDescriptors, indices, dist, k, null); 

        mask = new Matrix<byte>(dist.Rows, 1); 

        mask.SetValue(255); 

        Features2DTracker.VoteForUniqueness(dist, 0.8, mask); 

        int nonZeroCount = CvInvoke.cvCountNonZero(mask); 
        if (nonZeroCount >= 4) 
        { 
         nonZeroCount = Features2DTracker.VoteForSizeAndOrientation(modelKeyPoints, observedKeyPoints, indices, mask, 1.5, 20); 
         if (nonZeroCount >= 4) 
          homography = Features2DTracker.GetHomographyMatrixFromMatchedFeatures(modelKeyPoints, observedKeyPoints, indices, mask, 3); 
        } 

        watch.Stop(); 
       } 

       //Draw the matched keypoints 
       Image<Bgr, Byte> result = Features2DTracker.DrawMatches(modelImage, modelKeyPoints, observedImage, observedKeyPoints, 
        indices, new Bgr(255, 255, 255), new Bgr(255, 255, 255), mask, Features2DTracker.KeypointDrawType.NOT_DRAW_SINGLE_POINTS); 

       #region draw the projected region on the image 
       if (homography != null) 
       { //draw a rectangle along the projected model 
        Rectangle rect = modelImage.ROI; 
        PointF[] pts = new PointF[] { 
        new PointF(rect.Left, rect.Bottom), 
        new PointF(rect.Right, rect.Bottom), 
        new PointF(rect.Right, rect.Top), 
        new PointF(rect.Left, rect.Top)}; 
        homography.ProjectPoints(pts); 

        result.DrawPolyline(Array.ConvertAll<PointF, Point>(pts, Point.Round), true, new Bgr(Color.Red), 5); 
       } 
       #endregion 

       // ImageViewer.Show(result, String.Format("Matched using {0} in {1} milliseconds", GpuInvoke.HasCuda ? "GPU" : "CPU", watch.ElapsedMilliseconds)); 
      } 

답변

0

내가 사용한 SURF tutorial을 찾았지만 오류가 발생하는 이유는 없습니다. GPU 가속화 문제없이 자습서 코드를 단독으로 실행할 수 있습니까? 또한 어떤 오류가 발생 했습니까?

+0

예,이 자습서는 정적 이미지에서 오류를 발생시키지 않습니다. 위의 예제와 같이이 튜토리얼을 구현하려고하면 오류가 발생합니다. – Kerberos

+0

어떤 오류가 있습니까? 예외 클래스의 이름은 무엇입니까? 세부 사항? 아니면 일치하지 않는 유형과 같은 컴파일 타임 오류입니까? – GGulati

+0

이 행에서 오류가 발생합니다. "Image observedImage = TempImage.Convert ();" 오류 메시지는 "매개 변수가 유효하지 않습니다."입니다. 이 코드 "Image observedImage = new Image ("box_in_scene.png "); "대신 해당 줄을 사용하면 오류가 발생하지 않습니다. 하지만 난 캠 캡처와 함께이 방법을 사용할 수 없습니다. – Kerberos