无法在OpenCV中解包图像 [英] Unable to unwrap image in OpenCV

查看:350
本文介绍了无法在OpenCV中解包图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将图像从笛卡尔转换为极性,以便可以解开图像,但出现运行时错误.如果您对外观感到好奇,请参见此示例.

代码:

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)


控制台中的错误消息:

Traceback (most recent call last):
  File "test.py", line 23, in <module>
    result = logpolar(image)
  File "test.py", line 15, in 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='constant')
  File "/Library/Python/2.7/site-packages/scipy-0.13.0.dev_c31f167_20130415-py2.7-macosx-10.8-intel.egg/scipy/ndimage/interpolation.py", line 295, in map_coordinates
    raise RuntimeError('invalid shape for coordinate array')
RuntimeError: invalid shape for coordinate array

解决方案

我的第一个猜测是您要传递的是3维彩色图像.乍一看,我认为您的代码无法解决这个问题.

我的猜测是基于您粘贴的错误,特别是 坐标数组的形状无效" 当使用类似这样的高维数组时,通常必须传递额外的参数,以指定要在其上重复操作的轴,有时甚至不起作用.我没有在参数列表的末尾看到重复的额外整数,因此我发现您不是在试图显式地处理这种情况,并且可能已经忘记在读取图像后检查数组的维数了.

很高兴:)

I am trying to convert an image from cartesian to polar so that I can unravel the image, but I am getting a runtime error. If you are curious how this looks visually, see this example.

Code:

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)


Error message in console:

Traceback (most recent call last):
  File "test.py", line 23, in <module>
    result = logpolar(image)
  File "test.py", line 15, in 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='constant')
  File "/Library/Python/2.7/site-packages/scipy-0.13.0.dev_c31f167_20130415-py2.7-macosx-10.8-intel.egg/scipy/ndimage/interpolation.py", line 295, in map_coordinates
    raise RuntimeError('invalid shape for coordinate array')
RuntimeError: invalid shape for coordinate array

解决方案

My first guess would be that you are passing in a colour image which is 3 dimensional. At first glance I don't think your code could handle that.

My guess was based off of the error you pasted, specifically "invalid shape for coordinate array" When using higher dimensional arrays like that usually you have to pass extra parameters around specifying which axis to repeat the operations over and even then sometimes it does not work. I didn't see a repeated extra integer at the end of your argument lists so I figured you weren't trying to handle that case explicitly and might have forgotten to check your array dimensions after reading in the image.

Glad it helped :)

这篇关于无法在OpenCV中解包图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆