在Android + OpenCV上删除HoughLinesP之后的重复行

时间:2015-12-08 15:49:05

标签: java android opencv houghlinesp

我试图在黑色图像中检测到白色矩形。我在Canny之后使用了HoughLinesP,检测结果准确无误。 问题在于,一些线非常相似并几乎定义了相同的边。所以在HgouhLinesP返回的矩阵中,而不是有4行,而是我有更多。是否可以更改HoughLinesP中的参数以使其仅生成4行?

我试图实现一种方法,我比较所有生成的线的方程,但类似的线似乎有非常不同的方程,结果是错误的。所以基本上我创建了一个4x4矩阵并将第一行放入其中。然后我比较以下几行,当其中一行不同时,我把它放在4x4矩阵等中。我将其余的行与已经在4x4矩阵中的行进行比较。有人可以帮忙吗?

 Imgproc.Canny(diff, diff2, 100, 100, 3);

        Mat lines = new Mat();
        int threshold = 80;
        int minLineSize = 150;
        int lineGap = 80;
        Imgproc.HoughLinesP(diff2, lines, 1, Math.PI / 180, threshold, minLineSize, lineGap);
        double[][] linesFinal = new double[4][4];
        linesFinal[0] = lines.get(0, 0);

        double x01 = linesFinal[0][0],
                y01 = linesFinal[0][1],
                x02 = linesFinal[0][2],
                y02 = linesFinal[0][3];
        double a = y02 - y01 / x02 - x01;
        double b = y01 - a * x01;
        Log.i(TAG, "aaaaaaaaaaaaaaaaaaaaa:    " + String.valueOf(a) + "bbbbbbbbbb     " + String.valueOf(b));
        Point start0 = new Point(x01, y01);
        Point end0 = new Point(x02, y02);

        Core.circle(finaleuh, end0, 10, new Scalar(255, 0, 0, 255), 10);
        Core.circle(finaleuh, start0, 10, new Scalar(255, 0, 0, 255), 10);

        int index = 1;
        int x = 1;


        while (index < 4 && x < lines.cols()) {

            // Log.i(TAG,"xxxxxxxxxxxxxxxx:    "+ String.valueOf(x)+"   indeeeeeex      "+ String.valueOf(index));
            double[] vec = lines.get(0, x);
            double Xi1 = vec[0],
                    Yi1 = vec[1],
                    Xi2 = vec[2],
                    Yi2 = vec[3];
            double Ai = (Yi2 - Yi1) / (Xi2 - Xi1);
            double Bi = Yi1 - Ai * Xi1;
            //  Log.i(TAG,"aaaaaaaaaaaaaaaaaaaaa:    "+ String.valueOf(Ai)+ "bbbbbbbbbb     " + String.valueOf(Bi));

            int counter = 0;
            for (int i = 0; i < index; i++)

            {

                double xF = linesFinal[i][0],
                        yF = linesFinal[i][1],
                        xFF = linesFinal[i][2],
                        yFF = linesFinal[i][3];


                double aF = yFF - yF / xFF - xF;
                double bF = yF - aF * xF;
                Log.i(TAG, "aaaaaaaaaaaaaaaaaaaaa:    " + String.valueOf(aF) + "bbbbbbbbbb     " + String.valueOf(bF));

                double diffFA = Math.abs(aF - Ai);
                double diffFB = Math.abs(bF - Bi);

                if (diffFA > 250 && diffFB > 300) {
                    counter = counter + 1;


                }

            }


            if (counter == index)


            {
                linesFinal[index] = vec;
                double xF = linesFinal[index][0],
                        yF = linesFinal[index][1],
                        xFF = linesFinal[index][2],
                        yFF = linesFinal[index][3];

                Point startF = new Point(xF, yF);
                Point endF = new Point(xFF, yFF);
                Core.circle(finaleuh, endF, 10, new Scalar(255, 0, 0, 255), 10);
                Core.circle(finaleuh, startF, 10, new Scalar(255, 0, 0, 255), 10);
                index++;
                x++;
            } else {
                x++;
            }


        }

1 个答案:

答案 0 :(得分:0)

对于那些可能需要它的人,我毕竟把事情分类了。我基本上用我的线创建了两个类别,在每个类别中,所有线都是平行的。我通过比较第一行的斜率和所有剩余的行来做到这一点。然后在每个类别中,我比较了B的值(来自等式y = Ax + B)。我最终只有4行。