Repository: opencv
Issue: video stream stiching ,error : stiching failed status :3
Labels: bug

### System Information

OpenCV version: 4.8.0
Operating System / Platform: window 11
Python version:3.11.5

### Detailed description

import cv2
import numpy as np

def find_homography(img1, img2, ransac_threshold=5.0):
    orb = cv2.ORB_create(nfeatures=1000)

    kp1, des1 = orb.detectAndCompute(img1, None)
    kp2, des2 = orb.detectAndCompute(img2, None)

    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
    matches = bf.match(des1, des2)

    matches = sorted(matches, key=lambda x: x.distance)

    src_pts = np.float32([kp1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2)
    dst_pts = np.float32([kp2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2)

    homography, _ = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, ransac_threshold)

    return homography

def stitch_frames(stitcher, cap):
    # Initialize the homography matrix with the identity matrix
    homography = np.eye(3)

    # Read the first frame from the camera to initialize the homography matrix
    ret, frame_prev = cap.read()
    if not ret:
        print("Error: Could not read frame from the camera.")
        cap.release()
        cv2.destroyAllWindows()
        exit()

    # Resize the first frame if needed
    frame_prev = cv2.resize(frame_prev, (640, 480))

    cv2.namedWindow("Stitched Result", cv2.WINDOW_NORMAL)

    while True:
        # Capture frames from the camera
        ret, frame = cap.read()
        if not ret:
            break

        # Resize the frame if needed
        frame = cv2.resize(frame, (640, 480))

        # Update the homography matrix based on the current and previous frames
        homography = find_homography(frame_prev, frame)

        # Manually set the homography (replace with your own matrix)
        # For example, use the identity matrix:
        homography = np.eye(3)

        # Use the updated homography matrix for stitching
        status, result = stitcher.stitch([frame_prev, frame], homography)
        if status == cv2.Stitcher_OK:
            cv2.imshow("Stitched Result", result)
        else:
            print(f"Stitching failed with status: {status}")

        # Set the current frame as the previous frame for the next iteration
        frame_prev = frame.copy()

        if cv2.waitKey(1) == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    # Create a Stitcher object
    stitcher = cv2.Stitcher_create()

    # Create a VideoCapture object for the camera (use 0 for the default camera)
    cap = cv2.VideoCapture(0)

    # Check if the camera opened successfully
    if not cap.isOpened():
        print("Error: Could not open camera.")
        exit()

    # Stitch frames using the Stitcher class and homography matrix
    stitch_frames(stitcher, cap)


### Steps to reproduce

"""if the environment changes significantly between frames, it can affect the stitching process. Ensure that the scene remains relatively constant during the capture."""

### Issue submission checklist

- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [X] I updated to the latest OpenCV version and the issue is still there
- [ ] There is reproducer code and related data files (videos, images, onnx, etc)