gpt4 book ai didi

python - 为什么我没有足够的值来解压某些图像(预期 2,得到 1)?

转载 作者:太空宇宙 更新时间:2023-11-03 23:07:08 25 4
gpt4 key购买 nike

import cv2
import numpy as np
import sys
from IPython.display import Image
from matplotlib import pyplot as plt
import numpy
import os


img = cv2.imread('C:\\Users\\not-cropped.jpg')
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

search_params = dict(checks = 50)

FLANN_INDEX_LSH = 6
index_params= dict(algorithm = FLANN_INDEX_LSH,
table_number =12 , # 12
key_size = 20, # 20
multi_probe_level = 2) #2



# Initiate STAR detector
star = cv2.xfeatures2d.StarDetector_create()
# Initiate BRIEF extractor
brief = cv2.xfeatures2d.BriefDescriptorExtractor_create()

# find the keypoints with STAR
kp_brief_o = star.detect(gray,None)

# compute the descriptors with BRIEF
kp_brief_o, des_brief_o = brief.compute(gray, kp_brief_o)


img66 = cv2.drawKeypoints(gray,kp_brief_o,None,(255,0,0),4)
plt.imshow(img66),plt.show()
##########################################################33
gray_crop = cv2.imread('C:\\Users\\cropped.jpg', 0)

kp_brief_crop = star.detect(gray_crop,None)

kp_brief_crop, des_brief_crop = brief.compute(gray_crop, kp_brief_crop)

flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des_brief_o, des_brief_crop, k=2)

good = []
for m,n in matches:
if m.distance < 0.7*n.distance:
good.append(m)

我想将一个图像与另一个使用星号作为描述符并简要检测描述符的图像进行比较,它在某些图像上按预期工作,但在其他图像上,如附件 2,它会抛出异常,尽管第二个是首先,当图像完全不同时,它甚至会抛出相同的异常。

错误:

ValueError Traceback (most recent call last)
<ipython-input-56-9685f4afabba> in <module>
44
45 good = []
---> 46 for m,n in matches:
47 if m.distance < 0.7*n.distance:
48 good.append(m)

ValueError: not enough values to unpack (expected 2, got 1)

图片:

主要

main

裁剪

cropped

不相关的图片我仍然会得到错误

unrelated pic where i'd still get the error

最佳答案

听起来您没有得到任何匹配项。尽量确保你有关键点。也许您可以检查 len(des_brief_o)len(des_brief_crop) 返回的内容。我假设 matches 在这种情况下是 None

所以我运行了你的代码,我遇到了同样的问题:

for m in matches:
print(m)

有些匹配项只包含一个元素或不包含任何元素。

...
[<DMatch 0x10e6b93f0>, <DMatch 0x10e6b9410>]
[<DMatch 0x10e6b9430>, <DMatch 0x10e6b9450>]
[<DMatch 0x10e6b9470>, <DMatch 0x10e6b9490>]
[<DMatch 0x10e6b94b0>, <DMatch 0x10e6b94d0>]
[]
[<DMatch 0x10e6b94f0>, <DMatch 0x10e6b9510>]
[<DMatch 0x10e6b9530>, <DMatch 0x10e6b9550>]
[<DMatch 0x10e6b9570>, <DMatch 0x10e6b9590>]
[]
[<DMatch 0x10e6b95b0>, <DMatch 0x10e6b95d0>]
[<DMatch 0x10e6b95f0>]
[<DMatch 0x10e6b9610>, <DMatch 0x10e6b9630>]
...

为什么不跳过没有或只有一个匹配的结果:

good = []
for m in matches:
if len(m) == 2:
if m[0].distance < 0.7 * m[1].distance:
good.append(m[0])

关于python - 为什么我没有足够的值来解压某些图像(预期 2,得到 1)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56780421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com