[1042] Stitch multiple images based on overlay areas in Python
To stitch multiple images into one big image based on their overlay areas in Python, you can use the OpenCV library. Here’s a step-by-step guide:
-
Install Required Libraries:
pip install opencv-python numpy -
Stitch Images:
import cv2 import numpy as np # Load the images img1 = cv2.imread('image1.jpg') img2 = cv2.imread('image2.jpg') # Create a stitcher object stitcher = cv2.Stitcher_create() # Perform the stitching process (status, stitched) = stitcher.stitch([img1, img2]) if status == cv2.Stitcher_OK: print("Stitching successful!") cv2.imwrite('stitched_image.jpg', stitched) else: print("Stitching failed:", status)
In this example:
cv2.imreadloads the images.cv2.Stitcher_createcreates a stitcher object.stitcher.stitchperforms the stitching process.
This code will stitch the images together based on their overlapping areas. You can add more images to the list passed to the stitch method if needed.
For more complex stitching tasks, you might need to detect features and match them manually. Here’s a more detailed approach:
- Detect Features and Match Them:
import cv2 import numpy as np # Load the images img1 = cv2.imread('image1.jpg') img2 = cv2.imread('image2.jpg') # Convert images to grayscale gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) # Detect ORB features and compute descriptors orb = cv2.ORB_create() keypoints1, descriptors1 = orb.detectAndCompute(gray1, None) keypoints2, descriptors2 = orb.detectAndCompute(gray2, None) # Match features bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) matches = bf.match(descriptors1, descriptors2) matches = sorted(matches, key=lambda x: x.distance) # Draw matches (optional) img_matches = cv2.drawMatches(img1, keypoints1, img2, keypoints2, matches[:10], None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS) cv2.imwrite('matches.jpg', img_matches) # Extract location of good matches 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.trainIdx].pt # Find homography h, mask = cv2.findHomography(points2, points1, cv2.RANSAC) # Use homography to warp images height, width, channels = img1.shape img2_warped = cv2.warpPerspective(img2, h, (width, height)) # Combine images stitched = np.maximum(img1, img2_warped) cv2.imwrite('stitched_image.jpg', stitched)
This approach involves:
- Detecting features using ORB.
- Matching features between images.
- Finding the homography matrix.
- Warping one image to align with the other.
- Combining the images.
This method provides more control and can handle more complex stitching scenarios12.
Are you working on a specific project that requires image stitching? Let me know if you need more detailed steps or have any questions!
浙公网安备 33010602011771号