mjirik / imcut

3D graph cut segmentation
http://mjirik.github.io/imcut/
BSD 3-Clause "New" or "Revised" License
89 stars 31 forks source link

Multi-Label Graphcut #41

Open ReubenDo opened 4 years ago

ReubenDo commented 4 years ago

Hello,

Congrats for your work and thank you for releasing the code! Does you code support multi-classes? Is there an easy way to extend it?

Regards, Reuben

NeuZhangQiang commented 4 years ago

how to apply the imcut on multi-label segmentation?

mjirik commented 4 years ago

Hello, the multi-label graph-cut is not supported now. It would require to do some changes in the code and we are not planning to do it soon. Sorry. I would appreciate it if you would like to try it.

Best regards, Miroslav

NeuZhangQiang commented 4 years ago

@mjirik I am interesting in this graph cut project. I want to try to extend multi-label graph-cut. But how can I start? Could you please give me some guideline? In addition, when I try the demo code:

import numpy as np
import imcut.pycut as pspc
import matplotlib.pyplot as plt
import time
s = 30
# create data
data = np.random.rand(s, s, s)
data[10:20, 5:15, 3:13] += 1
data = data * 30
data = data.astype(np.int16)

# Make seeds
seeds = np.zeros([s, s, s])
seeds[13:17, 7:10, 5:11] = 1
seeds[0:5:, 0:10, 0:11] = 2

# Run
t1 = time.time()
igc = pspc.ImageGraphCut(data, voxelsize=[1, 1, 1])
igc.set_seeds(seeds)
igc.run()
t2 = time.time()
print('graph time: ', t2-t1)

# Show results
colormap = plt.cm.get_cmap('brg')
colormap._init()
colormap._lut[:1:, 3] = 0

plt.imshow(data[:, :, 10], cmap='gray')
plt.contour(igc.segmentation[:, :, 10], levels=[0.5])
plt.imshow(igc.seeds[:, :, 10], cmap=colormap, interpolation='none')
plt.show()

I find the calculation time is 0.04 (s=30). But, if i set s to 300, the calculation time is 194s. Is it possible to speed it up? For example, use numba module to speed it up?

mjirik commented 4 years ago

The Max-Flow/Min-Cut algorithm is implemented in C. I guess there is not much space for optimization. You can check the time required by this part by:

igc.stats["gc time"]

Some time is consumed by intensity model preparation in function igc.fit_model(). In some cases, you can fit your model only once and skip this part in the following runs.

Most time for optimization would be in n-link and t-link matrix preparation (pycut.ImageGraphCut.__create_nlinks() and pycut.ImageGraphCut.__create_tlinks())

You can try to speed it up by using the multi-scale version of the algorithm (example) which is useful when the segmented object is small. We described it in the paper: Jirik, Miroslav, et al. "Multiscale Graph-Cut for 3D Segmentation of Compact Objects." International Workshop on Combinatorial Image Analysis. Springer, Cham, 2018.

If you would like to start with multi-label, you should try the original single-scale version (functions names starts with ssgc). The best start would be to understand the function __single_scale_gc_run()

pycut.py -> ImageGraphCut.run()