给出不要使用hough函数来进行Hough变换,然后不使用houghpeaks函数来寻找峰值,最后不使用houghlines函数来提取直线的代码,
时间: 2023-05-19 17:05:06 浏览: 134
可以使用霍夫变换的自定义实现来代替hough函数,使用非极大值抑制算法来寻找峰值,最后使用直线拟合算法来提取直线。以下是一个示例代码:
import numpy as np
import cv2
def hough_transform(img, theta_res=1, rho_res=1):
# Define the ranges of theta and rho
thetas = np.deg2rad(np.arange(-90.0, 90.0, theta_res))
width, height = img.shape
diag_len = np.ceil(np.sqrt(width * width + height * height)) # maximum possible length
rhos = np.arange(-diag_len, diag_len, rho_res)
# Create an accumulator array to store the votes
accumulator = np.zeros((len(rhos), len(thetas)), dtype=np.uint8)
# Find the indices of the non-zero pixels in the binary image
y_idxs, x_idxs = np.nonzero(img)
# Vote in the accumulator array
for i in range(len(x_idxs)):
x = x_idxs[i]
y = y_idxs[i]
for j in range(len(thetas)):
rho = int((x * np.cos(thetas[j]) + y * np.sin(thetas[j])) + diag_len)
accumulator[rho, j] += 1
return accumulator, thetas, rhos
def non_max_suppression(accumulator, threshold):
# Find local maxima above threshold
loc_max = (accumulator == cv2.dilate(accumulator, np.ones((3, 3))) &
(accumulator > threshold))
# Find the indices of the local maxima
loc_max_idxs = np.argwhere(loc_max)
# Get the values of the local maxima
loc_max_vals = accumulator[loc_max]
# Sort the local maxima by their values in descending order
sorted_idxs = np.argsort(loc_max_vals)[::-1]
# Keep track of the indices of the peaks
peak_idxs = []
# Iterate over the sorted local maxima
for idx in sorted_idxs:
# Get the coordinates of the current peak
rho_idx, theta_idx = loc_max_idxs[idx]
# Check if the current peak is too close to any of the previous peaks
too_close = False
for peak_idx in peak_idxs:
peak_rho_idx, peak_theta_idx = peak_idx
if abs(rho_idx - peak_rho_idx) < 20 and abs(theta_idx - peak_theta_idx) < 20:
too_close = True
break
# If the current peak is not too close to any of the previous peaks, add it to the list of peaks
if not too_close:
peak_idxs.append((rho_idx, theta_idx))
# Return the indices of the peaks
return peak_idxs
def fit_lines(accumulator, thetas, rhos, peak_idxs):
lines = []
for peak_idx in peak_idxs:
rho_idx, theta_idx = peak_idx
rho = rhos[rho_idx]
theta = thetas[theta_idx]
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))
lines.append((x1, y1, x2, y2))
return lines
# Load the image
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
# Apply Canny edge detection
edges = cv2.Canny(img, 50, 150)
# Perform Hough transform
accumulator, thetas, rhos = hough_transform(edges)
# Find the peaks in the accumulator array
peak_idxs = non_max_suppression(accumulator, threshold=100)
# Fit lines to the peaks
lines = fit_lines(accumulator, thetas, rhos, peak_idxs)
# Draw the lines on the original image
for line in lines:
x1, y1, x2, y2 = line
cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Display the result
cv2.imshow('result', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
阅读全文