简体   繁体   English

在Python中使用OpenCv检测几乎直线

[英]Detecting almost straight lines using OpenCv in Python

I am detecting straight lines in an image using OpenCv. 我正在使用OpenCv检测图像中的直线。 Below is the code: 下面是代码:

import cv2
import numpy as np

img = cv2.imread('Image.jpg')
img = img[:, 10:img.shape[1]-10]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
minLineLength = img.shape[1] - 300
lines = cv2.HoughLinesP(image=edges, rho=0.02, theta=np.pi / 500, threshold=10, lines=np.array([]), minLineLength=minLineLength, maxLineGap=2)
a, b, c = lines.shape
for i in range(a):
    cv2.line(img, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 2, cv2.LINE_AA)
cv2.imwrite('result.png', img)

For the image( Screenshot of a PDF ) Image.jpg ( Below ) I am getting result.png ( Below ) as a result which is exactly the output I desire. 对于图像( PDF的截图Image.jpg ),我得到的是result.png ),其结果恰好是我想要的输出。

Image.jpg image.jpg的 在此处输入图片说明

result.png result.png 在此处输入图片说明

But when I give the below Image Test.jpg as an input, my algorithm is not working correctly. 但是,当我将下面的Image Test.jpg作为输入时,我的算法无法正常工作。 It is giving the following error: 它给出了以下错误:

a, b, c = lines.shape # 10th Line
AttributeError: 'NoneType' object has no attribute 'shape'

I think because in Test.jpg the horizontal lines are not that straight( because I clicked this by a phone's camera ) and also If I change the minLineLength value to let's say 100 it is not showing the above error but showing incomplete faded lines on each row. 我认为是因为在Test.jpg中 ,水平线不是那么直( 因为我通过手机的摄像头单击了它 ),而且如果我将minLineLength值更改为100 ,则不会显示上述错误,而是在每条显示不完整的褪色线行。 So can anyone please tell me what params should I change in my algorithm to make it work correctly? 那么,谁能告诉我在算法中应该更改哪些参数才能使其正常工作?

Test.jpg test.jpg放在 在此处输入图片说明

You need to loosen up on your definition of "straight". 您需要放松对“直线”的定义。 The documentation is clear if you are already familiar with the terminology of the geometry. 如果您已经熟悉几何的术语,则说明文件很清楚。 "rho" and "theta" are the variables for polar coordinates: length and direction, in this case. “ rho”和“ theta”是极坐标的变量:在这种情况下为长度和方向。 Since you're worried about a variation in direction, you need to loosen up on the theta value 由于您担心方向变化,因此需要放宽theta值

theta=np.pi / 500

Is too restrictive (PI/500 radians, just over 1/3 of a degree). 过于严格(PI / 500弧度,刚好超过1/3度)。 Decrease that 500 figure until you have a result you like. 减少500数字,直到获得所需的结果。 For instance, try starting with 90 (1 degree). 例如,尝试从90 (1度)开始。

possibly add a condition to test if there is a line. 可能添加条件以测试是否有一行。 remember you are looking for lines of a particular length. 请记住,您正在寻找特定长度的线。 the second image has none. 第二张图片没有任何图片。 i tried your code and commented 我尝试了您的代码并发表了评论

minLineLength = img.shape[1] - 300

your code works with only particluar images. 您的代码仅适用于特定图像。 if you need to use any image comment out the lines 如果您需要使用任何图像注释掉行

#img = img[:, 10:img.shape[1]-10]
minLineLength = 10 #img.shape[1] - 300

it works if this is the case the final code i executed is 这是可行的,如果是这种情况,我执行的最终代码是

import cv2
import numpy as np
img = cv2.imread('test.jpg')
#img = img[:, 10:img.shape[1]-10]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
minLineLength = 20 #img.shape[1] - 300
lines = cv2.HoughLinesP(image=edges, rho=0.02, theta=np.pi / 500,     threshold=10, lines=np.array([]), minLineLength=minLineLength, maxLineGap=2)
a, b, c = lines.shape

for i in range(a):
    cv2.line(img, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2],  lines[i][0][3]), (0, 0, 255), 2, cv2.LINE_AA)
cv2.imwrite('result.png', img)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM