Search code examples
c#classsurf

How to get value of a variable from another class


I am using Emgu CV's SURF and I used int j = CvInvoke.cvCountNonZero(mask); to find matched pair points. The problem how to return this value into main()?

...
...
...
public static Image<Bgr, Byte> Draw(Image<Gray, Byte> modelImage, Image<Gray, byte> observedImage, out long matchTime)      
{
    HomographyMatrix homography;
    VectorOfKeyPoint modelKeyPoints;
    VectorOfKeyPoint observedKeyPoints;
    Matrix<int> indices;
    Matrix<byte> mask;

    FindMatch(modelImage, observedImage, out matchTime, out modelKeyPoints, out observedKeyPoints, out indices, out mask, out homography);

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

    int j = CvInvoke.cvCountNonZero(mask);
    return result;
}

Solution

  • You can create a simple result object:

    public class DrawingResult {
        public Image<Bgr, Byte> Images { get; private set; }
        public int Count {get; private set; }
    
        public DrawingResult(Image<Bgr, Byte> images, int count) {
           Images = images;
           Count = count;
        }
    }
    

    And in your method:

    public static DrawingResult Draw(Image<Gray, Byte> modelImage, Image<Gray, byte> observedImage, out long matchTime)
    {
        HomographyMatrix homography;
        VectorOfKeyPoint modelKeyPoints;
        VectorOfKeyPoint observedKeyPoints;
        Matrix<int> indices;
        Matrix<byte> mask;
    
        FindMatch(modelImage, observedImage, out matchTime, out modelKeyPoints, out observedKeyPoints, out indices, out mask, out homography);
    
        //Draw the matched keypoints
        Image<Bgr, Byte> result = Features2DToolbox.DrawMatches(modelImage, modelKeyPoints, observedImage, observedKeyPoints,
           indices, new Bgr(255, 255, 255), new Bgr(255, 255, 255), mask, Features2DToolbox.KeypointDrawType.DEFAULT);
    
        int j = CvInvoke.cvCountNonZero(mask);
        return new DrawingResult(result, j);
    }
    

    And in the main method:

    DrawingResult result = MyClass.Draw(...);
    int count = result.Count;
    Image<Bgr, Byte> images = result.Images;