1

So i'm trying to make a function that segmens out parts of the blood vessels of an eyeball as a part of my math course, however i get the same result no matter what threshold i put in, so i assume i must have done something very wrong in the following code:

def threshold_image(retina, threshold):
  tImage = retina
  for pixel in tImage.shape:
    if pixel <=threshold:
      pixel=1
    else:
      pixel=0
  return tImage

img2 = threshold_image(img, 3)
io.imshow(img2, cmap=cm.Greys_r)
plt.show()

My plan was, to make a function that goes through every pixel of the picture, and then apply the threshold, and if it was above the threshold, the pixel would become 0(black), and if it was within it would be 1(white). However, im currently just getting the same picture i gave it as a result

Shai
  • 111,146
  • 38
  • 238
  • 371
Levicia
  • 33
  • 6

1 Answers1

1

You are iterating on tImage.shape: your pixel get only two values h and w of the image. You are not iterating over the pixels themselves.

Try:

for pixel in tImage:
  pixel = 0 if pixel > thr else 1

While you are at it, what's wrong with simply

tImage = img < threshold
Shai
  • 111,146
  • 38
  • 238
  • 371