IndexError:在对我的数据运行广义hough变换时,为什么?

时间:2018-02-27 23:11:56

标签: python python-2.7 numpy image-processing hough-transform

我正在尝试将available generalized hough transform (GHT)应用于我自己的数据。程序在提供的示例数据上运行得非常好,但是,对于我的数据,一旦达到this line,它就会出错: 我的数据已保存到main function中的两个numpy数组中:

f = h5py.File(img_path,'r')  # reading the reference image
refim = f['image'].value
refim = np.asarray(refim)
refim[refim!=1]=0 
#im = imread('Input1.png')
f = h5py.File(im_path,'r')  # reading the image that should be matched
im = f['image'].value
im = np.asarray(im)

参考图像和测试图像的大小256x256相同,参考图像中的对象中心为[ 83.02902047 127.19376853]。名称为table的变量是形状为(90,)的列表,其中每个列表元素的形状为(144,2)元组,例如,一个元素包括[-102.97097952712484, 12.193768525539397]

/home/user/anaconda2/envs/testcaffe/lib/python2.7/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Traceback (most recent call last):
  File "generalized-hough-demo.py", line 43, in <module>
    acc = matchTable(im, table)
  File "/home/user/workspace/jupyter_codes/PythonSIFT/Genarlized_Hough_Voting/generalised-hough-transform/match_table.py", line 38, in matchTable
    acc[vector[0]+x, vector[1]+y]+=1
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

我挣扎了两天,非常感谢你的专家意见。

1 个答案:

答案 0 :(得分:-1)

此错误消息指出索引应为if(aTerm == null || aTerm.isZero()) { return;// this is not a valid term } int insertIndex = -1; int i = 0; boolean addToList = true; // loop until you've found your place, make sure to test for end conditions // so you don't hit an IndexOutOfBounds error or create an infinite loop while(insertIndex == -1 && i < theAL.size() && addToList) { Term thisTerm = theAL.get(i); if(aTerm.degree() == thisTerm.degree()) { thisTerm.addIn(aTerm); addToList = false; } else if(aTerm.degree() > thisTerm.degree()) { insertIndex = i;// this will end the loop, we know where to sort // this element in our array } i++;// increment our counter } if(addToList) { // make sure we found a valid location, if so, add in our term if(insertIndex >= 0) { theAL.add(insertIndex, aTerm); } else { theAL.add(aTerm); // insert the term at the end, it's less than all // other terms in the list, or the list is empty } } 。如您所见,only integerstable的列表,每个vectors都是2d-vector,如您在问题中所述。

因此,vectorvector[0]主要是 float ,矩阵的索引必须是整数。这就是为什么它将错误指向 43 行。