Python cv2抽油机是否在空白屏幕上抽油?

Python cv2抽油机是否在空白屏幕上抽油?,python,opencv,Python,Opencv,在jupyter notebook中提供了预期的输出,但是当我使用python脚本保存文件时,它正在绘制匹配项,flags=4正在绘制关键点,除了所有事情都发生在黑色图像上(大小正确:左+右组合) 可能的后端选择问题,如我们在matplotlib中遇到的问题 示例代码运行良好: imageCorrespondence = cv2.drawMatches(imageLeft, kpLeft, imageRight, kpRight, [goodMatches[0]], None, flags=

在jupyter notebook中提供了预期的输出,但是当我使用python脚本保存文件时,它正在绘制匹配项,
flags=4
正在绘制关键点,除了所有事情都发生在黑色图像上(大小正确:左+右组合)

可能的后端选择问题,如我们在
matplotlib
中遇到的问题


示例代码运行良好:

imageCorrespondence = cv2.drawMatches(imageLeft, kpLeft, imageRight, kpRight, [goodMatches[0]], None, flags=2)
cv2.imwrite('imageCorrespondence.png', imageCorrespondence)
将numpy导入为np
进口cv2
def GetRecompondence(图像左、图像右):
orb=cv2.orb_create()
kpLeft,desLeft=orb.detectAndCompute(imageLeft,无)
kpRight,desRight=orb.detectAndCompute(imageRight,无)
bf=cv2.BFMatcher(cv2.NORM\u HAMMING,交叉检查=True)
匹配=bf.match(减左,减右)
goodMatches=[]
对于匹配中的m:
如果m.距离<100:
goodMatches.append(m)
打印('Matches',len(goodMatches))
ImageAccordinance=cv2.drawMatches(imageLeft、kpLeft、imageRight、kpRight、[goodMatches[0]],无,标志=2)
回信
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
imageLeft=cv2.imread('image_001.png'))
imageRight=cv2.imread('image_002.png'))
ImageRecompondence=GetRecompondence(imageLeft,imageRight)
cv2.imwrite('imagecorrectivement.png',imagecorrectivement)
打印('已保存图像')

但是,当我开始对从别处加载的其他图像使用该函数时,它就破坏了某些东西。我确定这些图像是否有内容,并且
cv2.imwrite('imageLeft',imageLeft)
工作正常,图像保存正常。

我最初认为第六个参数
导致了这种情况,但这并没有造成任何问题

cv2.drawMatches()
将imageLeft和imageRight作为numpy数组,如中所述:

参数

  • img1
    第一个源图像
  • 关键点1
    来自第一个源图像的关键点
  • img2
    第二个源图像
  • 关键点2
    来自第二个源图像的关键点
然而,破坏这一点的是alpha层,如果您碰巧在numpy数组中加载alpha层,它将绘制一个黑色图像。当我手动移除numpy阵列中的alpha层时,它只有三个通道,开始工作正常。这可能是因为
matplotlib
处理alpha层的方式与
cv2.imwrite
处理alpha层的方式不同,它似乎在Jupyter笔记本中工作,但不使用Python脚本


我最初认为我需要从BGRA切换到ABGR,但事实并非如此,BGRA很好,如果输入图像有第四个alpha层,我会得到一个黑屏Opencv通常在读取图像时剥离alpha层

提供一个。不必提醒一个拥有数千声誉的5年用户这一点……对不起,我认为我在最初发布的两行中遗漏了一些东西(特别是因为我将输出图像参数设置为“无”),我认为这对opencv用户来说是显而易见的,因此没有添加完整的代码。现在又加了一句:)抓得好。查看源代码,然后。随机颜色生成将alpha通道保留为默认值0,因此它绘制的任何内容最终都是透明的。我将提交一份bug报告,这是一个微不足道的修复。还有一个:|上一个问题的修复已经合并,应该可以在3.4.5和4.x的下一个版本中使用。我想这就是你遇到的。
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')
outImg    =   cv.drawMatches( img1, keypoints1, img2, keypoints2, matches1to2, outImg[, matchColor[, singlePointColor[, matchesMask[,flags]]]]    )