OpenCV/Python中的粗线 [英] Hough lines in OpenCV/Python

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

问题描述

我正在尝试在python中使用opencv在图像中查找霍夫线.

I'm trying to find hough lines in an image using opencv in python.

我的代码是:

import cv2
import numpy as np

img = cv2.imread('DLMIA.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)


edges = cv2.Canny(gray,100,200,apertureSize = 3)
cv2.imshow('edges',edges)
cv2.waitKey(0)

minLineLength = 30
maxLineGap = 10
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap)
for x1,y1,x2,y2 in lines[0]:
    cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

cv2.imshow('hough',img)
cv2.waitKey(0)

我使用的图像是.

我得到的图像是.

我的代码示例摘自此处.

生成的图像与上一个链接中提到的图像不同.有什么帮助吗?

The resulted image is not the same as noted in the previous link. Any help please?

推荐答案

我找到了解决方案.

该代码示例仅显示第一行.

The code example only shows the first hough line.

如果要在图像上打印所有线条,则必须打印所有线条.

In case you want to print all the hough lines on an image you have to print all lines.

这是更正的代码:

import cv2
import numpy as np

img = cv2.imread('dave.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)


edges = cv2.Canny(gray,100,200,apertureSize = 3)
cv2.imshow('edges',edges)
cv2.waitKey(0)

minLineLength = 30
maxLineGap = 10
lines = cv2.HoughLinesP(edges,1,np.pi/180,15,minLineLength=minLineLength,maxLineGap=maxLineGap)
for x in range(0, len(lines)):
    for x1,y1,x2,y2 in lines[x]:
        cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

cv2.imshow('hough',img)
cv2.waitKey(0)

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

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