我正在使用OpenCV的Houghlines尝试在图形中查找矩形,但是我使用的例程似乎生成的线的端点沿边缘和对角线。
我使用的代码来自我在StackOverflow Houghlines in Android with OpenCV的其他地方找到的Java代码。对于Kotlin,它已经进行了更改,但是我想知道,即使Kotlin与Java紧密相关,数据格式在两种语言之间是否也有所改变:
private fun HoughlinesTest(baseMat : Mat) {
val grayMat = Mat()
cvtColor(baseMat, grayMat, Imgproc.COLOR_BGRA2GRAY)
val RHO : Double = 1.0
val THETA : Double = Math.PI / 180
val threshold : Int = 50
val lines = Mat()
Imgproc.HoughLinesP(grayMat, lines, RHO, THETA, threshold)
val MIN_DIST = 10.0
for (x in 0 until lines.rows()) {
val vec = lines.get(x, 0)
val x1 = vec[0]
val y1 = vec[1]
val x2 = vec[2]
val y2 = vec[3]
val start = Point(x1, y1)
val end = Point(x2, y2)
val dx = x1 - x2
val dy = y1 - y2
val dist = Math.sqrt(dx * dx + dy * dy)
if (dist > MIN_DIST) {
Imgproc.line(grayMat, start, end, Scalar(0.0, 255.0, 0.0, 255.0), 5)// here initimg is the original image.
}
}
val linesBitmap = Bitmap.createBitmap(grayMat.width(), grayMat.height(), Bitmap.Config.ARGB_8888)
Utils.matToBitmap(grayMat, linesBitmap)
imageView.setBitmap(linesBitmap)
}
显示原始图像和输出图像。
由于某些原因,我打印出的输出样本显示了点在边缘处的位置。
I/System.out: Line at {0.0, 53.0} to {142.0, 195.0} with a length of 200.8
I/System.out: Line at {35.0, 0.0} to {186.0, 151.0} with a length of 213.5
I/System.out: Line at {0.0, 74.0} to {121.0, 195.0} with a length of 171.1
I/System.out: Line at {32.0, 0.0} to {186.0, 154.0} with a length of 217.8
I/System.out: Line at {67.0, 0.0} to {186.0, 119.0} with a length of 168.3
I/System.out: Line at {7.0, 0.0} to {186.0, 179.0} with a length of 253.1
如果我忽略所有具有边界坐标的线,我仍然沿对角线得到线: