-1

I want to segment the liver from medical images in python. My approach is organizing the images into folders (one folder for every patient ID) and combining every 2D image with its corresponding mask. The resulting image should be written so the same folder.

I already generated the masks. Adding the two images does not give the desired result, though, it should be some kind of 'inverse subtraction' but I don't know how to implement this.

I also do not know how to automate the process so that I don't have to always specify the folder - I want the program to be able to automatically switch between folders.

For example typing image1 = imread("/Downloads/image1"); image1_mask = imread("/Downloads/image1_mask"); result = image1 + image1_mask

requires me to specify the path for every image combination. Is there a good naming strategy to make the process easier? I tried to implement it with a for loop, but it doesn't work.

And as I said, addition does also not produce the desired result (neither does image subtraction)

Thank you in advance! Maria

1 Answers1

1

You asked in fact several questions.

If you want to display only these pixels, whos corresponding pixels of the mask are non-zero, then you need to multiplicate, not add. E.g.

import numpy as np
image = np.random.randint(1,255,(5,5))
mask = np.zeros((5,5))
mask[1:,2:4]=10
newImage = image * (mask!=0)
print(image, mask, newImage,sep='\n')

Automating of passing directories is a completely different problem, there is a lot of similar questions on SO.

Bartłomiej
  • 1,068
  • 1
  • 14
  • 23
  • Thank you! I saw that line 4 ( mask[1:,2:4]=10 ) inserts the number 10 at some points; why this number? If I insert 1 instead, would that give a one-hot-encoding of the fusion image? –  Jan 08 '18 at 19:12
  • You are right, i did it just to point out, that if there was a multi-mask image (i.e. different values for the liver, spleen, right kidney and so on), than you could easly choose the mask. Well, there should be `image * (mask==10)`, but it was just an example. – Bartłomiej Jan 08 '18 at 19:36