# Problems in aligning figures

**URL:** <https://forum.opencv.org/t/problems-in-aligning-figures/13581>\
**Category:** Python\
**Tags:** features2d\
**Created:** [June 18, 2023, 1:01pm UTC](https://forum.opencv.org/t/problems-in-aligning-figures/13581 "2023-06-18T13:01:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![serbring](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@serbring](https://forum.opencv.org/u/serbring)\
**Post date:** [June 18, 2023, 1:01pm UTC](https://forum.opencv.org/t/problems-in-aligning-figures/13581/1 "2023-06-18T13:01:19Z")

</div>

Hi all,

I have tried to follow [this](https://docs.opencv.org/4.x/dc/dc3/tutorial_py_matcher.html) tutorial for aligning two figures but I get a wrong result. In particular, independently of the images I use for feeding the code, I always get a picture like that reported in the figure on the right in the following [image](https://we.tl/t-8B0tCqOFGY):

 ![Figure_1](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/a/a490f5f4e7908112b7a19da6ae89a7d7a4118b81.jpeg)

It seems that the feature matching is not right. Below, you will find the code that I have used. **How can I solve this issue?** Any suggestion is appreciated.  
Thanks.

```auto
MAX_FEATURES = 5000
GOOD_MATCH_PERCENT = 0.15

im1=cv2.imread("xxx1.jpg")
im2=cv2.imread("xxx2.jpg")

# Convert images to grayscale
im1Gray = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY)
im2Gray = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)               

orb = cv2.ORB_create(MAX_FEATURES)
keypoints1, descriptors1 = orb.detectAndCompute(im1Gray, None)
keypoints2, descriptors2 = orb.detectAndCompute(im2Gray, None)       

im1_diplay = cv2.drawKeypoints(im1Gray,keypoints1, outImage=np.array([]), color=(255,0,0), flags=None)
im2_diplay = cv2.drawKeypoints(im2Gray,keypoints2, outImage=np.array([]), color=(255,0,0), flags=None)

plt.figure(figsize=[20,10])
plt.subplot(121); plt.axis('off'); plt.imshow(im1_diplay); plt.title("original form")
plt.subplot(122); plt.axis('off'); plt.imshow(im2_diplay); plt.title("tilted form")
plt.show()

# Match features
matcher =cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
matches = matcher.match(descriptors1,descriptors2,None)
if matches is None:
    print("the matches is none")
    sys.exit()
# sort matches by score
matches=sorted(matches,key=lambda x: x.distance)
print(type(matches))

#Remove not so good matches
numGoodMatches = int(len(matches)*0.2)
matches = matches[:numGoodMatches]

im_matches = cv2.drawMatches(im1,keypoints1, im2, keypoints2, matches, None)

plt.figure(figsize=[40,10])
plt.imshow(im_matches); plt.axis("off"); plt.title("Original form")
plt.show()

points1= np.zeros((len(matches),2),dtype=np.float32)
points2= np.zeros((len(matches),2),dtype=np.float32)

for i, match in enumerate(matches):
    points1[i,:] = keypoints1[match.queryIdx].pt
    points2[i,:] = keypoints2[match.queryIdx].pt

h, mask = cv2.findHomography(points2,points1, cv2.RANSAC)

height, width, channels = im1.shape
im2_reg = cv2.warpPerspective(im2, h, (width, height))

# Display results
plt.figure(figsize=[20,10])
plt.subplot(121); plt.imshow(im1); plt.axis("off"); plt.title("original")
plt.subplot(122); plt.imshow(im2_reg); plt.axis("off"); plt.title("scanned")
plt.show()

```
