繁体   English   中英

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

[英]Why am i getting not enough values to unpack (expected 2, got 1) for some images?

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:没有足够的值可解包(预期2,得到1)

图片:

主要

主要

裁剪

裁剪

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

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

听起来您没有得到任何匹配。 尝试确保您有关键点。 也许您可以检查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])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM