Unity and matches[i].distance - how do I filter matches
I'm attempting to implement image comparison in Unity3D using OpenCVForUnity.
I can't seem to filter matches and need some help.
From all the posts that I've read, I should be able to filter my matches between two images by using distance. However matches[i].distance doesn't seem to exist in the Unity plugin.
The following code will simply error out due to the missing call to 'distance'
for( int i = 0; i < descriptors_1.rows; i++ )
{ double dist = matches[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
}
So I ran a test on my matches.
for (int i = 0; i < matches.cols (); i++)
{
for(int j = 0; j < matches.rows (); j++)
{
Debug.Log("Row " + i + " Column " + j + " Value " + matches.get(i,j));
}
}
The above always returns 'Row 0 Column 0 Value System.Double[]' for the first entry and 'Row 0 Column 1 Value ' for the following 499 entries.
I do see lines between the matches. However I see 500 lines regardless of the quality of the match. I guess that is expected as it's my duty to filter those matches (as I'm trying to do).
My entire code is:
void Start ()
{
Texture2D imgTexture;
if (Random.value < .5f)
imgTexture = Resources.Load ("lena") as Texture2D;
else
imgTexture = Resources.Load ("triangular-face") as Texture2D;
Texture2D imgTexture2 = Resources.Load ("lena") as Texture2D;
Mat img1Mat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC3);
Utils.texture2DToMat (imgTexture, img1Mat);
Mat img2Mat = new Mat (imgTexture2.height, imgTexture2.width, CvType.CV_8UC3);
Utils.texture2DToMat (imgTexture2, img2Mat);
MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
Mat descriptors1 = new Mat();
Mat descriptors2 = new Mat();
FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
detector.detect(img1Mat, keypoints1);
detector.detect(img2Mat, keypoints2);
extractor.compute(img1Mat, keypoints1, descriptors1);
extractor.compute(img2Mat, keypoints2, descriptors2);
DescriptorMatcher matcher = DescriptorMatcher.create (DescriptorMatcher.BRUTEFORCE_HAMMINGLUT);
MatOfDMatch matches = new MatOfDMatch();
matcher.match(descriptors1,descriptors2 ,matches);
for (int i = 0; i < matches.cols (); i++)
{
for(int j = 0; j < matches.rows (); j++)
{
Debug.Log("Row " + i + "Column " + j + "Value " + matches.get(i,j));
}
}
Mat resultImg = new Mat ();
Features2d.drawMatches (img1Mat, keypoints1, img2Mat, keypoints2, matches, resultImg);
Texture2D texture = new Texture2D (resultImg.cols (), resultImg.rows (), TextureFormat.RGBA32, false);
Utils.matToTexture2D (resultImg, texture);
gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
}