# Extracting Homography from Stitcher

**URL:** <https://forum.opencv.org/t/extracting-homography-from-stitcher/11393>\
**Category:** Python\
**Tags:** stitching\
**Created:** [December 22, 2022, 12:19pm UTC](https://forum.opencv.org/t/extracting-homography-from-stitcher/11393 "2022-12-22T12:19:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AleksCST](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@AleksCST](https://forum.opencv.org/u/AleksCST)\
**Post date:** [December 22, 2022, 12:19pm UTC](https://forum.opencv.org/t/extracting-homography-from-stitcher/11393/1 "2022-12-22T12:19:53Z")

</div>

Hi, I’m currently stitching two images (captured from live video) using cv2.Stitcher\_create().

The output looks decent - I’m now wondering if I could somehow extract the homography matrix and reuse it for perspective warping since running the stitcher on every frame is too slow.

Here’s what I have for image stitching so far:

```auto
import cv2
import time

left = cv2.imread('left.png', 1)
right = cv2.imread('right.png', 1)
images = [left, right]
input_images = cv2.hconcat([left, right])

imStitcher = cv2.Stitcher_create()

t1 = time.time()

error, stitched_img = imStitcher.stitch(images)

if not error:
    cv2.imshow('stitched', stitched_img)
    cv2.imwrite('stitched_img.png', stitched_img)
    cv2.imshow('input images', input_images)
    t2 = time.time()
    print('process time: ' + str(t2-t1))
    cv2.waitKey(0)

else:
    print('Stitching not successful')

```

I’m only allowed one link, so here are the input images:

 ![input imags](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/b/b27441fa7d56e44c48a248824dda6fcfed444118.jpeg)

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [December 22, 2022, 2:22pm UTC](https://forum.opencv.org/t/extracting-homography-from-stitcher/11393/2 "2022-12-22T14:22:05Z")

</div>

Hi,  
I wrote a [program](https://github.com/D-BookeR/Traitement_d-images_et_de_videos_avec_OpenCV4_en_Python/blob/master/py_panoramique/panorama.py) to stitch all images from webcam in one.  
When it’s done you have got a wide angle camera (and no need to sticth again)  
Sorry it’s in french. 🥲

---

<div class="post-metadata">

**Author:** ![AleksCST](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@AleksCST](https://forum.opencv.org/u/AleksCST)\
**Post date:** [December 23, 2022, 9:00am UTC](https://forum.opencv.org/t/extracting-homography-from-stitcher/11393/3 "2022-12-23T09:00:29Z")

</div>

Hi Laurent, thanks for your reply. I already found your code from another post and gave it a try. I need to spent some more time on it - or perhaps you’re able to help me out.

Your script is giving me the following error message:

Aucune camera trouvee

Or in English: No cameras found 🙂

I think I narrowed it down to line 106:  
webcam = cv.VideoCapture(i + api)

Normally, I’d simply but an integer as parameter but I am not using a webcam - I’m using a camera with FPD link. I guess I should also mention I’m trying this out on a Linux system running Ubuntu 18.04.

My ‘VideoCapture’ looks like this:

cv2.VideoCapture(“v4l2src device=/dev/video0 ! video/x-raw, format=BGRx, width=2064, height=1544 ! videoconvert ! video/x-raw, format=BGR ! appsink”)

Since it’s a v4l2 source, maybe I need something other than “cv.CAP\_DSHOW”.

Best regards  
Aleks

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [December 23, 2022, 9:34am UTC](https://forum.opencv.org/t/extracting-homography-from-stitcher/11393/4 "2022-12-23T09:34:03Z")

</div>

Yes you can replace

```auto
def recherche_camera(api=CAMAPI):
    liste_webcam_ouvertes = []
    for i in range(NBCAMERA):
        webcam = cv.VideoCapture(i + api)
        if webcam.isOpened():
            liste_webcam_ouvertes.append((webcam, i))
    return liste_webcam_ouvertes

```

with

```auto
def recherche_camera(api=CAMAPI):
    liste_webcam_ouvertes = []
    webcam = cv.VideoCapture(“v4l2src device=/dev/video0 ! video/x-raw, format=BGRx, width=2064, height=1544 ! videoconvert ! video/x-raw, format=BGR ! appsink”)
    liste_webcam_ouvertes.append((webcam,0 ))
    webcam = cv.VideoCapture(“v4l2src device=/dev/video1 ! video/x-raw, format=BGRx, width=2064, height=1544 ! videoconvert ! video/x-raw, format=BGR ! appsink”)
    liste_webcam_ouvertes.append((webcam,1 ))
return liste_webcam_ouvertes

```

Try it and may be it will be good…

---

<div class="post-metadata">

**Author:** ![AleksCST](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@AleksCST](https://forum.opencv.org/u/AleksCST)\
**Post date:** [December 23, 2022, 10:21am UTC](https://forum.opencv.org/t/extracting-homography-from-stitcher/11393/5 "2022-12-23T10:21:43Z")

</div>

Ok, some progress:

The cameras are showing in their respective windows but I think the stitching failed somehow:

Appariement : Best  
Transformation : homography  
Couture : no  
Surface : plane  
Exposition : 2  
Ajustement : ray  
Appariement : Best  
Transformation : homography  
Couture : no  
Surface : stereographic  
Exposition : 2  
Ajustement : ray  
Echec de l’appariement

I guess it comes from this line:

```auto
  if nb_images < 2:
      print("Echec de l'appariement")
      return False, pano

```

Also, is there a resize option? Getting the full image size makes it pretty laggy and not easy to get an overview of the output video 🙂

Thank you for your help so far!

---

<div class="post-metadata">

**Author:** ![AleksCST](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@AleksCST](https://forum.opencv.org/u/AleksCST)\
**Post date:** [December 23, 2022, 10:31am UTC](https://forum.opencv.org/t/extracting-homography-from-stitcher/11393/6 "2022-12-23T10:31:18Z")

</div>

Regarding size, I just added:

```auto
image_tmp = cv.resize(image_tmp, (960, 640))

```

right above:  
` cv.imshow("Webcam" + str(idx), image_tmp)`

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [December 23, 2022, 12:34pm UTC](https://forum.opencv.org/t/extracting-homography-from-stitcher/11393/7 "2022-12-23T12:34:53Z")

</div>

About Echec de l’appariement (maching not successful) You have to change constrast or brightness of your camera. You can decrease confiance threshold using m key  
Try again andIt will work

About zoom you can use key + or -
