首先,很抱歉再次向该主题提问。我很清楚这里有很多问题和答案。我读过其中的一些但我的问题是我仍然无法弄清楚我做错了什么。这是我的代码:
Collections.sort(hLines, new Comparator<Line>() {
@Override
public int compare(Line lhs, Line rhs) {
if ( lhs.p1.y < rhs.p1.y){
if (lhs.p2.y < rhs.p2.y)
return 1;
else
return -1;
}
if (lhs.p1.y > rhs.p1.y){
if (lhs.p2.y > rhs.p2.y)
return -1;
else
return 1;
}
else
return 0;
}
});
Collections.sort(vLines, new Comparator<Line>() {
@Override
public int compare(Line lhs, Line rhs) {
if ( lhs.p1.x < rhs.p1.x){
if (lhs.p2.x < rhs.p2.x)
return 1;
else
return -1;
}
if (lhs.p1.x > rhs.p1.x){
if (lhs.p2.x > rhs.p2.x)
return -1;
else
return 1;
}
else
return 0;
}
});
似乎我只是盲目地看到我的错误,所以如果你们中的某个人可以帮助我,我会非常感激。
编辑: 我想要做的是确定一条直线是坐标系中的上,下,最左或最右边的线,它的左上角是0/0坐标。这些点是double类型。这是错误消息:
06-03 10:42:22.576: E/OpenCV_NativeCamera(15810): CameraHandler::Notify: msgType=4 ext1=0 ext2=0
06-03 10:42:22.815: E/OpenCV_NativeCamera(15810): CameraHandler::Notify: msgType=4 ext1=1 ext2=0
06-03 10:42:22.848: E/OpenCV_NativeCamera(15810): CameraHandler::Notify: msgType=4 ext1=1 ext2=0
06-03 10:42:26.408: E/OpenCV_NativeCamera(15810): CameraHandler::Notify: msgType=4 ext1=0 ext2=0
06-03 10:42:26.747: E/OpenCV_NativeCamera(15810): CameraHandler::Notify: msgType=4 ext1=1 ext2=0
06-03 10:42:26.781: E/OpenCV_NativeCamera(15810): CameraHandler::Notify: msgType=4 ext1=1 ext2=0
06-03 10:42:29.474: E/OpenCV_NativeCamera(15810): CameraHandler::Notify: msgType=4 ext1=0 ext2=0
06-03 10:42:30.613: E/OpenCV_NativeCamera(15810): CameraHandler::Notify: msgType=4 ext1=0 ext2=0
06-03 10:42:30.646: E/OpenCV_NativeCamera(15810): CameraHandler::Notify: msgType=4 ext1=0 ext2=0
06-03 10:42:30.874: E/AndroidRuntime(15810): FATAL EXCEPTION: Thread-2592
06-03 10:42:30.874: E/AndroidRuntime(15810): java.lang.IllegalArgumentException: Comparison method violates its general contract!
06-03 10:42:30.874: E/AndroidRuntime(15810): at java.util.TimSort.mergeHi(TimSort.java:864)
06-03 10:42:30.874: E/AndroidRuntime(15810): at java.util.TimSort.mergeAt(TimSort.java:481)
06-03 10:42:30.874: E/AndroidRuntime(15810): at java.util.TimSort.mergeForceCollapse(TimSort.java:422)
06-03 10:42:30.874: E/AndroidRuntime(15810): at java.util.TimSort.sort(TimSort.java:219)
06-03 10:42:30.874: E/AndroidRuntime(15810): at java.util.TimSort.sort(TimSort.java:169)
06-03 10:42:30.874: E/AndroidRuntime(15810): at java.util.Arrays.sort(Arrays.java:2038)
06-03 10:42:30.874: E/AndroidRuntime(15810): at java.util.Collections.sort(Collections.java:1891)
06-03 10:42:30.874: E/AndroidRuntime(15810): at com.example.camera.RectangleDetector.drawLines(RectangleDetector.java:108)
06-03 10:42:30.874: E/AndroidRuntime(15810): at com.example.camera.RectangleDetector.findRectangle(RectangleDetector.java:94)
06-03 10:42:30.874: E/AndroidRuntime(15810): at com.example.camera.MainActivity.onCameraFrame(MainActivity.java:114)
06-03 10:42:30.874: E/AndroidRuntime(15810): at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:387)
06-03 10:42:30.874: E/AndroidRuntime(15810): at org.opencv.android.NativeCameraView$CameraWorker.run(NativeCameraView.java:177)
06-03 10:42:30.874: E/AndroidRuntime(15810): at java.lang.Thread.run(Thread.java:838)
答案 0 :(得分:5)
这不是正确的顺序。
考虑第一种方法:
如果第一个(左)点的y坐标都小于第二个(右)点的y坐标,则返回1,否则返回-1。
这意味着如果你将y坐标为4和6的Point与y坐标为6和4的Point进行比较,那么无论哪个Point是第一个参数,你都将返回-1,这是相当于说Point1&#34;&lt;&#34; Point2和Point2&#34;&lt;&#34; Point1,只有在Point1&#34; ==&#34; POINT2。
您的compare
方法应该处理lhs.p1.y和rhs.p1.y(<
,>
,==
)和lhs.p2.y和rhs.p2.y(<
,>
,==
)的关系。即你必须覆盖表格的9个条件(lhs.p1.y rel rhs.p1.y&amp;&amp; lhs.p2.y rel rhs.p2.y),其中rel是<
,{{1 }或>
。