Python3 ValueError:没有足够的值可解包(预期3,得到2)

时间:2019-06-03 14:11:26

标签: python python-3.x

基于加权最小二乘的快速全局图像平滑 本文将全局平滑问题主要转化为加权二次能量函数,使能量函数最小化,以实现图像的平滑度,并将该优化问题转化为线性方程组的求解问题。在求解效率和求解时间上都有明显的优势,这也是一个全局最优的线性系统。

此分配主要用于本文的基本算法。可分离的2d iamge平滑全局平滑器使用python语言进行了复制,为了加快计算速度,请使用mxnet.ndarray在CPU上执行操作。

此任务主要是针对本文的基本算法.2d iamge平滑的可分离全局平滑器使用python语言重现,为了加快计算速度,请使用mxnet.ndarray在GPU上执行操作

我的Python 3程序有问题。我使用的是Window10。此代码运行正常。

import mxnet as mx
from mxnet import nd
import numpy as np
import warnings
from PIL import Image
from matplotlib import pyplot as plt
import warnings
warnings.filterwarnings('ignore')
def cw_1d(p, q, g, sigma):
    norm = nd.norm(g[p] - g[q])
    return nd.exp(-norm/sigma)

def compute_lamb(t, T, lamb_base):
    return 1.5 * 4**(T-t) / (4 ** T - 1) * lamb_base

def compute_1d_fast_global_smoother(lamb, f, g, sigma, ctx):
    w = f.shape[0]
    _c = nd.zeros(shape=w-1, ctx=ctx)
    _c[0] = -lamb * cw_1d(0, 1, g, sigma) / (1 + lamb * cw_1d(0, 1, g, sigma))
    _f = nd.zeros(shape=f.shape, ctx=ctx)
    _f[0] = f[0] / (1 + lamb * cw_1d(0, 1, g, sigma))

    for i in range(1, w-1):
        _c[i] = -lamb * cw_1d(i, i + 1, g, sigma) / (
            1 + lamb * (cw_1d(i, i - 1, g, sigma) + cw_1d(i, i + 1, g, sigma)) +
            lamb * _c[i - 1] * cw_1d(i, i - 1, g, sigma))
        _f[i] = (f[i] + _f[i - 1] * lamb * cw_1d(i, i - 1, g, sigma)) / (
            1 + lamb * (cw_1d(i, i - 1, g, sigma) + cw_1d(i, i + 1, g, sigma)) +
            lamb * _c[i - 1] * cw_1d(i, i - 1, g, sigma))
    _f[w-1] = (f[w-1] + _f[w-2] * lamb * cw_1d(w-1, w-2, g, sigma)) / (
            1 + lamb * (cw_1d(w-1, w-2, g, sigma)) +
            lamb * _c[w-2] * cw_1d(w-1, w-2, g, sigma))
    u = nd.zeros(shape=f.shape, ctx=ctx)
    u[w - 1] = _f[w - 1]

    for i in range(w - 2, -1, -1):
        u[i] = _f[i] - _c[i] * u[i + 1]
    return u.asnumpy()

x1 = np.random.normal(scale=0.2, size=(100))
x2 = np.random.normal(4, 0.2, size=(100))
x = np.concatenate((x1, x2))
plt.plot(np.arange(x.shape[0]), x)

u = compute_1d_fast_global_smoother(900, nd.array(x, ctx=mx.cpu(0)), nd.array(x, mx.cpu(0)), 0.07, mx.cpu(0))
plt.plot(np.arange(u.shape[0]), u)

def Separable_global_smoother(f, T, lamb_base, sigma, ctx):

    print('origin lamb is {}'.format(lamb_base))
    print('sigma is {}'.format(sigma))
    H, W, C = f.shape
    u = f.copy()
    for t in range(1, T+1):
        lamb_t = compute_lamb(t, T, lamb_base)
        for y in range(0, W):
            g = u[:, y, :]
            for c in range(C):
                f_h = u[:, y, c]
                u[:, y, c] = compute_1d_fast_global_smoother(lamb_t, f_h, g, sigma, ctx)
        # vertical
        for x in range(0, H):
            g = u[x, :, :]
            for c in range(C):
                f_v = u[x, :, c]
                u[x, :, c] = compute_1d_fast_global_smoother(lamb_t, f_v, g, sigma, ctx)
    return u

当我尝试为迭代添加价值时,问题就开始了。

origin_img = Image.open('C:/Users/蔡晉易天蔡之蔡總統/Desktop/MSRA10K_Imgs_GT - 複製 (3)/img_ (1).jpg')

plt.figure(figsize=(5, 5))
plt.title('origin image')
plt.imshow(origin_img)

img_100 = origin_img.resize((100, 100))
plt.figure(figsize=(5, 5))
plt.title('50 x 50 image')
plt.imshow(img_100)

lamb_base = 30**2
sigma = 255 * 0.03
ctx = mx.cpu(0)
img = np.array(img_100)
img = nd.array(img, ctx=ctx)

T = 1
u1 = Separable_global_smoother(img, T, lamb_base, sigma, ctx)
show_u = u1.astype('uint8').asnumpy()
plt.figure(figsize=(5, 5))
plt.title('1 iteration smooth result')
plt.imshow(show_u)

终端显示错误:

ValueError                                Traceback (most recent call last)
<ipython-input-12-ff6f57019d54> in <module>
      1 T = 1
----> 2 u1 = Separable_global_smoother(img, T, lamb_base, sigma, ctx)
      3 show_u = u1.astype('uint8').asnumpy()
      4 plt.figure(figsize=(5, 5))
      5 plt.title('1 iteration smooth result')

<ipython-input-7-468a27edeff0> in Separable_global_smoother(f, T, lamb_base, sigma, ctx)
      6     print('origin lamb is {}'.format(lamb_base))
      7     print('sigma is {}'.format(sigma))
----> 8     H, W, C = f.shape
      9     u = f.copy()
     10     for t in range(1, T+1):

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

0 个答案:

没有答案