Project

General

Profile

Anomalie #54 ยป pb_vol3d.py

Beatriz Marcotegui, 01/20/2009 03:28 PM

 
1
#!/usr/bin/python
2
#-*- coding: utf-8 -*-
3

    
4
# To define the path:
5
from morphee import *
6

    
7
import sys, os
8
pjoin = os.path.join
9

    
10
# To use Morphee
11
sys.path.append(pjoin(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),"Common","python"))
12
from morphee import *
13

    
14
def ImDisplay3d(im3d, start,M,N,value,filename):
15
	xsize = im3d.getXSize()
16
	ysize = im3d.getYSize()
17
	zsize = im3d.getZSize()
18
	xsize2 = im3d.getXSize()+2
19
	ysize2 = im3d.getYSize()+2
20
	imd = ImCreate(xsize2*M,ysize2*N,1,"UINT8")
21
	ImSetConstant(imd,value)
22
	type(imd)
23
	time = start
24
	for i in range(N):
25
		for j in range (M):
26
			if(time < zsize):
27
				imd.setActiveWindow(j*xsize2,i*ysize2,0, xsize, ysize, 1)
28
				im3d.setActiveWindow(0,0, time, xsize, ysize, 1)
29
				print ';',type(imd)
30
				ImCopy(im3d,imd)
31
				time = time + 1
32
	imd.resetActiveWindow()
33
	im3d.resetActiveWindow()
34
	ImDisplayX(imd,filename)
35

    
36

    
37
#--- SOLUTIONS
38

    
39
def Exercice2(nb_frames):
40
	"""
41
	
42
  	Segment a 3D grey level image with graphs
43
	
44
	"""
45
	print "II. Segment a 3D grey image"
46
	# Allocate images
47
	im = fileRead(pjoin(images_dir,"3D","brain.vis"))
48
	xsize = im.getXSize()
49
	ysize = im.getYSize()
50

    
51
	im2= ImCreate(xsize,ysize,nb_frames,"UINT8")
52
	im.setActiveWindow(0,0,0,xsize,ysize,nb_frames)
53
	ImCopy(im,im2)
54
	im = ImCreateSame(im2)
55
	ImCopy(im2,im)
56
	
57
	pdb.set_trace()
58
	ImDisplay3d(im,0,6,4,255,"ori")
59
	imFil = getSame(im)
60
	imTemp = getSame(im)
61
	imGrad = getSame(im)
62
	imLabel   = getSameOf(im,dataCategory.dtScalar,scalarDataType.sdtUINT32)
63
	imFineSeg = getSame(imLabel)
64
	imOut = getSame(imLabel)
65
	# Choice of the neighborhood graph
66
	nl = NeighborList.neighborsCubic63D
67
	
68
	# Alternate Sequential Filtering
69
	ImASFCloseOpen(im,nl,1,imFil)
70
	ImDisplay3d(imFil,0,6,4,255,"fil")
71
	
72
	# Gradient
73
	ImMorphoGradient(imFil,nl,imGrad)
74
	ImDisplay3d(imGrad,0,6,5,255,"gra")
75
	
76
	# Minima labelisation
77
	imMini = getSame(imGrad)
78
	ImMinima(imGrad,nl,imMini)
79
	ImLabel(imMini,nl,imLabel)
80
	ImDisplay3d(imLabel,0,6,4,255,"label")
81
    
82
	
83
	# Hierarchical segmentation:
84
	segmType = HierarchicalSegmentationType.Volumic
85
	graph = ImHierarchicalSegmentation(imGrad,imLabel,nl,segmType,imFineSeg)
86
	ImDisplay3d(imFineSeg,0,6,4,255,"fineseg")
87
	
88
	# Choice of a level in the hierarchy
89
	numberOfRegions = 2
90
	ImDisplayHierarchyLevel(imFineSeg,graph,numberOfRegions,imOut)
91
	imSeg = ImCreateSame(imOut)
92
	imSeg8 = ImCreateSame(imOut,"UINT8")
93
	ImLabelFlatZones(imOut,nl,imSeg)
94
	ImCopy(imSeg,imSeg8)
95
	ImDisplay3d(imSeg8,0,6,4,255,"seg")
96
	pdb.set_trace()
97
	
98

    
99

    
100
#--- GLOBAL SCRIPT
101

    
102
if __name__=='__main__':
103
	
104
	print "3D segmentation and tracking"
105
	nb_frames = 10
106
	Exercice2(nb_frames)
107
	
    (1-1/1)