Population
Image processing library in C++
3d visualization

In Image processing, a visual checking is essential to prototype and calibrate your process. Population library gives you only some basic tools to visualize a 3d matrix. For more extended tools, use dedicated library for this purpose, as VTK/ITK or amira.

In this following code, I give many methods to visualization a 3d matrix :

#include"Population.h"//Single header
using namespace pop;//Population namespace
void visu2DSlice(){
Mat3UI8 img;
img.load(POP_PROJECT_SOURCE_DIR+std::string("/image/rock3d.pgm"));
img.display();//use the arrows to move in z-axis
}
void visu3DCube(){
Scene3d scene;
Mat3UI8 img;
img.load(POP_PROJECT_SOURCE_DIR+std::string("/image/rock3d.pgm"));
Visualization::cubeExtruded(scene,img);//add the cube surfaces to the scene
Visualization::lineCube(scene,img);//add the border red lines to the scene to the scene
scene.display(false);//display the scene
waitKey();
}
void visu3DCubeExtrudedWithAxis(){
Mat3UI8 img;
img.load(POP_PROJECT_SOURCE_DIR+std::string("/image/rock3d.pgm"));
Scene3d scene;
Mat3UI8 extruded(img.getDomain());
int radius=img.getDomain()(0)/2;
Vec3I32 x1(0,0,0);
Vec3I32 x2(img.getDomain());
ForEachDomain3D(x,extruded){
if((x-x1).norm(2)<radius||(x-x2).norm(2)<radius)
extruded(x)=0;
else
extruded(x)=255;
}
Visualization::cubeExtruded(scene,img,extruded);//add the cube surfaces to the scene
Visualization::lineCube(scene,img);//add the border red lines to the scene to the scene
Visualization::axis(scene,40);//add axis
scene.display(false);//display the scene
waitKey();
}
void visu3DSlice(){
Mat3UI8 img;
img.load(POP_PROJECT_SOURCE_DIR+std::string("/image/rock3d.pgm"));
Scene3d scene;
Visualization::plane(scene,img,50,2);
Visualization::plane(scene,img,50,1);
Visualization::plane(scene,img,200,0);
scene.display(false);
waitKey();
}
void visu3DMarchingCube(){
Mat3UI8 img;
img.load(POP_PROJECT_SOURCE_DIR+std::string("/image/rock3d.pgm"));
img = img(Vec3I32(0,0,0),Vec3I32(64,64,64));
//SEGMENTATION OF THE TREE PHASES
Mat3UI8 imgfilter= Processing::median(img,2);
Mat3UI8 grain= Processing::threshold(imgfilter,155);
Mat3UI8 oil = Processing::threshold(imgfilter,70,110);
oil = Processing::openingRegionGrowing(oil,2);//To remove the interface artefact
Mat3UI8 air = Processing::threshold(imgfilter,0,40);
Mat3UI8 seed = Processing::labelMerge(grain,oil);
seed = Processing::labelMerge(seed,air);
Mat3UI8 water = Processing::watershed(seed,gradient);
//THE LABEL GRAIN WITH THE GREY-LEVEL 255*0.75
grain = Processing::labelFromSingleSeed(water,grain);
grain=Mat3F32(grain)*0.75;
//THE LABEL OIL WITH THE GREY-LEVEL 255*0.4
oil = Mat3F32(oil)*0.4f;
Mat3UI8 grain_oil = grain+oil;
Scene3d scene;
Visualization::marchingCube(scene,grain_oil);//add the marching cube of the grain to the scene
Visualization::lineCube(scene,grain_oil);//add the border red lines to the scene to the scene
scene.display(false);//display the scene
waitKey();
}
int main(){
visu2DSlice();
visu3DCube();
visu3DCubeExtrudedWithAxis();
visu3DMarchingCube();
visu3DSlice();
}

In python code, we have: Pyton code

1 import sys
2 import os
3 PathPop= ""
4 if os.path.isfile(PathPop+"population.py")==0:
5  print "set the variable PathPop to the path where you compile population, for instance D:\Users/vtariel/Desktop/ANV/Population-build/. This folder must contain population.py"
6  sys.exit(-1)
7 sys.path.append(PathPop)
8 from population import *
9 
10 
11 def visu2DSlice() :
12  img = Mat3UI8(PathPop+"/image/rock3d.pgm")
13  img.display()#use the arrows to move in z-axis
14 
15 def visu3DCube():
16  scene = Scene3d()
17  img = Mat3UI8(PathPop+"/image/rock3d.pgm")
18  visu = Visualization()
19  visu.cubeExtruded(scene,img)#add the cube surfaces to the scene
20  visu.lineCube(scene,img)#add the border red lines to the scene to the scene
21  scene.display(0)#display the scene
22  waitKey()
23 
24 def visu3DSlice():
25  scene = Scene3d()
26  img = Mat3UI8(PathPop+"/image/rock3d.pgm")
27  visu = Visualization()
28  visu.plane(scene,img,50,2)
29  visu.plane(scene,img,50,1)
30  visu.plane(scene,img,200,0)
31  visu.lineCube(scene,img)
32  scene.display(0)
33  waitKey()
34 
35 def visu3DMarchingCube():
36  img = Mat3UI8()
37  img.load(PathPop+"/image/rock3d.pgm")
38  img = img(Vec3I32(0,0,0),Vec3I32(64,64,64))
39  pde = PDE()
40  imgfilter= pde.nonLinearAnisotropicDiffusionDericheFast(img)
41  app=Application()
42  #app.thresholdSelection(imgfilter)
43  proc = Processing()
44  grain= proc.threshold(imgfilter,155)
45  oil = proc.threshold(imgfilter,70,110)
46  oil = proc.openingRegionGrowing(oil,2)
47  air = proc.threshold(imgfilter,0,40)
48  seed = proc.labelMerge(grain,oil)
49  seed = proc.labelMerge(seed,air)
50  visu =Visualization()
51  #visu.labelForeground(seed,imgfilter).display()
52  gradient = proc.gradientMagnitudeDeriche(img,1.5)
53  water = proc.watershed(seed,gradient)
54  grain = proc.labelFromSingleSeed(water,grain)
55  grain=grain/2
56  oil = proc.labelFromSingleSeed(water,oil)
57  oil = oil/4
58  scene = Scene3d()
59  visu.marchingCube(scene,grain)
60  visu.marchingCube(scene,oil)
61  visu.lineCube(scene,img)
62  scene.display(0)
63  waitKey()
64 
65 def visu3DCubeExtrudedWithAxis():
66  scene = Scene3d()
67  img = Mat3UI8()
68  img.load(PathPop+"/image/rock3d.pgm")
69  img = img(Vec3I32(0,0,0),Vec3I32(64,64,64))
70  extruded = Mat3UI8(img.getDomain())
71  radius=img.sizeI()/2
72  center = Vec3I32(img.getDomain()/2)
73  it = img.getIteratorEDomain()
74  while it.next():
75  if (it.x()-center).norm(2)<radius:
76  extruded.setValue(it.x(),100)
77  visu = Visualization()
78  visu.cubeExtruded(scene,img,extruded)#add the cube surfaces to the scene
79  visu.lineCube(scene,img)#add the border red lines to the scene to the scene
80  scene.display(0)#display the scene
81  waitKey()
82 
83 
84 visu2DSlice()
85 visu3DCube()
86 visu3DSlice()
87 visu3DMarchingCube()
88 visu3DCubeExtrudedWithAxis()
89