Как я могу заблокировать неправильные строки в шумном пространстве - PullRequest
0 голосов
/ 26 марта 2020

Я нашел реализацию строки в github. И я попробую этот код моего компьютера. Когда я строю большие пространства с помощью matplot, на рисунке показано наращивание. Это вызывает обнаружение неправильных линий в изображении.

def hough_line(img, angle_step=1, lines_are_white=True, value_threshold=5):

# Rho and Theta ranges
thetas = np.deg2rad(np.arange(-90.0, 90.0, angle_step))
width, height = img.shape
diag_len = int(round(math.sqrt(width * width + height * height)))
rhos = np.linspace(-diag_len, diag_len, diag_len * 2)

# Cache some resuable values
cos_t = np.cos(thetas)
sin_t = np.sin(thetas)
num_thetas = len(thetas)

# Hough accumulator array of theta vs rho
accumulator = np.zeros((2 * diag_len, num_thetas), dtype=np.uint8)
# (row, col) indexes to edges
are_edges = img > value_threshold if lines_are_white else img < value_threshold
y_idxs, x_idxs = np.nonzero(are_edges)

# Vote in the hough accumulator
for i in range(len(x_idxs)):
    x = x_idxs[i]
    y = y_idxs[i]

    for t_idx in range(num_thetas):
        # Calculate rho. diag_len is added for a positive index
        rho = diag_len + int(round(x * cos_t[t_idx] + y * sin_t[t_idx]))
        accumulator[rho, t_idx] += 1

return accumulator, thetas, rhos


def show_hough_line(img, accumulator, thetas, rhos, save_path=None):
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 2, figsize=(10, 10))

ax[0].imshow(img, cmap=plt.cm.gray)
ax[0].set_title('Input image')
ax[0].axis('image')

ax[1].imshow(
    accumulator, cmap='jet',
    extent=[np.rad2deg(thetas[-1]), np.rad2deg(thetas[0]), rhos[-1], rhos[0]])
ax[1].set_aspect('equal', adjustable='box')
ax[1].set_title('Hough transform')
ax[1].set_xlabel('Angles (degrees)')
ax[1].set_ylabel('Distance (pixels)')
ax[1].axis('image')

# plt.axis('off')
if save_path is not None:
    plt.savefig(save_path, bbox_inches='tight')
plt.show()

И я запускаю этот код,

img = cv2.imread('05102009081.png')
img_dark = cv2.imread('05102009081-1.png')
img1 = cv2.bitwise_and(img, img_dark)
gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
edges = cv2.Canny(blur, 100, 200)

accumulator, thetas, rhos = hough_line(edges)

#Thresholding with 100
a = (accumulator > 100).astype(int)
accumulator = accumulator * a

show_hough_line(edges, accumulator, thetas, rhos)

Это результат без порога

Это результат после порогового значения

Как вы видите, когда я применяю пороговое значение для этих краев, существует некоторая пиковая точка приблизительно 55. градусов и между 10-50. пикселей в жестком пространстве, это приводит к неправильным линиям на изображении. В чем проблема ? Как я могу решить эту проблему ? Заранее спасибо.

...