0

I am working on a project, matching image fragments to larger pieces. For example, matching Fragment to Complete in the below image.Figure_1

Following on from this question, I am trying to draw a lines across two plots to indicate matching points, with each point having different coordinates.

This is the code that I have:

import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch
import numpy as np

(x1,y1) = (292, 21)
(x2,y2) = (930, 1111)

fig = plt.figure()

a1=fig.add_subplot(1,2,1)
imgplot = plt.imshow(fragment) #fragment is the name of the first image
a2=fig.add_subplot(1,2,2)
imgplot = plt.imshow(complete) #complete is the name of the second image

xy1 = (x1,y1)
xy2 = (x2,y2)
con = ConnectionPatch(xyA=xy1, xyB=xy2, coordsA="data", coordsB="data",
                  axesA=a1, axesB=a2, color="red")

a1.add_artist(con)

a1.plot(x1,y1,'ro',markersize=2)
a2.plot(x2,y2,'ro',markersize=2)
plt.show()

However, the resulting image comes out with the line going behind the second image (as per below). What do I need to change in my code?

NB: I realise I could be doing this with OpenCV.

Figure_2

Albort
  • 177
  • 1
  • 11

1 Answers1

2

As seen in this question's answer, you need to provide the second axes as the first argument to the connection patch and add the patch to the second axes, instead of the first one.

con = ConnectionPatch(xyA=xy2, xyB=xy1, coordsA="data", coordsB="data",
                  axesA=a2, axesB=a1, color="red")

a2.add_artist(con)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712