How to detect line using radon transformation using python?
Asked Answered
T

1

1

MATLAB solutions are available elsewhere on the internet, but there is need for the open-source python based solution.

Starter code to create a blank image with a white line.

import cv2
import numpy as np
from skimage.transform import radon
from matplotlib import pyplot as plt

blank = np.zeros((100,100))
blank = cv2.line(blank, (25,25), (75,75), (255, 255, 255), thickness=1)
plt.imshow(blank, cmap='gray')

enter image description here

Trichocyst answered 17/4, 2023 at 22:47 Comment(0)
T
0

Line detection is easy with python's scikit-image using radon transformation

The sinogram, comprises a set of 1-D projections at various angles, each row of the sinogram contains one projection. The brightest parts of the sinogram correspond to the white area in the original image. The brighter the area in sinogram, the greater the collinearity of the objects in the original image. We assume that the coordinates of the sinogram’s maximum value relate to a straight line. Let's use radon transformation and plot the sinogram.

sinogram = radon(blank) 
plt.title("Radon transform\n(Sinogram)")
plt.xlabel("Projection angle (deg)")
plt.ylabel("Projection position (pixels)")
plt.imshow(sinogram, cmap='gray')
plt.show()

enter image description here

We can see the brightest spot at 45 degrees (horizontal axis) and centered around row pixel=50 (vertical axis)

Find the RMS value of each row and find brightest rotation, where the transform is lined up perfectly with the line

r = np.array([np.sqrt(np.mean(np.abs(line) ** 2)) for line in sinogram.transpose()])

angle = np.argmax(r)
print('Angle: {:.1f} degrees'.format(90 - angle))

It outputs

Angle: 45.0 degrees

Similarly you can find pixel value on vertical axis

Trichocyst answered 17/4, 2023 at 22:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.