如何提取matplotlib axlines交点以用于单应性矩阵?
解决方案:提取axlines交点并计算单应性矩阵
问题
我需要提取matplotlib中绘制的这些axlines的交点,将其用于单应性矩阵(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")
实现步骤
1. 编写直线交点计算函数
每条axline由两个点定义,我们可以通过直线的一般式 (Ax + By + C = 0) 求解两条直线的交点。以下是通用计算函数:
def line_intersection(line1, line2): # line1、line2格式为 [(x1,y1), (x2,y2)] x1, y1 = line1[0] x2, y2 = line1[1] x3, y3 = line2[0] x4, y4 = line2[1] # 计算直线一般式系数 A1 = y2 - y1 B1 = x1 - x2 C1 = x2*y1 - x1*y2 A2 = y4 - y3 B2 = x3 - x4 C2 = x4*y3 - x3*y4 # 解线性方程组,判断是否有唯一交点 det = A1 * B2 - A2 * B1 if det == 0: return None # 直线平行/重合,无唯一交点 x = (B1*C2 - B2*C1) / det y = (A2*C1 - A1*C2) / det return (x, y)
2. 提取直线点并计算交点
将四条直线的点对存储后,计算所需的四个交点(对应图像中待变换区域的四个角):
# 存储每条直线的两点 red_line = [(361, 39), (1051, 15)] blue_line = [(1048, 75), (1158, 115)] green_line = [(361, 229), (1051, 160)] yellow_line = [(123, 75), (151, 115)] # 计算四个角的交点 top_left = line_intersection(red_line, yellow_line) top_right = line_intersection(red_line, blue_line) bottom_left = line_intersection(green_line, yellow_line) bottom_right = line_intersection(green_line, blue_line) # 输出交点坐标 print("左上交点:", top_left) print("右上交点:", top_right) print("左下交点:", bottom_left) print("右下交点:", bottom_right)
3. 计算单应性矩阵
使用OpenCV的findHomography函数,传入图像中的源交点和目标矩形的顶点,即可得到单应性矩阵:
import numpy as np # 源点:图像中的四个交点,转为float32格式 src_pts = np.array([top_left, top_right, bottom_left, bottom_right], dtype=np.float32) # 目标点:自定义的矩形顶点,可根据需求调整尺寸 dst_pts = np.array([[0, 0], [500, 0], [0, 300], [500, 300]], dtype=np.float32) # 计算单应性矩阵 homography_matrix, _ = cv.findHomography(src_pts, dst_pts) print("单应性矩阵:\n", homography_matrix)
4. 透视变换验证(可选)
可以用得到的单应性矩阵对原图进行透视变换,验证效果:
# 执行透视变换 height, width = a.shape warped_img = cv.warpPerspective(a, homography_matrix, (500, 300)) # 显示变换后的图像 plt.figure(figsize=(8,6)) plt.imshow(warped_img, cmap="gray") plt.show()
完整整合代码
import matplotlib.pyplot as plt from matplotlib.pyplot import figure import cv2 as cv from skimage import io import numpy as np def line_intersection(line1, line2): x1, y1 = line1[0] x2, y2 = line1[1] x3, y3 = line2[0] x4, y4 = line2[1] A1 = y2 - y1 B1 = x1 - x2 C1 = x2*y1 - x1*y2 A2 = y4 - y3 B2 = x3 - x4 C2 = x4*y3 - x3*y4 det = A1 * B2 - A2 * B1 if det == 0: return None x = (B1*C2 - B2*C1) / det y = (A2*C1 - A1*C2) / det return (x, y) # 加载图像 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") red_line = [(361, 39), (1051, 15)] blue_line = [(1048, 75), (1158, 115)] green_line = [(361, 229), (1051, 160)] yellow_line = [(123, 75), (151, 115)] plt.axline(*red_line, color = "red") plt.axline(*blue_line, color = "blue") plt.axline(*green_line, color = "green") plt.axline(*yellow_line, color = "yellow") # 计算并标记交点 top_left = line_intersection(red_line, yellow_line) top_right = line_intersection(red_line, blue_line) bottom_left = line_intersection(green_line, yellow_line) bottom_right = line_intersection(green_line, blue_line) plt.scatter(*top_left, color='black', s=50) plt.scatter(*top_right, color='black', s=50) plt.scatter(*bottom_left, color='black', s=50) plt.scatter(*bottom_right, color='black', s=50) plt.show() # 计算单应性矩阵 src_pts = np.array([top_left, top_right, bottom_left, bottom_right], dtype=np.float32) dst_pts = np.array([[0, 0], [500, 0], [0, 300], [500, 300]], dtype=np.float32) homography_matrix, _ = cv.findHomography(src_pts, dst_pts) print("单应性矩阵:\n", homography_matrix) # 透视变换验证 height, width = a.shape warped_img = cv.warpPerspective(a, homography_matrix, (500, 300)) plt.figure(figsize=(8,6)) plt.imshow(warped_img, cmap="gray") plt.show()
内容的提问来源于stack exchange,提问作者J33T
相关产品推荐
相关产品推荐

