我正在尝试检测黑色图像中的白色矩形。我在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++;
}
}