简体   繁体   English

Hough Line 仅在 python 中写入 1 行,其中 OpenCV 和 Numpy

[英]Hough Line only writes 1 line in python with OpenCV and Numpy

I'm learning to use HoughLines() with Python, OpenCV, and numpy.我正在学习将 HoughLines() 与 Python、OpenCV 和 numpy 一起使用。 I use the following image:我使用下面的图片:

在此处输入图像描述

And i'm trying to detect all of the lines, not just the thickest lines.我正在尝试检测所有线条,而不仅仅是最粗的线条。 Here is the result:结果如下:

在此处输入图像描述

And here is my code:这是我的代码:

import cv2
import numpy as np
import argparse

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="path to the imput image")
args = vars(ap.parse_args())

img = cv2.imread(args['image'])
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
cv2.imwrite('edges_found.jpg',edges)
lines = cv2.HoughLines(edges, 1, np.pi/180, 200)

for r,theta in lines[0]:
    a = np.cos(theta)
    b=np.sin(theta)
    x0 = a*r
    y0 = b*r
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int(y0 - 1000*(a))

    cv2.line(img, (x1,y1), (x2,y2), (0,0,255), 2)

cv2.imwrite('linesDetected.jpg',img)

I am using the tutorial / code here: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html我在这里使用教程/代码: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html

This seems straight forward but in the tutorial, the images used is a sudoku in a newspaper and many lines are returned where I'm getting a single line.这看起来很简单,但在本教程中,使用的图像是报纸上的数独,并且在我得到单行的地方返回了许多行。 As you can see in the code, I have output edges to a separate image to see what is going on there, and this is the result:正如您在代码中看到的,我将 output edges设置为单独的图像,以查看那里发生了什么,结果如下:

在此处输入图像描述

It appears that many edges have been found, yet I'm only getting a single red line, rather than many.似乎找到了许多边缘,但我只得到一条红线,而不是很多。 Where is my code incorrect?我的代码在哪里不正确?

This is because you only display one line in your code, there are actually more than just one.这是因为您只在代码中显示一行,实际上不止一行。

for r,theta in lines[0]:
    a = np.cos(theta)
    b=np.sin(theta)
    x0 = a*r
    y0 = b*r
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int(y0 - 1000*(a))

    cv2.line(img, (x1,y1), (x2,y2), (0,0,255), 2)

should be应该

for line in lines:
    for r,theta in line:
        a = np.cos(theta)
        b=np.sin(theta)
        x0 = a*r
        y0 = b*r
        x1 = int(x0 + 1000*(-b))
        y1 = int(y0 + 1000*(a))
        x2 = int(x0 - 1000*(-b))
        y2 = int(y0 - 1000*(a))

        cv2.line(img, (x1,y1), (x2,y2), (0,0,255), 2)

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

相关问题 Python和OpenCV-Hough Line变换中的优势线 - Python and OpenCV - Dominant Line in Hough Line transform 即使图像在Python中的OpenCV中包含许多行,Hough Line Transform也只识别一行 - Hough Line Transform identifies only one line even though image contains many lines in OpenCV in Python 使用 Hough Transform、OpenCV 和 python 进行平行线检测 - Parallel Line detection using Hough Transform, OpenCV and python Python:仅写入输出的最后一行 - Python: Only writes last line of output 在Python中使用.write()只能写一行 - Using .write() in Python only writes a single line Python 仅写入 json 的第一行 - Python writes only first line of json 为什么 Opencv/Hough 变换找不到整行? - Why is Opencv/Hough Transform not finding the whole line? Python / OpenCV - 在OpenCV中使用两种不同的Hough Line方法检测网球场中的线 - 得到不同的结果 - Python/OpenCV - Detect lines in a tennis court using two differents methods of Hough Line in OpenCV - Get differents results (opencv+python) 在使用概率霍夫线变换时去除重叠线 - (opencv+python) Getting rid of overlapping lines when using probabilistic hough line transform Python opencv 概率霍夫线变换 - TypeError:'NoneType' 类型的对象没有 len() - Python opencv probabilistic Hough line transform - TypeError: object of type 'NoneType' has no len()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM