Reputation: 31
I am stitching together four pictures. Here is my code:
import cv2
stitcher = cv2.Stitcher.create()
img1 = cv2.imread("Stitching/Images/Newspapers/Newspaper1.jpg")
img2 = cv2.imread("Stitching/Images/Newspapers/Newspaper2.jpg")
img3 = cv2.imread("Stitching/Images/Newspapers/Newspaper3.jpg")
img4 = cv2.imread("Stitching/Images/Newspapers/Newspaper4.jpg")
result = stitcher.stitch((img1,img2,img3,img4))
cv2.imshow("camera",result[1])
cv2.waitKey(0)
--- My very first picture is not showing up. What would cause all three of my other pictures to stitch but not one of them? I am using Python and OpenCV Stitcher. Help!
This is the newspaper with the top cut off. It is blurry since I had to resize to fit on here.
Upvotes: 1
Views: 1749
Reputation: 721
Ok I got my last shot. Apparently the Feature Detector used for the newspaper example was –features surf
. If opencv_contrib is not available (which it isn't for opencv-python), surf is not available, and ORB is used by default. Somehow ORB does not work well with the newspaper example. Changing to SIFT solves this issue and lead to the following matches graph
I used the stitching package with pip install stitching
and then
stitch newspaper* --affine --detector sift --matches_graph_dot_file matches.txt
Upvotes: 2
Reputation: 721
I think one problem could be an inappropriate high confidence theshold within opencvs default stitcher.
You could try the stitching package (which uses the opencv stitching module) and set the confidence threshold to 0 (since you know that all images are part of the panorama)
so do pip install stitching
and
from stitching import Stitcher
imgs = ["Stitching/Images/Newspapers/Newspaper1.jpg",
"Stitching/Images/Newspapers/Newspaper2.jpg",
"Stitching/Images/Newspapers/Newspaper3.jpg",
"Stitching/Images/Newspapers/Newspaper4.jpg"]
stitcher = Stitcher(matcher_type="affine",
estimator="affine",
adjuster="affine",
wave_correct_kind="no",
warper_type="affine",
compensator="no"
# these are the values for cv2.STITCHER_SCANS +
confidence_threshold=0)
panorama = stitcher.stitch(imgs)
Upvotes: 1
Reputation: 6298
Update: I can reproduce this issue with the newspaper sample images from the tutorial. Even with cv2.Stitcher.create(mode=cv2.STITCHER_SCANS)
as suggested in the tutorial the first image is omitted. My current opencv
version is opencv-contrib-python 4.5.5.64.
It seems someone else has already reported this as a bug: https://github.com/opencv/opencv/issues/21010.
If I take four photos of a normal landscape the stitcher
does a good job. I also tried it with a photo of a newspaper and cut it into four overlapping images (upper left, upper right, lower left, lower right, approx. 1700 x 2400 px each) and everything was stitched together.
My code (which is essentially identical to yours):
import cv2
img1 = cv2.imread("newspaper1.jpg")
img2 = cv2.imread("newspaper2.jpg")
img3 = cv2.imread("newspaper3.jpg")
img4 = cv2.imread("newspaper4.jpg")
stitcher = cv2.Stitcher.create(mode=cv2.Stitcher_SCANS)
status, result = stitcher.stitch([img1, img2, img3, img4])
if status == cv2.Stitcher_OK:
cv2.imshow("stitch", result)
cv2.waitKey(0)
cv2.imwrite("out.jpg", result)
else:
print(f"Error code {status}")
Upvotes: 1