如何从 Opencv lines 函数正确加入折线

How to rightly join broken line from Opencv lines function

我正在尝试使用 opencv lines 函数复制我在 示例图像 上的内容。我已经能够在目标图像上画线了,但是线断了,

我想对齐虚线,以便它们可以显示为示例图像,请问我该如何实现。

下面是我用虚线输出的图像。

示例图片

输出图像

我的示例代码。

results = hr['matchedPoints1'].values.tolist()
results2 = hr2['matchedPoints2'].values.tolist()

for i in zip(results, results2):
    color = tuple(np.random.randint(0,255,3).tolist())
    (x1, y1), (x2, y2) = i
    print( (x1, y1), (x2, y2))
    cv2.line(img1, (x1,y1), (x2,y2),(255,255,255), 2)

    
img1 = cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)

plt.subplot(121),plt.imshow(img1)
plt.show()

从下面的两个匹配点中提取的线

(89, 368) (108, 343)
(39, 384) (57, 354)
(109, 532) (54, 492)
(685, 320) (711, 348)
(625, 292) (647, 308)
(625, 306) (646, 331)
(176, 466) (139, 442)
(156, 350) (118, 330)
(269, 318) (232, 308)
(269, 301) (229, 293)
(632, 209) (659, 225)
(587, 138) (617, 147)
(578, 80) (609, 86)
(571, 128) (601, 135)
(388, 155) (390, 155)
(370, 88) (371, 90)
(364, 159) (366, 158)
(211, 189) (199, 181)
(193, 240) (198, 234)
(194, 264) (199, 253)
(103, 292) (106, 271)
(135, 331) (112, 308)

目标图片

下面是我的 cameraMatrices 和 MatchedPoints。

匹配点 1

89.6214953271028,368.64953271028
39.3785046728971,384.200934579439
109.957943925234,532.53738317757
685.359813084112,320.799065420561
625.546728971963,292.088785046729
625.546728971963,306.443925233645
176.948598130841,466.742990654206
156.61214953271,350.705607476636
269.060747663551,318.406542056075
269.060747663551,301.658878504673
632.724299065421,209.546728971963
587.266355140187,138.967289719626
578.892523364486,80.3504672897196
571.714953271028,128.200934579439
388.686915887851,155.714953271028
370.742990654206,88.7242990654206
364.761682242991,159.303738317757
211.640186915888,189.210280373832
193.696261682243,240.64953271028
194.892523364486,264.57476635514
103.976635514019,292.088785046729
135.079439252336,331.565420560748

匹配点 2

108.593312597201,343.442457231726
57.2340590979786,354.192068429238
54.8452566096425,492.742612752722
711.765940902022,348.220062208398
647.268273716952,308.804821150855
646.073872472784,331.498444790047
139.647744945568,442.577760497667
118.148522550545,330.304043545879
232.811041990669,308.804821150855
229.227838258165,293.277604976672
659.212286158632,225.196734059098
617.408242612753,147.56065318818
609.047433903577,86.6461897356141
601.88102643857,135.616640746501
390.47200622084,155.921461897356
371.361586314153,90.229393468118
366.583981337481,158.310264385692
199.367807153966,181.003888024883
198.173405909798,234.751944012442
199.367807153966,253.862363919129
106.204510108865,271.778382581648
112.176516329705,308.804821150855

每组点对于相应的图像都是唯一的。

我已经展示了如何使用 MatchedPoints1:

获取第一张图像的可视化效果
MatchedPoints1 = [(89.6214953271028,368.64953271028),
(39.3785046728971,384.200934579439),
(109.957943925234,532.53738317757),
(685.359813084112,320.799065420561),
(625.546728971963,292.088785046729),
(625.546728971963,306.443925233645),
(176.948598130841,466.742990654206),
(156.61214953271,350.705607476636),
(269.060747663551,318.406542056075),
(269.060747663551,301.658878504673),
(632.724299065421,209.546728971963),
(587.266355140187,138.967289719626),
(578.892523364486,80.3504672897196),
(571.714953271028,128.200934579439),
(388.686915887851,155.714953271028),
(370.742990654206,88.7242990654206),
(364.761682242991,159.303738317757),
(211.640186915888,189.210280373832),
(193.696261682243,240.64953271028),
(194.892523364486,264.57476635514),
(103.976635514019,292.088785046729),
(135.079439252336,331.565420560748)]


img = cv2.imread('house.png')
img2 = img.copy()

for e, i in enumerate(MatchedPoints1):
  color = tuple(np.random.randint(0,255,3).tolist())
  if (e < (len(MatchedPoints1) - 1)):
    img2 = cv2.line(img2, (int(i[0]), int(i[1])), (int(MatchedPoints1[e+1][0]), int(MatchedPoints1[e+1][1])), color, 2)
    img2 = cv2.circle(img2, (int(i[0]), int(i[1])), 5, color, -1)

结果:

The same can be done for the second image using MatchedPoints2.