我使用opencv java库,通过计算单应矩阵和透视变换来进行一些图像匹配和对象检测。在运行时,我的系统崩溃了,我收到了下面发布的错误消息,我不知道这意味着什么,也不知道如何解决,因为正如你在下面看到的,它说Assertion failed (count >= 4) in cvFindHomography
,它不清楚
请看下面的代码,它显示了我如何计算单应矩阵
代码:
MatOfPoint2f objPointMat = new MatOfPoint2f();
MatOfPoint2f scenePointMat = new MatOfPoint2f();
objPointMat.fromList(objPoint);
scenePointMat.fromList(scenePoint);
H = Calib3d.findHomography(objPointMat, scenePointMat, Calib3d.RANSAC, 3);
if (H != null) {
if (!H.empty()) {
double det = Core.determinant(H);
if (det > 0.09) {
this.validHMatrix = true;
Log.D(TAG, "descriptorMatcher", this.token+"_valid HMatrix, det(H): "+det);
} else {
this.validHMatrix = false;
Log.D(TAG, "descriptorMatcher", this.token+"_invalid HMatrix, det(H): "+det);
}
Mat objCorners = new Mat(4, 1, CvType.CV_32FC2);
Mat sceneCorners = new Mat(4, 1, CvType.CV_32FC2);
objCorners.put(0, 0, new double[] {0, 0});//top left
objCorners.put(1, 0, new double[] {this.obj.cols(), 0});//top right
objCorners.put(2, 0, new double[] {this.obj.cols(), this.obj.rows()});//bottom right
objCorners.put(3, 0, new double[] {0, this.obj.rows()});//bottom left.
Core.perspectiveTransform(objCorners, sceneCorners, H);// the values inside sceneCorners matrix changes after this line as it is what this function returns.
if (!sceneCorners.empty()) {
double p10 = sceneCorners.get(0, 0)[0] + this.obj.cols();
double p11 = sceneCorners.get(0, 0)[1];// + matFactory.getMatAt(0).cols();
double p20 = sceneCorners.get(1, 0)[0] + this.obj.cols();
double p21 = sceneCorners.get(1, 0)[1];// + matFactory.getMatAt(0).cols() ;
double p30 = sceneCorners.get(2, 0)[0] + this.obj.cols();
double p31 = sceneCorners.get(2, 0)[1];// + matFactory.getMatAt(0).rows();
double p40 = sceneCorners.get(3, 0)[0] + this.obj.cols();
double p41 = sceneCorners.get(3, 0)[1];// + matFactory.getMatAt(0).rows();
Point p1 = new Point(p10, p11);
Point p2 = new Point(p20, p21);
Point p3 = new Point(p30, p31);
Point p4 = new Point(p40, p41);
Core.line(goodMatchesImage, p1, p2, new Scalar(0, 0, 255), 4);
Core.line(goodMatchesImage, p2, p3, new Scalar(0, 255, 0), 4);
Core.line(goodMatchesImage, p3, p4, new Scalar(255, 0, 0), 4);
Core.line(goodMatchesImage, p4, p1, new Scalar(255, 255, 255), 4);
控制台错误:
OpenCV Error: Assertion failed (count >= 4) in cvFindHomography, file ........opencvmodulescalib3dsrcfundam.cpp, line 235
java.util.concurrent.ExecutionException: CvException [org.opencv.core.CvException: cv::Exception: ........opencvmodulescalib3dsrcfundam.cpp:235: error: (-215) count >= 4 in function cvFindHomography
]
at java.util.concurrent.CompletableFuture.reportGet(Unknown Source)
at java.util.concurrent.CompletableFuture.get(Unknown Source)
at com.example.foa17_nolog.Performance.compSIFT(Performance.java:116)
at com.example.foa17_nolog.Performance.compRep(Performance.java:86)
at com.example.foa17_nolog.Performance.<init>(Performance.java:60)
at com.example.foa17_nolog.MainClass$PerfMeasure.get(MainClass.java:216)
at com.example.foa17_nolog.MainClass$PerfMeasure.get(MainClass.java:1)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
要检测单应性,需要给函数至少4个"好"的点。现在发生的情况是,你给函数的图像没有至少4个好的点来计算单应性,这就是为什么你会得到错误。因此,要解决这个错误,你要么必须找到一种获得更多好点的方法,要么写一个if语句,只在图像有4个好点时调用findHomography。