Extracting Homography from Stitcher

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:

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:

Hi,
I wrote a program 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. :smiling_face_with_tear:

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 :slight_smile:

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

Yes you can replace

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

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…

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:

  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 :slight_smile:

Thank you for your help so far!

Regarding size, I just added:

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

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

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 -