0

I tried to make stitching with tif pictures but it doesn't work and it does not come from the code:


import cv2
image_paths=['LFMstar_6.png','LFMstar_7.png']
# initialized a list of images
imgs = []
  
for i in range(len(image_paths)):
    imgs.append(cv2.imread(image_paths[i]))
    #imgs[i]=cv2.resize(imgs[i],(0,0),fx=0.4,fy=0.4)
    # this is optional if your input images isn't too large
    # you don't need to scale down the image
    # in my case the input images are of dimensions 3000x1200
    # and due to this the resultant image won't fit the screen
    # scaling down the images 
# showing the original pictures
cv2.imshow('1',imgs[0])
cv2.imshow('2',imgs[1])

stitchy=cv2.Stitcher.create()
(dummy,output)=stitchy.stitch(imgs)
  
if dummy != cv2.STITCHER_OK:
  # checking if the stitching procedure is successful
  # .stitch() function returns a true value if stitching is 
  # done successfully
    print("stitching ain't successful")
else: 
    print('Your Panorama is ready!!!')

I tried with jpg images and it works perfectly but not with mine...Maybe the overlap between my pictures doesn't work ? My tif pictures are 140x140 pixels currently. I converted the pictures in RGB and jpg but it doesn't change anything. Here an example of my image.

Thank you

enter image description here

enter image description here

  • so... what is your question ? also, it seems you have forgotten to include the images. – Hoodlum Jul 27 '23 at 16:22
  • I edit my post thank you, so my question is why my image doesn't work ? – Rosario night Jul 27 '23 at 19:13
  • unfortunately you still have not provided enough information. can you tell us what is failing in your process? are you receiving an incorrectly stitched image, or nothing at all? in that case, what is the error code you're getting (what you've called `dummy` is really an [exit status with a code](https://stackoverflow.com/a/36645276/21688300)). also, you've only provided one image (i'm guessing you have more?). – Hoodlum Jul 27 '23 at 20:04
  • Thank you for the answer, I will give you two imagse that I want to you stitch. Indeed I cannot stitch images at all and I know it comes from my image because I can do with other one. They are .tif but I converted in RGB and JPG but it also doesn't work.... I tried with 2 "classic" JPG images and it works perfectly but not with mine. – Rosario night Jul 27 '23 at 21:04

1 Answers1

0

To answer your question: Your images do not overlap enough or do not contain enough "keypoints" for OpenCV to be able to figure out how they fit together.

By modifying your code a little you can find out (a little bit) more about why the process is failing. Specifically, you should use the return code provided by the stitch function (called dummy in the code you are using.
Checking return values is generally a good practice. OpenCV does not usually raise Python Exceptions, so this is the only way of knowing when something goes wrong.

import cv2
err_dict = {
    cv2.STITCHER_OK: "STITCHER_OK",
    cv2.STITCHER_ERR_NEED_MORE_IMGS: "STITCHER_ERR_NEED_MORE_IMGS",
    cv2.STITCHER_ERR_HOMOGRAPHY_EST_FAIL: "STITCHER_ERR_HOMOGRAPHY_EST_FAIL",
    cv2.STITCHER_ERR_CAMERA_PARAMS_ADJUST_FAIL: "STITCHER_ERR_CAMERA_PARAMS_ADJUST_FAIL",
}

image_paths = ['LFMstar_6.jpg', 'LFMstar_7.jpg']

imgs = [cv2.imread(p) for p in image_paths]

stitchy = cv2.Stitcher.create()
(retval, output) = stitchy.stitch(imgs)

if retval != cv2.STITCHER_OK:
    print(f"stitching falied with error code {retval} : {err_dict[retval]}")
    exit(retval)
else:
    print('Your Panorama is ready!!!')
    success = cv2.imwrite("result.jpg", output)
    if not success:
        print("but could not be saved to `result.jpg`")

Using the images you have provided, the error code 1, for STITCHER_ERR_NEED_MORE_IMGS.
For some reason, OpenCV is unable to find enough un-ambiguous common features ('keypoints') between your images such that it can stitch them together. This could be due to the small size of the images, or the relatively linear design, or the flat colors, it's really hard to know without access to the internal image processing pipeline of the stitcher.

Since your images do have some overlap, and since they do not seem to have any distortion or offset between them, it should be relatively simple to join them nonetheless using a much simpler method, (e.g. autocorrelation). The fact that the images are so small also means that the computational expense of such methods will be somewhat reasonable.

Hoodlum
  • 950
  • 2
  • 13
  • Hi, thank you for answer. It's weird because I segmented my image with an overlap, I tested with imageJ the pluging stitching and I have an overlap of 20 % between my images approximately so I'm surprised. Moreover, the problem that is I snap different iamges to stitch them but all images are oriented with some degree each that's why I tried with this method. – Rosario night Jul 27 '23 at 22:32
  • My previous answer was a little hasty - I've played around a little with what you've provided and done some googling. I've updated the answer to reflect the information I was able to find. – Hoodlum Jul 28 '23 at 09:47