Home > database >  how can i find intersection points of these axlines?
how can i find intersection points of these axlines?

Time:08-12

I want to extract the intersection point of these axlines, how can that be achieved ?

I want to extract the points and use them for homography matrix.

#######################
# Load and Show image #
#######################

import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
import cv2 as cv
from skimage import io
link = "https://i.imgur.com/WKiQZgj.jpeg"
a = io.imread(link)
a = cv.cvtColor(a, cv.COLOR_RGB2GRAY)
figure(figsize=(15,10), dpi = 75)
plt.imshow(a,cmap = "gray")

##############
# Plot Lines #
##############

#red
plt.axline([361, 39], [1051, 15], color = "red")

#blue
plt.axline([1048, 75], [1158, 115], color = "blue")

#green 
plt.axline([361, 229], [1051, 160], color = "green")

#yellow 
plt.axline([123, 75], [151, 115], color = "yellow")

output_image

CodePudding user response:

There's too much to write for me to give you a working code, but I'll point out a resource and provide further suggestions, in case you are open to taking them.

  1. How to detect where two line segments intersect. To get you started on the math, here's what the p and s variables would be for the "yellow" line.
p = np.array([[123, 151]])
s = p - np.array([[75,115]])
  1. There's no need for using OpenCV and Scikit-Image. You can remove the OpenCV lines and replace them with this.
from skimage import color
a = color.rgb2gray(a)
  1. Scikit-Image can also help with the homography. See the ProjectiveTransform class. You'll want to use the estimate instance method therein.

CodePudding user response:

Thanks to @Juancheeto i found the solution, leaving the code as reference

#######################
# Load and Show Image #
#######################

import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
import cv2 as cv
from scipy.stats import linregress
from skimage import io

link = "https://i.imgur.com/WKiQZgj.jpeg"
a = io.imread(link)
a = cv.cvtColor(a, cv.COLOR_RGB2GRAY)
figure(figsize=(15,10), dpi = 75)
plt.imshow(a,cmap = "gray")


# Plotting line
#green 
plt.axline([361, 229], [1051, 160], color = "green",zorder = 1)

#yellow 
plt.axline([123, 75], [151, 115], color = "yellow", zorder = 2)

########################################################
# Finding intersection point For Green And Yellow Line #
########################################################

############################## 
# Find slopes and intercepts #
##############################


# Line 1 [Yellow]
x_yellow = [123, 75] 
y_yellow = [151, 115]

slope_yellow , intercept_yellow, r_value, p_value, std_err = linregress(x_yellow, y_yellow)

# Line 2 [Green]
x_green = [361, 229]
y_green = [1051, 160]

slope_green , intercept_green, r_value, p_value, std_err = linregress(x_green, y_green)


###########################
# Find X intersect point  #
###########################

x = (intercept_green - intercept_yellow) / (slope_yellow - slope_green)


###########################
# Find Y intersect point  #                        
###########################

y = ( slope_yellow * x )   intercept_yellow

########################################
# Plotting intersection point #
########################################


# Intersection point
plt.scatter(x,y, marker = "x", color = "black", s = 75, zorder = 3)

  • Related