# Keypoint detection: drawMatches on multiple images / confusion over DRAW\_OVER\_OUTIMG flag

**URL:** <https://forum.opencv.org/t/keypoint-detection-drawmatches-on-multiple-images-confusion-over-draw-over-outimg-flag/2200>\
**Category:** Python\
**Tags:** features2d\
**Created:** [March 16, 2021, 11:34am UTC](https://forum.opencv.org/t/keypoint-detection-drawmatches-on-multiple-images-confusion-over-draw-over-outimg-flag/2200 "2021-03-16T11:34:42Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![mluerig](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mluerig/32/1436_2.png) [@mluerig](https://forum.opencv.org/u/mluerig)\
**Post date:** [March 16, 2021, 11:34am UTC](https://forum.opencv.org/t/keypoint-detection-drawmatches-on-multiple-images-confusion-over-draw-over-outimg-flag/2200/1 "2021-03-16T11:34:42Z")

</div>

I’m using `opencv-python`, and I am trying to visualize results from AKAZE keypoint matching **for multiple images**. While playing around with `drawMatches`, several questions arose. Below is the code, and my questions:

```
import cv2

template_image cv2.imread("img1.tif")
target_image cv2.imread("img2.tif")

akaze = cv2.AKAZE_create()
kp1, des1 = akaze.detectAndCompute(template_image, None)
kp2, des2 = akaze.detectAndCompute(target_image, None)
matcher = cv2.DescriptorMatcher_create(cv2.DescriptorMatcher_BRUTEFORCE_HAMMING)
matches = matcher.knnMatch(des1, des2, 2)

good = []
for m, n in matches:
    if m.distance < 0.8 * n.distance:
        good.append(m)

results = cv2.drawMatches(template_image,kp1,
                          target_image,kp2,
                          good,None,
                          (0,255,0),
                          flags=1)

```

1. What is the correct use of the `DRAW_OVER_OUTIMG` flag (flag “1”)? I tried the following, but I get an error:  
`results = cv2.drawMatches(template_image, kp1, target_image, kp2, good, target_image, flags=1) `

1. `drawMatches takes` takes only two images, but I would like to visualize keypoint matches between the template and multiple target images - is there any way to do this?

2. Can I change the image-matrix layout in `drawMatches`?

---

<div class="post-metadata">

**Author:** ![Supra](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/supra/32/91_2.png) [@Supra](https://forum.opencv.org/u/Supra)\
**Post date:** [March 16, 2021, 11:57am UTC](https://forum.opencv.org/t/keypoint-detection-drawmatches-on-multiple-images-confusion-over-draw-over-outimg-flag/2200/2 "2021-03-16T11:57:00Z")

</div>

Change this

```auto
results = cv2.drawMatches(template_image, kp1, target_image, kp2, good, target_image, flags=1)

```

to:

```auto
results = cv2.drawMatches(template_image, kp1, target_image, kp2, good, None, flags=1)

```

---

<div class="post-metadata">

**Author:** ![mluerig](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mluerig/32/1436_2.png) [@mluerig](https://forum.opencv.org/u/mluerig)\
**Post date:** [March 16, 2021, 12:11pm UTC](https://forum.opencv.org/t/keypoint-detection-drawmatches-on-multiple-images-confusion-over-draw-over-outimg-flag/2200/3 "2021-03-16T12:11:10Z")

</div>

no effect - I get the same error

---

<div class="post-metadata">

**Author:** ![Supra](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/supra/32/91_2.png) [@Supra](https://forum.opencv.org/u/Supra)\
**Post date:** [March 16, 2021, 12:22pm UTC](https://forum.opencv.org/t/keypoint-detection-drawmatches-on-multiple-images-confusion-over-draw-over-outimg-flag/2200/4 "2021-03-16T12:22:30Z")

</div>

What if you change flags 0 to 4?  
Btw, If you could providing source code. So, everyone can see what wrong.

---

<div class="post-metadata">

**Author:** ![mluerig](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mluerig/32/1436_2.png) [@mluerig](https://forum.opencv.org/u/mluerig)\
**Post date:** [March 16, 2021, 12:43pm UTC](https://forum.opencv.org/t/keypoint-detection-drawmatches-on-multiple-images-confusion-over-draw-over-outimg-flag/2200/5 "2021-03-16T12:43:04Z")

</div>

I added the code in the OP

when I change 0 to 4 I get the rich keypoints output (as per [OpenCV: cv::DrawMatchesFlags Struct Reference](https://docs.opencv.org/3.4.9/de/d30/structcv_1_1DrawMatchesFlags.html#a13278be2b5fb496f99e6cce707008bb6ac22e71af97a4741038f2fc47437371b8))

I guess what I am also asking with Q1 is: what I do I get when I use DRAW\_OVER\_OUTIMG?

I am basically just trying to find better means of visualizing keypoint detection results from one template in multiple target images

---

<div class="post-metadata">

**Author:** ![Supra](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/supra/32/91_2.png) [@Supra](https://forum.opencv.org/u/Supra)\
**Post date:** [March 16, 2021, 12:52pm UTC](https://forum.opencv.org/t/keypoint-detection-drawmatches-on-multiple-images-confusion-over-draw-over-outimg-flag/2200/6 "2021-03-16T12:52:06Z")

</div>

You are missing **cv2.BFMatche** before **knnMatch**. This is what I have in below

```auto
import cv2 
import numpy as np 

img_comp = cv2.imread("image1.jpg")
img_ref = cv2.imread("image2.jpg")

gray_img_comp = cv2.cvtColor(img_comp, cv2.COLOR_BGR2GRAY)
gray_img_ref = cv2.cvtColor(img_ref, cv2.COLOR_BGR2GRAY)

akaze = cv2.AKAZE_create() 

kp1, des1 = akaze.detectAndCompute(gray_img_comp, None) 
kp2, des2 = akaze.detectAndCompute(gray_img_ref, None)

bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=False)

matches = bf.knnMatch(des1, des2, k=2) 

ratio = 0.5 
good = [] 
for m, n in matches: 
    if m.distance < ratio * n.distance: 
        good.append(m) 

img_result = cv2.drawMatches(img_comp,
                             kp1,
                             img_ref,
                             kp2,
                             good,
                             None,
                             flags=2) 

cv2.namedWindow("Result", cv2.WINDOW_NORMAL)
cv2.imshow('Result', img_result) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 

```

Btw, You should change flags=2

---

<div class="post-metadata">

**Author:** ![mluerig](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mluerig/32/1436_2.png) [@mluerig](https://forum.opencv.org/u/mluerig)\
**Post date:** [March 16, 2021, 1:09pm UTC](https://forum.opencv.org/t/keypoint-detection-drawmatches-on-multiple-images-confusion-over-draw-over-outimg-flag/2200/7 "2021-03-16T13:09:47Z")

</div>

thanks - but, sorry, I think you misunderstood me: it was working fine with any of the flags before, **except for flag 1** ( DRAW\_OVER\_OUTIMG: Output image matrix will not be created ([Mat::create](https://docs.opencv.org/3.4/d3/d63/classcv_1_1Mat.html#a55ced2c8d844d683ea9a725c60037ad0)). Matches will be drawn on existing content of output image.)

what does this do?

---

<div class="post-metadata">

**Author:** ![Supra](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/supra/32/91_2.png) [@Supra](https://forum.opencv.org/u/Supra)\
**Post date:** [March 16, 2021, 1:46pm UTC](https://forum.opencv.org/t/keypoint-detection-drawmatches-on-multiple-images-confusion-over-draw-over-outimg-flag/2200/8 "2021-03-16T13:46:36Z")

</div>

You need upgrading 3.4.9 to 4.5.1. The 3.4.9 is an obsoleted. OpenCV 4.5.2 in later than June, 2021. I am using 4.5.1. Sorry, mlurgig.

---

<div class="post-metadata">

**Author:** ![mluerig](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mluerig/32/1436_2.png) [@mluerig](https://forum.opencv.org/u/mluerig)\
**Post date:** [March 16, 2021, 1:50pm UTC](https://forum.opencv.org/t/keypoint-detection-drawmatches-on-multiple-images-confusion-over-draw-over-outimg-flag/2200/9 "2021-03-16T13:50:41Z")

</div>

ok, thanks for your help!

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [March 16, 2021, 2:36pm UTC](https://forum.opencv.org/t/keypoint-detection-drawmatches-on-multiple-images-confusion-over-draw-over-outimg-flag/2200/10 "2021-03-16T14:36:55Z")

</div>

> [@mluerig](#):
>
> DRAW\_OVER\_OUTIMG

is for the case, where you already _have_ a preallocated image, that you want to _reuse_ for drawing. it should have `outimg.rows == a.rows == b.rows` and `outimg.cols = a.cols + b.cols` (so they both fit in horizontally next to each other)

you’d also have to pass this image as argument 6 (where you have `None`)

if you don’t have such an image, pass None as outimg and use the DEFAULT flag, so it will automatically be allocated

and no, you cannot do this with multiple query images at the same time

---

<div class="post-metadata">

**Author:** ![mluerig](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mluerig/32/1436_2.png) [@mluerig](https://forum.opencv.org/u/mluerig)\
**Post date:** [March 16, 2021, 3:09pm UTC](https://forum.opencv.org/t/keypoint-detection-drawmatches-on-multiple-images-confusion-over-draw-over-outimg-flag/2200/11 "2021-03-16T15:09:13Z")

</div>

thanks that clarified it!

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [March 16, 2021, 3:33pm UTC](https://forum.opencv.org/t/keypoint-detection-drawmatches-on-multiple-images-confusion-over-draw-over-outimg-flag/2200/12 "2021-03-16T15:33:13Z")

</div>

it’s also a very good idea to NEVER use magic numbers. use the named constants that exist.

```python
cv2.DrawMatchesFlags_DRAW_OVER_OUTIMG

```

these identifiers aren’t easy to find but it’s possible:

```python
>>> [x for x in dir(cv2) if 'DrawMatchesFlags' in x]
['DrawMatchesFlags_DEFAULT', 'DrawMatchesFlags_DRAW_OVER_OUTIMG', 'DrawMatchesFlags_DRAW_RICH_KEYPOINTS', 'DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS']

```

---

<div class="post-metadata">

**Author:** ![mluerig](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mluerig/32/1436_2.png) [@mluerig](https://forum.opencv.org/u/mluerig)\
**Post date:** [March 16, 2021, 3:35pm UTC](https://forum.opencv.org/t/keypoint-detection-drawmatches-on-multiple-images-confusion-over-draw-over-outimg-flag/2200/13 "2021-03-16T15:35:35Z")

</div>

yupp, I did use them initially, but while playing around and making a minimal example for this post I got lazy 🙂

---

<div class="post-metadata">

**Author:** ![Alejandro\_Silvestri](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/alejandro_silvestri/32/357_2.png) [@Alejandro\_Silvestri](https://forum.opencv.org/u/Alejandro_Silvestri)\
**Post date:** [March 23, 2021, 11:42am UTC](https://forum.opencv.org/t/keypoint-detection-drawmatches-on-multiple-images-confusion-over-draw-over-outimg-flag/2200/14 "2021-03-23T11:42:12Z")

</div>

About 2 and 3:

`drawMatches` is a convenience function: it is easy but not flexible. And because of this often you come out with your own implementation. `drawMatches` takes only two images, and that’s it.

The function can’t change the layout. If you want a vertical layout, you can rotate both input images, rotate matches, and rotate back the output image. Not nice nor clean.

You can change the layout by generating your own output image and manipulating matches coordinates. Then you are one step away from making your own implementation.

Note: The main purpose of `DRAW_OVER_OUTIMG` is to let you draw different matches lines with different colors, like green for inliers and red for outliers. With this flag the function already needs input images to get the output coordinates of the second image on the output one.
