0

I'm trying to create an image depicting matches between keypoints in images generated from sift files, using the Features2d.drawMatches method from openCV java API. I can't seem to figure out what kind of argument the method will take as an output parameter - I keep getting the following Assertion Error:

OpenCV(3.4.1) Error: Assertion failed (!outImage.empty()) in 
cv::drawKeypoints, file C:\build\master_winpack-bindings-win32-vc14- 
static\opencv\modules\features2d\src\draw.cpp, line 115
Exception in thread "main" CvException [org.opencv.core.CvException:  
cv::Exception: OpenCV(3.4.1) C:\build\master_winpack-bindings-win32-vc14- 
static\opencv\modules\features2d\src\draw.cpp:115: error: (-215) 
!outImage.empty() in function cv::drawKeypoints
]
    at org.opencv.features2d.Features2d.drawMatches_1(Native Method)
    at org.opencv.features2d.Features2d.drawMatches(Features2d.java:71)
    at com.company.GUI.ImagesView.matchPoints(ImagesView.java:94)
    at com.company.GUI.ImagesView.<init>(ImagesView.java:69)
    at com.company.Main.main(Main.java:17)

My code:

private void matchPoints() {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    MatOfKeyPoint matKey1 = new MatOfKeyPoint(keyPoints1);
    MatOfKeyPoint matKey2 = new MatOfKeyPoint(keyPoints2);
    MatOfDMatch matDMatch = new MatOfDMatch(matches);
    Mat output = new Mat();
    //output = new Mat(matKey1.rows(), matKey1.cols(), CvType.CV_8U, Scalar.all(0));
    if (!output.empty())
        System.out.println("not empty");
    else
        System.out.println("empty");
    Features2d.drawMatches(mat1, matKey1, mat2, matKey2, matDMatch, output);
    HighGui.imshow("Matches", output);
}

The exact same assertion error shows up whether or not I un-comment the commented line, despite the empty() check below returning different values for these two Mats. I'm at loss, help would be much appreciated.

NaN
  • 11
  • 1
  • Are `mat1` and `mat2` both 3 channel images, or are they grayscale? FWIW the number of rows and columns of `matKey1` is definitely not the same as the output size of the image. Keypoints are drawn on horizontally concatenated images, so you'd have the sum of the widths of the images for the width of output, and then the max of the height of the two images would be the height of the output image. However, that shouldn't matter---AFAIK you should be able to pass the output just from a default constructor like you do in the above code. – alkasm May 28 '18 at 22:32
  • Thanks for trying to help! Both mat1 and mat2 are 3 channel. Yeah, you're definitely right - I didn't pay that much attention to it while I was trying to get rid of that pesky assertion (I went to check just in case, but it didn't change anything). Could the error be caused by a dependency configuration, or maybe there's a chance it's a java openCV bug? – NaN May 28 '18 at 22:47
  • Yeah I'm not sure, I don't have OpenCV compiled for Java. Wish I could be more help, but otherwise your code looks like it follows what documentation and examples seem to show, so I'm out of ideas. You could try actually filling the two images into the `Mat` (copy to ROIs inside the output image) and changing the flag to whatever is correct to only draw on top of the output image instead of adding the images in. – alkasm May 28 '18 at 23:26
  • I think the problem might be in your matches, how do you work out the matches? – Rick M. May 29 '18 at 11:35

1 Answers1

0

I can't comment, so I must write an answer:
I had the same problem and somehow solved it. It is no dependency error - I have solved it without changing my dependencies (or imported libraries).
If the matches computed correctly, there won't occure this specific error when calling this function.
In my code, the error was the call of the compute function. I tried to call this function with an FastFeatureDetector - but I somewhere have read that this detector can't find any matches.

Try to compute the
ORB extractor = ORB.create();
extractor.compute(currentFrame, keyPoints, imgDescriptor);

When trying to compute the imgDescriptor, no error should occur; after matching you should be able to draw the matches.
Hope, I helped you or anyone else struggling with this kinda error.