cvFindHomography 抛出错误
cvFindHomography throws an error
我正在使用 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 ..\..\..\..\opencv\modules\calib3d\src\fundam.cpp, line 235
java.util.concurrent.ExecutionException: CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\calib3d\src\fundam.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 个 "good" 点。正在发生的事情是,您提供给函数的图像没有至少 4 个好点来计算单应性,这就是您收到错误的原因。因此,要解决该错误,您要么必须找到一种方法来获得更多优点,要么编写一个 if 语句,仅在图像具有 4 个优点时调用 findHomography。
我正在使用 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 ..\..\..\..\opencv\modules\calib3d\src\fundam.cpp, line 235
java.util.concurrent.ExecutionException: CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\calib3d\src\fundam.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 个 "good" 点。正在发生的事情是,您提供给函数的图像没有至少 4 个好点来计算单应性,这就是您收到错误的原因。因此,要解决该错误,您要么必须找到一种方法来获得更多优点,要么编写一个 if 语句,仅在图像具有 4 个优点时调用 findHomography。