是否可以将 Houghlines 函数的输出转换为 HoughlinesP 函数之一?

问题描述 投票:0回答:1

我有一张图像,我想从这张图像中去掉红色网格。我已经尝试过使用 HoughlinesP 函数并且可以工作(参见 enter image description here),但现在我想使用 HoughLines 函数来执行此操作,因为这样更快。问题是这个的输出不是 [x1,y1,x2,y2],而是 [rho, theta]。

我已经尝试过这样交谈:

line_coordinates = []
for line in lines:
    for rho,theta in line:
        x1 = int(rho * math.cos(theta))
        y1 = int(rho * math.sin(theta))
        x2 = int(x1 + 1000 * (-math.sin(theta)))  
        y2 = int(y1 + 1000 * (math.cos(theta)))
        line_coordinates.append([x1, y1, x2, y2])

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

但是如果我这样做,那么我会得到这样的结果:enter image description here,我怎样才能使这个转换更好?

我将其用作函数:

imgOriginal = cv2.imread(path, cv2.IMREAD_COLOR)
hsv = cv2.cvtColor(imgOriginal, cv2.COLOR_BGR2HSV)

mask_red = cv2.inRange(hsv, np.array([150, 25, 150]), np.array([180, 255, 255]))
mask_red_extra = cv2.inRange(hsv, np.array([1, 25, 150]), np.array([40, 255, 255]))
red_mask = mask_red + mask_red_extra
cv2.imshow('edges', red_mask)
threshold = 150
lines = cv2.HoughLines(red_mask, 1, np.pi/180, threshold)

hough_lines = cv2.HoughLinesP(red_mask, 1, np.pi / 180, threshold=50, minLineLength=75, maxLineGap=300)

原图在这里:enter image description here

python opencv houghlinesp houghlines
1个回答
0
投票

使用

HoughLines
代替
HoughLinesP

片段:

import cv2
import numpy as np

img = cv2.imread('l.png')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

edges = cv2.Canny(gray, 50, 150, apertureSize=3)

lines = cv2.HoughLines(edges, 1, np.pi/180, 200)

for r_theta in lines:
    arr = np.array(r_theta[0], dtype=np.float64)
    r, theta = arr
    # Stores the value of cos(theta) in a
    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.imshow('linesDetected', img)
cv2.waitKey(0)
© www.soinside.com 2019 - 2024. All rights reserved.