【问题标题】:ValueError: too many values to unpack (expected 2) while applying cross_correlation_shiftsValueError:应用 cross_correlation_shifts 时解包的值太多(预期为 2)
【发布时间】:2020-12-07 18:53:15
【问题描述】:

我对计算机视觉非常陌生,想了解图像配准。我想应用 cross_correlation_shifts 但我不断收到错误消息。 我在jupyter notebook中写的代码如下:

from skimage import io
import image_registration
from image_registration import cross_correlation_shifts
image = io.imread("Images/Mug.jpg")
offset_image = io.imread("Images/rotated_Mug.jpg")
xoff, yoff = image_registration.cross_correlation_shifts(image, offset_image)
print("Offset image ")
print("Pixels shifted by: ", xoff, yoff)

运行与cross_correlation_shifts相关的代码,我得到这个错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-46-0d64c5cb8855> in <module>
----> 1 xoff, yoff = image_registration.cross_correlation_shifts(image, offset_image)
      2 
      3 print("Offset image ")
      4 print("Pixels shifted by: ", xoff, yoff)

/opt/anaconda3/lib/python3.7/site-packages/image_registration/cross_correlation_shifts.py in cross_correlation_shifts(image1, image2, errim1, errim2, maxoff, verbose, gaussfit, return_error, zeromean, **kwargs)
     87         raise ValueError("Cross-correlation image must have same shape as input images.  This can only be violated if you pass a strange kwarg to correlate2d.")
     88 
---> 89     ylen,xlen = image1.shape
     90     xcen = xlen/2-(1-xlen%2)
     91     ycen = ylen/2-(1-ylen%2)

ValueError: too many values to unpack (expected 2)

我使用的图像是同一个杯子。第一个 Mug.jpg 是正常的直线图像,旋转后的图像是同一个 Mug 的图像,但它向右倾斜。

谁能告诉我这里有什么问题?

【问题讨论】:

  • 你能重新格式化错误输出吗?它不是超级可读的。但我认为问题在于图像必须具有相同的形状“互相关图像必须与输入图像具有相同的形状。只有将一个奇怪的 kwarg 传递给 correlate2d 才能违反这一点。”
  • 我更新了格式,希望它更清晰。两个图像的形状和大小属性是相同的,我只是检查以确保。
  • 您的图像看起来是 2D RGB,所以 image1.shape 是(比如说)(512, 512, 3),即 ylen, xlen, num_channels。您需要以某种方式将它们转换为灰度图像。
  • @Juan 这是一个很好的提示,谢谢。请把它作为一个答案,以便我可以标记它。有了这个,我不再遇到执行错误,但我仍然没有从图像注册中得到我正在寻找的答案,因为图像似乎没有朝正确的方向旋转。因此,我将进一步调查并可能提出另一个问题。

标签: python computer-vision scikit-image image-registration


【解决方案1】:

您的图像看起来是 2D RGB,因此 image1.shape 是(例如)(512, 512, 3),即 ylen, xlen, num_channels。您需要以某种方式将它们转换为灰度,例如使用skimage.color.rgb2gray(但请注意,这会将您的图像重新缩放为浮动在 [0, 1] 中)。

【讨论】:

    猜你喜欢
    • 2017-08-29
    • 1970-01-01
    • 2014-07-31
    • 2017-12-14
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多