cv2 drawMatches draws on blank screen?

imageCorrespondence = cv2.drawMatches(imageLeft, kpLeft, imageRight, kpRight, [goodMatches[0]], None, flags=2)
cv2.imwrite('imageCorrespondence.png', imageCorrespondence)

gives expected output in jupyter notebook, but when I save the file using python script, it is drawing matches and flags=4 is drawing keypoints just fine, except everything is happening on a black image (of right size: left + right combined).

enter image description here

Possible backend selection issues like we have with matplotlib?


The example code works just fine:

import numpy as np
import cv2

def getCorrespondence(imageLeft, imageRight):
    orb = cv2.ORB_create()
    kpLeft, desLeft = orb.detectAndCompute(imageLeft, None)
    kpRight, desRight = orb.detectAndCompute(imageRight, None)

    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
    matches = bf.match(desLeft, desRight)

    goodMatches = []
    for m in matches:
        if m.distance < 100:
            goodMatches.append(m)

    print('Matches', len(goodMatches))

    imageCorrespondence = cv2.drawMatches(imageLeft, kpLeft, imageRight, kpRight, [goodMatches[0]], None, flags=2)
    return imageCorrespondence

if __name__ == '__main__':
    imageLeft = cv2.imread('image_001.png')
    imageRight = cv2.imread('image_002.png')

    imageCorrespondence = getCorrespondence(imageLeft, imageRight)
    cv2.imwrite('imageCorrespondence.png', imageCorrespondence)
    print('Image Saved')

But as soon as I start using the function with other images loaded from elsewhere, it is breaking something. I made sure if those images have content and cv2.imwrite('imageLeft', imageLeft) works just fine and the image gets saved fine.

Upvotes: 2

Views: 1396

Answers (1)

I initially thought the sixth param None was causing this, but that is not causing any trouble.

cv2.drawMatches() takes in imageLeft and imageRight as numpy arrays as mentioned in the docs:

outImg    =   cv.drawMatches( img1, keypoints1, img2, keypoints2, matches1to2, outImg[, matchColor[, singlePointColor[, matchesMask[,flags]]]]    )

Parameters

  • img1 First source image.
  • keypoints1 Keypoints from the first source image.
  • img2 Second source image.
  • keypoints2 Keypoints from the second source image. ...

However something that breaks this is the alpha layer, if you happen to load the alpha layer in the numpy array, it would draw on a black image. When I removed the alpha layer in the numpy array manually and had only three channels, it started working fine. This could be because of the way matplotlib handles alpha layer differs from how cv2.imwrite handles the same, that it seemed to work in Jupyter notebook but not using the Python script.

I initially thought I needed to switch from BGRA to ABGR, but that's not the case BGRA is just fine, I was getting a black screen if the input images had a fourth alpha layer. Opencv usually strips the alpha layer when reading an image...!

Upvotes: 2

Related Questions