【问题标题】:Unable to unwrap image in OpenCV无法在 OpenCV 中解开图像
【发布时间】:2013-05-15 06:43:51
【问题描述】:

我正在尝试将图像从笛卡尔转换为极坐标,以便解开图像,但出现运行时错误。如果您对它的视觉效果感到好奇,请参阅 this example.

代码:

import scipy
import scipy.ndimage
import numpy as np
from math import *
import cv2

def logpolar(input):
    # This takes a numpy array and returns it in Log-Polar coordinates.

    coordinates = np.mgrid[0:max(input.shape[:])*2,0:360] # We create a cartesian array which will be used to compute log-polar coordinates.
    log_r = 10**(coordinates[0,:]/(input.shape[0]*2.)*log10(input.shape[1])) # This contains a normalized logarithmic gradient
    angle = 2.*pi*(coordinates[1,:]/360.) # This is a linear gradient going from 0 to 2*Pi

    # Using scipy's map_coordinates(), we map the input array on the log-polar coordinate. Do not forget to center the coordinates!
    lpinput = scipy.ndimage.interpolation.map_coordinates(input,(log_r*np.cos(angle)+input.shape[0]/2.,log_r*np.sin(angle)+input.shape[1]/2.),order=3,mode='constant')

    # Returning log-normal...
    return lpinput


# Load image
image = cv2.imread("test.jpg")
result = logpolar(image)


控制台中的错误消息:

回溯(最近一次通话最后): 中的文件“test.py”,第 23 行 结果 = 对数极坐标(图像) 文件“test.py”,第 15 行,在 logpolar lpinput = scipy.ndimage.interpolation.map_coordinates(input,(log_r*np.cos(angle)+input.shape[0]/2.,log_r*np.sin(angle)+input.shape[1]/2. ),order=3,mode='常数') 文件“/Library/Python/2.7/site-packages/scipy-0.13.0.dev_c31f167_20130415-py2.7-macosx-10.8-intel.egg/scipy/ndimage/interpolation.py”,第 295 行,位于 map_coordinates raise RuntimeError('坐标数组的形状无效') RuntimeError:坐标数组的形状无效

【问题讨论】:

  • 我的第一个猜测是您传入的是 3 维彩色图像。乍一看,我认为您的代码无法处理。
  • 嘿锤子你介意让你的评论成为答案吗?另外,解释一下你是如何得出你的猜测的?如果您想知道,您是对的,因为它是彩色图像 =)
  • 当然,很高兴它有帮助:)

标签: python opencv python-2.7 numpy scipy


【解决方案1】:

我的第一个猜测是您传入的是 3 维彩色图像。乍一看,我认为您的代码无法处理。

我的猜测是基于您粘贴的错误,特别是 “坐标数组的形状无效” 当使用像这样的高维数组时,通常您必须传递额外的参数来指定要重复操作的轴,即使这样有时它也不起作用。我没有在您的参数列表末尾看到重复的额外整数,所以我认为您没有尝试明确处理这种情况,并且可能在读取图像后忘记检查您的数组尺寸。

很高兴它有帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    相关资源
    最近更新 更多