2

I have run into an issue with a stitching program I made. The way I am slicing the image makes it so the only way it works is if the first image if to the left and above the one it would be stitched to.

    def stitchMatches(self,image1,image2,homography):
        #gather x and y axis of images that will be stitched
        height1, width1 = image1.shape[0], image1.shape[1]
        height2, width2 = image2.shape[0], image2.shape[1]
        #create blank image that will be large enough to hold stitch image
        blank_image = np.zeros(((width1 + width2),(height1 + height2),3),np.uint8)
        #stitch image two into the resulting image while using blank_image 
        #to create a large enough frame for images
        result = cv2.warpPerspective((image1),homography,blank_image.shape[0:2])
        #numpy notation for slicing a matrix together
        #allows you to see the image
        result[0:image2.shape[0], 0:image2.shape[1]] = image2

This code runs when the left most image is represented by image1.

When I reverse the order of the images however I only have on image received as it the final line in my code "result[0...=image2" is unable to slive an image in an orientation that is not oriented with the first image in the upper left most corner between tho two images being stitched.

Here is a full example with homography

This is the homgraphy between the two images and Their result:

This is the correct result with imaege1 on the left

This is the incorrect result with image1 on the right

I know the issue is with th final slicing line I am just at a loss to get it to work, Any help is appreciated.

C.Radford
  • 882
  • 4
  • 13
  • 31
  • Please fix your indentation, which is definitely broken. Then could you elaborate a bit more, perhaps with some simple examples, creating a [MCVE]? – Andras Deak -- Слава Україні Dec 01 '16 at 23:53
  • Hey Andras Deak. I am very new to posting to stack overflow so thanks for the suggestions. – C.Radford Dec 02 '16 at 00:26
  • If you switch `image1` and `image2`, don't you have to change the homography accordingly? – Andras Deak -- Слава Україні Dec 02 '16 at 00:32
  • Yes, that is done by another function that brings in homography. No matter the orientation of the two images about each other I am able to generate the correct homogony matrix. The only issue is the physical stitching of the two images. My final line of code that does this should have the x and y coordinates be 0 but I can't figure out what to change them to (at least I think that is where the issue is). – C.Radford Dec 02 '16 at 00:40
  • Yeah, sorry, I don't understand your issue. What is the difference between "working" and "non-working" cases? Are the two cases you linked "working" ones? They look like that. What does a "non-working" one look like? Is it possible that the stitching simply fails for some reason? – Andras Deak -- Слава Україні Dec 02 '16 at 00:42
  • I'll throw some more pictures done for you:) Assume that no matter what image I put first homography is found between both images. The issue is stitching them together so they are both seen in the resulting stitched image – C.Radford Dec 02 '16 at 00:55
  • OK, I think I see the problem, thanks:) I suggest that you try plotting the direct `result` of `warpPerspective`, without your last line. If my suspicion is correct, then in a "working" case you'll see a warped image1, and in a "non-working" case you'll see an empty image. If this is correct, then the issue is that the homography transforms your image coordinates outside of the actual image space (i.e. typically to negative pixel indices). Then again I can be completely wrong, so I'd look at the outputs of `warpPerspective` if I were you. – Andras Deak -- Слава Україні Dec 02 '16 at 01:33
  • Thanks for your advice mate I'll give it a go. Also thanks for bearing with me as I explained the problem. – C.Radford Dec 02 '16 at 01:57
  • So I gave it a go and image 1 would always be displayed in the top left corner. I am almost 100% sure that my homography matrix is correct now after doing some other tests. Thanks for your suggesting though really appreciate it. Any help is good help – C.Radford Dec 07 '16 at 20:08
  • Thanks for the feedback. That might still be the same thing. Think about it: if image 1 is always in the top left, then the assignment that overwrites the top left corner with image 2 (this is your last line) will overwrite image 1, and you end up with only image 2. It's possible that negative pixel indices are clipped to 0, and that's why image 1 ends up in the top left corner. I never said your homography matrix is wrong: I just thought that `warpPerspective` might not handle negative shifts properly. Anyway, I don't really know how you could fix it, sorry. – Andras Deak -- Слава Україні Dec 07 '16 at 20:23
  • Ahh I see what you mean. Yes I somehow need to get 0 to equal the leftmost y axis based on the two images and same for the x axis get the north-most between the two images. I can do 0 to whatever distance south I want and 0 to whatever distance east I want but not whatever distance north to south and east to west. I think we are both saying the same thing just differently? Tell me if I am understanding you wrong. Again thanks for your help. – C.Radford Dec 08 '16 at 19:35
  • Yes, that is exactly my suspicion. – Andras Deak -- Слава Україні Dec 08 '16 at 20:03

0 Answers0