MATLAB到Python转换 - ConvexHull问题

时间:2016-04-07 21:04:05

标签: python matlab image-processing convex-hull

好的,所以我手动将MATLAB转换为Python,我无法转换某些片段,因为我在Python上不是那么棒

MATLAB代码如下:

theta = [0, 90, 180];
var1 = [0 ,90, 180];
varargin = [];
spacing = [1, 1];
calib = 0;
origin = [1, 1];
labels = [];
nTheta = 3;
labels = 1;
nLabels = 1;
fd = [0, 0, 0];
i = 1;

[y,x] = find(img==1);
inds = convhull(x,y);
x = x(inds);
y = y(inds);

imgenter image description here

下面的照片

Python等价物:

import numpy as np
import math
from scipy.spatial import ConvexHull
from PIL import Image

img = Image.open("csm.jpg")
img = np.array(img)

theta = [0, 90, 180]
var1 = [0 ,90, 180]
varargin = []
spacing = [1, 1]
calib = 0
origin = [1, 1]
labels = []
nTheta = 3
labels = 1
nLabels = 1
fd = [0, 0, 0]
i = 1
pi = 3.142

inds = ConvexHull(np.transpose(np.nonzero(img)))

indices = np.transpose(np.where(img==1))
y = np.zeros(len(indices))
x = np.zeros(len(indices))
for i in range(0,len(indices)) : 
    y[i] = indices[i][0]
    x[i] = indices[i][1]

x = x[inds]
y = y[inds]

最后两行给出了如下错误:

Traceback (most recent call last):
  File "check2.py", line 32, in <module>
    x = x[inds]
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

我该怎么做?

0 个答案:

没有答案