0

Okay so I'm manually converting MATLAB to Python and I'm not able to convert certain snippets because I'm not that great at Python

The MATLAB code is as follows:

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);

img is the photo belowenter image description here

The Python equivalent:

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]

The last 2 lines give me an error as follows:

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

How do I go about this?

Nikhil Hegde
  • 341
  • 1
  • 3
  • 15
  • Check the docs for ConvexHull here: http://scipy.github.io/devdocs/generated/scipy.spatial.ConvexHull.html. You need to use one of the attributes of the ConvexHull object (perhaps inds.vertices or inds.simplices) – Markku K. Apr 07 '16 at 21:11
  • Its neither of them. The values that I get in MATLAB and what I get in Python don't match. @MarkkuK. – Nikhil Hegde Apr 07 '16 at 21:26
  • Do you still get the same errors on the last two lines? – Markku K. Apr 07 '16 at 22:44
  • If I use `x = x[inds.vertices]` or `x = x[inds.simplices`, I don't get any compilation error but a logical error. The convexhull result in python is not the same as matlab convhull. @MarkkuK – Nikhil Hegde Apr 08 '16 at 04:00
  • Sorry, I don't know anything about ConvexHull ... but presumably you do, perhaps you could visualize both results, and compare them in some way. – Markku K. Apr 11 '16 at 22:11

0 Answers0