OpenCV houghLinesP 参数 [英] OpenCV houghLinesP parameters

查看:19
本文介绍了OpenCV houghLinesP 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 HoughLinesP 和 Python 中的 OpenCV 时难以在此图像中找到棋盘上的线.

I am having difficulty finding the lines on a chessboard in this image using HoughLinesP with OpenCV in Python.

为了理解HoughLinesP的参数,我想出了以下代码:

In an attempt to understand the parameters of HoughLinesP, I have come up with the following code:

import numpy as np
import cv2
from matplotlib import pyplot as plt
from matplotlib import image as image

I = image.imread('chess.jpg') 
G = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY)

# Canny Edge Detection:
Threshold1 = 150;
Threshold2 = 350;
FilterSize = 5
E = cv2.Canny(G, Threshold1, Threshold2, FilterSize)

Rres = 1
Thetares = 1*np.pi/180
Threshold = 1
minLineLength = 1
maxLineGap = 100
lines = cv2.HoughLinesP(E,Rres,Thetares,Threshold,minLineLength,maxLineGap)
N = lines.shape[0]
for i in range(N):
    x1 = lines[i][0][0]
    y1 = lines[i][0][1]    
    x2 = lines[i][0][2]
    y2 = lines[i][0][3]    
    cv2.line(I,(x1,y1),(x2,y2),(255,0,0),2)

plt.figure(),plt.imshow(I),plt.title('Hough Lines'),plt.axis('off')
plt.show()

我遇到的问题是这只能拾取一行.如果我将 maxLineGap 减少到 1,它会增加数千.

The problem I am having is that this picks up only one line. If I reduce the maxLineGap to 1, it picks up thousands.

我明白为什么会这样,但是我如何选择一组合适的参数来合并所有这些共线线?我错过了什么吗?

I understand why this might be but how do I pick a suitable set of parameters to get all these co-linear lines to merge? Am I missing something?

我想保持代码简单,因为我将其用作此函数的示例.

I would like to keep the code simple as I am using it as an example of this function in action.

在此先感谢您的帮助!

更新:这与 HoughLines 完美配合.

Update: This works perfectly with HoughLines.

而且似乎没有边缘检测问题,因为 Canny 工作得很好.

And there doesn't seem to be edge detection issues as Canny is working just fine.

但是,我仍然需要让 HoughLinesP 工作.有什么想法吗??

However, I still need to get HoughLinesP to work. Any ideas??

这里的图片:结果

推荐答案

好的,我终于找到了问题,并认为我会与其他被此问题逼疯的人分享解决方案.问题是在 HoughLinesP 函数中,有一个额外的参数,lines",这是多余的,因为函数的输出是相同的:

Ok, I finally found the problem and thought I would share the solution for anyone else driven nuts by this. The issue is that in the HoughLinesP function, there is an extra parameter, "lines" which is redundant because the output of the function is the same:

cv2.HoughLinesP(image, rho, theta, threshold[, lines[, minLineLength[, maxLineGap]]])

cv2.HoughLinesP(image, rho, theta, threshold[, lines[, minLineLength[, maxLineGap]]])

这会导致参数出现问题,因为它们以错误的顺序读取.为了避免与参数的顺序混淆,最简单的解决方案是在函数内部指定它们,如下所示:

This is causing issues with the parameters as they are read in the wrong order. To avoid confusion with the order of the parameters, the simplest solution is to specify them inside the function like so:

lines = cv2.HoughLinesP(E,rho = 1,theta = 1*np.pi/180,threshold = 100,minLineLength = 100,maxLineGap = 50)

这完全解决了我的问题,我希望它能帮助其他人.

This totally fixed my problem and I hope it will help others.

这篇关于OpenCV houghLinesP 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆