OpenCV houghLinesP参数 [英] OpenCV houghLinesP parameters

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

问题描述

使用带有OpenCV的HoughLinesP在Python中,我很难在此图像中的棋盘上找到线条.

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(图像,rho,theta,阈值[,线 [,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天全站免登陆