好的,所以我手动将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);
下面的照片
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
我该怎么做?