0

I want to stitch together a bunch of images with OpenCV2 with python. Since it is very time consuming to stitch together 30+ images I modified my code to just stitch together 2 images and after that stitch together the next image with the previous stitched image.

But when running the code even at the first stitch it will run into an error with the status code 1 and I dont know why. I even tested it with only 2 images in the folder but with the same result so it seems like I made an error somewhere in the code.

Full code:

from imutils import paths
import numpy as np
import imutils
import cv2


print("[INFO] loading images...")
imagePaths = sorted(list(paths.list_images("./images/")))
images = []


for imagePath in imagePaths:
    image = cv2.imread(imagePath)
    images.append(image)
i = 0
currentImage = None
while len(images) > i + 1: #loop through all images. Use i+1 because we used i+1 to get the next image down below. If we get to the last image there is no next image
    imagesToCombine = []
    if i == 0:
        imagesToCombine.append(images[i]) #Use first image at the first run
    else:
        imagesToCombine.append(currentImage) #Use the last stiched image
    imagesToCombine.append(images[i + 1])

    print("[INFO] stitching images...")
    stitcher = cv2.createStitcher() if imutils.is_cv3() else cv2.Stitcher_create()
    (status, stitched) = stitcher.stitch(imagesToCombine)

    if status == 0:
            currentImage = stitched
            print("Done" + str(i))
            i += 1
    else:
            print("[INFO] image stitching failed ({})".format(status))
            break
if currentImage is not None:
    cv2.imwrite("./stiched.png", currentImage)
print("Done")

Console output:

[INFO] loading images...
[INFO] stitching images...
[INFO] image stitching failed (1)
Done

Edit:

After I wanted to create another set of example images I saw that with those images it was working! So somehow the images I actually want to combine doesn't work somehow even when they have a good portion of overlap.

Those images doesn't work: https://i.stack.imgur.com/pIjrs.jpg

But those images do work: https://i.stack.imgur.com/0zG9U.jpg

Any Idea why the other images don't work?

sirzento
  • 537
  • 1
  • 5
  • 23
  • welcome. [mre] should include the data required to reproduce the issue. you are also expected to have debugged your code. please step through it with a debugger. and consider simplifying the code. you have a weird while loop there and an `is not None` handling that could still grab a `None` value in the alternative case. – Christoph Rackwitz May 08 '23 at 11:22
  • "you are also expected to have debugged your code. please step through it with a debugger." - How would you know that I did use a debugger or not? – sirzento May 08 '23 at 11:27
  • so you claim that `imagesToCombine`, at the time of the failure, contains two numpy arrays that have a sensible size and values in them? -- don't forget to supply the input data that is required to reproduce the issue. – Christoph Rackwitz May 08 '23 at 11:40
  • As far I have seen it last time the list did contain two arrays but I will double check if I can get back to the code later. I will also add two example images then for reproduce purposes. I edit the code to make the while a bit more clear – sirzento May 08 '23 at 12:21
  • 1
    best make it three example pictures, since that will cover both branches of the `if i == 0` in the loop. – Christoph Rackwitz May 08 '23 at 13:12
  • @ChristophRackwitz I updated the question with example images. Strangly the other set of images do work but the result image is quite blurry but for the first part I want it to work with the other pictures – sirzento May 08 '23 at 16:04
  • 1
    the non-working set contains ambiguities. repeating features with minor differences. that will trip the stitching up. -- since you appear to wanna stitch pieces of a scrolled page together, use a different approach. stitching assumes homography or something comparably free. you only have one degree of freedom, translation along the page. further, pixels in overlapping regions match _exactly_, not approximately, so anything involving feature matching is overkill. just use correlation/convolution. – Christoph Rackwitz May 08 '23 at 21:56
  • @ChristophRackwitz Oh okey. To add to that, I did notice when using three images at a time, the first~6 images of my set do work but after that the image gets blurry and also there will be no matches after that. Do you have any recommendation on how to stitch them together with correlation/convolution you suggested? It's like everything I try to find about that will just result in the same approache as thise code. – sirzento May 09 '23 at 06:13
  • the image gets blurry because you're making a copy of a copy of a copy (warping/resampling). this is not how stitching works. why do you not simply give ALL pictures to the stitcher at once? everyone seems to be doing the same thing you're doing, or worse. I have no idea why. is there some bad youtube tutorial out there that you follow? – Christoph Rackwitz May 09 '23 at 10:33
  • @ChristophRackwitz No, I have just googled how to stich images together with python that have an overlapp and most of the results use the same methode. When I give all the images to the stitcher at once it will never finish. I tryed it with 30 pictures of the same size as the examples and after 20min I just killed it. What other option you think I should try? Do you have an module or option for opencv that doesn't invole feature matching since the pixel are matching perfectly? – sirzento May 09 '23 at 11:10
  • 1
    I believe I already recommended an approach: correlation/convolution to estimate the shift, which will be the location of the maximum (peak) in the result. then you can compose manually, using that shift value. – Christoph Rackwitz May 09 '23 at 11:45
  • Yeah I have no clue how that could work. I don't even know half of the code words you use. Any hint on how to google something like that where I could learn it? – sirzento May 09 '23 at 11:59
  • 1
    look for `cv.matchTemplate()` with `TM_SQDIFF`. best match will have lowest score (difference). there are a couple of writeups about that. official docs probably have something. it's about a handful of lines of code. https://docs.opencv.org/4.x/de/da9/tutorial_template_matching.html (very verbose article) – Christoph Rackwitz May 09 '23 at 14:24
  • You need to use the scan mode of the stitcher (https://docs.opencv.org/4.x/d2/d8d/classcv_1_1Stitcher.html). You can also check out https://github.com/OpenStitching/stitching in the affine mode – Lukas Weber May 15 '23 at 05:33

0 Answers0