1 #ifndef GEOCUTSALGO_IMPL_T_HPP
2 #define GEOCUTSALGO_IMPL_T_HPP
3 #include <morphee/selement/include/selementNeighborList.hpp>
4 #include <morphee/selement/include/private/selementNeighborhood_T.hpp>
5 #include <morphee/image/include/private/image_T.hpp>
6 #include <morphee/image/include/imageUtils.hpp>
8 #include <boost/config.hpp>
9 #include <boost/utility.hpp>
10 #include <boost/graph/graph_traits.hpp>
11 #include <boost/graph/adjacency_list.hpp>
12 #include <boost/graph/graphviz.hpp>
13 #include <boost/graph/prim_minimum_spanning_tree.hpp>
14 #include <boost/graph/dijkstra_shortest_paths.hpp>
15 #include <boost/graph/johnson_all_pairs_shortest.hpp>
17 #include <boost/version.hpp>
18 #if BOOST_VERSION >= 104700
19 #include <boost/graph/boykov_kolmogorov_max_flow.hpp>
20 #elif BOOST_VERSION >= 103500
21 #include <boost/graph/kolmogorov_max_flow.hpp>
23 #include "../boost_ext/kolmogorov_max_flow.hpp"
26 #include "../boost_ext/kolmogorov_max_flow_min_cost.hpp"
29 #include <boost/graph/connected_components.hpp>
31 #include <morphee/image/include/private/image_T.hpp>
32 #include <morphee/image/include/private/imageManipulation_T.hpp>
33 #include <morphee/selement/include/selementNeighborList.hpp>
34 #include <morphee/selement/include/private/selementNeighborhood_T.hpp>
35 #include <morphee/morphoBase/include/private/morphoLabel_T.hpp>
36 #include <morphee/morphoBase/include/private/morphoLabel2_T.hpp>
37 #include <morphee/morphoBase/include/private/morphoGraphs_T.hpp>
38 #include <morphee/morphoBase/include/private/morphoHierarch_T.hpp>
39 #include <morphee/graph/include/private/graphProjection_T.hpp>
40 #include <morphee/graph/include/graph.hpp>
43 #include <graphs/MorphoGraph/include/Morpho_Graph_T.hpp>
52 #define M_PI 3.14159265358979323846
53 #define INFINI_POSITIF std::numeric_limits<double>::max)()
63 typedef std::list<morceau> affine_par_morceaux;
74 template <
class BoostGraph>
75 const BoostGraph t_CopyGraph(
const BoostGraph &graphIn);
76 template <
class Graph>
77 RES_C t_LabelConnectedComponent(
const Graph &GIn, Graph &Gout);
78 template <
class Graph>
79 RES_C t_LabelConnectedComponent(
const Graph &GIn, Graph &Gout,
int *num);
86 template <
class ImageWs,
class ImageIn,
class ImageGrad,
typename _alpha1,
87 typename _alpha2,
typename _alpha3,
class SE,
class BoostGraph>
88 RES_C t_TreeReweighting_old(
const ImageWs &imWs,
const ImageIn &imIn,
89 const ImageGrad &imGrad, BoostGraph &Treein,
90 const _alpha1 alpha1,
const _alpha2 alpha2,
91 const _alpha3 alpha3,
const SE &nl,
94 MORPHEE_ENTER_FUNCTION(
"t_TreeReweighting");
96 if ((!imWs.isAllocated())) {
97 MORPHEE_REGISTER_ERROR(
"Not allocated");
98 return RES_NOT_ALLOCATED;
101 if ((!imIn.isAllocated())) {
102 MORPHEE_REGISTER_ERROR(
"Not allocated");
103 return RES_NOT_ALLOCATED;
106 if ((!imGrad.isAllocated())) {
107 MORPHEE_REGISTER_ERROR(
"Not allocated");
108 return RES_NOT_ALLOCATED;
112 typename ImageWs::const_iterator it, iend;
113 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
114 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
118 typename BoostGraph::EdgeIterator ed_it, ed_end;
119 typename BoostGraph::EdgeIterator ed_it2, ed_end2;
120 typename BoostGraph::EdgeIterator ed_it3, ed_end3;
121 typename BoostGraph::EdgeIterator ed_it4, ed_end4;
123 typename BoostGraph::VertexIterator v_it, v_end;
125 typename BoostGraph::VertexProperty vdata1, vdata2, vdata11, vdata22;
126 typename BoostGraph::VertexProperty label1, label2;
131 typename BoostGraph::EdgeDescriptor e1, e2;
132 typename BoostGraph::VertexDescriptor vs, vt;
133 typename BoostGraph::EdgeProperty tmp, tmp2, tmp3;
135 std::vector<double> val_edges;
136 std::vector<double> val_edges2;
137 val_edges.push_back(0.0);
139 std::cout <<
"build Region Adjacency Graph" << std::endl;
141 for (it = imWs.begin(), iend = imWs.end(); it != iend;
145 int val = imWs.pixelFromOffset(o0);
152 std::cout <<
"Compute mean and quaderror" << std::endl;
153 std::vector<double> mean_values;
154 std::vector<double> number_of_values;
155 std::vector<double> quad_error;
156 std::vector<double> min_gradient_values;
157 std::vector<double> max_gradient_values;
158 std::vector<double> area_values;
163 float *histogram_region1;
164 histogram_region1 =
new float[255];
166 float *histogram_region2;
167 histogram_region2 =
new float[255];
169 float *cumul_histogram_region1;
170 cumul_histogram_region1 =
new float[255];
172 float *cumul_histogram_region2;
173 cumul_histogram_region2 =
new float[255];
175 for (
int dim_allouee0 = 0; dim_allouee0 < numVert; ++dim_allouee0) {
179 for (
int dim_allouee0 = 0; dim_allouee0 < numVert; ++dim_allouee0) {
180 for (
int dim_allouee1 = 0; dim_allouee1 < 255; ++dim_allouee1) {
181 histogram[dim_allouee0][dim_allouee1] = 0;
186 for (
int i = 0; i < numVert; i++) {
187 mean_values.push_back(0);
188 number_of_values.push_back(0);
189 quad_error.push_back(0);
190 min_gradient_values.push_back(1000000000000);
191 max_gradient_values.push_back(0);
192 area_values.push_back(0);
196 for (it = imWs.begin(), iend = imWs.end(); it != iend;
200 int val = imWs.pixelFromOffset(o1);
201 double val_image = imIn.pixelFromOffset(o1);
202 double val_gradient = imGrad.pixelFromOffset(o1);
204 mean_values[val - 1] = mean_values[val - 1] + val_image;
205 number_of_values[val - 1] = number_of_values[val - 1] + 1.0;
207 if (val_gradient < min_gradient_values[val - 1])
208 min_gradient_values[val - 1] = val_gradient;
209 if (val_gradient > max_gradient_values[val - 1])
210 max_gradient_values[val - 1] = val_gradient;
212 area_values[val - 1] = area_values[val - 1] + 1;
213 int vald = (int) (val_image);
218 for (
int i = 0; i < numVert; i++) {
219 mean_values[i] = mean_values[i] / number_of_values[i];
223 float max_quad_val = 0.0f;
224 for (it = imWs.begin(), iend = imWs.end(); it != iend;
228 int val = imWs.pixelFromOffset(o1);
229 double val_image = imIn.pixelFromOffset(o1);
231 quad_error[val - 1] =
232 (double) quad_error[val - 1] +
233 std::pow(std::abs(val_image - (
double) mean_values[val - 1]), 2);
235 if (quad_error[val - 1] > max_quad_val)
236 max_quad_val = quad_error[val - 1];
239 std::cout <<
"build Region Adjacency Graph Vertices" << std::endl;
240 std::cout <<
"number of Vertices:" << numVert << std::endl;
243 Tree_out = morphee::graph::CommonGraph32(numVert);
245 BoostGraph Gout = morphee::graph::CommonGraph32(numVert);
246 BoostGraph Gout_t = morphee::graph::CommonGraph32(numVert);
247 BoostGraph Gtemp = morphee::graph::CommonGraph32(numVert);
249 BoostGraph Tree_temp = morphee::graph::CommonGraph32(numVert);
250 BoostGraph Tree_temp2 = morphee::graph::CommonGraph32(numVert);
252 BoostGraph Gclassic = morphee::graph::CommonGraph32(numVert);
254 morphee::morphoBase::t_NeighborhoodGraphFromMosaic(imWs, nl, Gclassic);
257 ImageWs ImTempSurfaces = imWs.getSame();
258 morphee::morphoBase::t_ImLabelFlatZonesWithArea(imWs, nl, ImTempSurfaces);
259 morphee::graph::t_ProjectMarkersOnGraph(ImTempSurfaces, imWs, Gclassic);
261 morphee::morphoBase::t_ImLabelFlatZonesWithVolume(imWs, imGrad, nl,
263 morphee::graph::t_ProjectMarkersOnGraph(ImTempSurfaces, imWs, Gout);
265 std::cout <<
"build Region Adjacency Graph Edges" << std::endl;
268 double local_volume = 0.0;
269 double area_total = 0.0;
272 for (boost::tie(v_it, v_end) = boost::vertices(Gout.getBoostGraph());
273 v_it != v_end; ++v_it) {
275 area_values[(int) *v_it] * (max_gradient_values[(
int) *v_it] -
276 min_gradient_values[(int) *v_it]);
278 Gout.setVertexData(*v_it, (
float) local_volume);
284 Gclassic.vertexData(*v_it, &vdata1);
285 area_total = area_total + (double) vdata1;
289 for (it = imWs.begin(), iend = imWs.end(); it != iend;
293 int val = imWs.pixelFromOffset(o1);
296 neighb.setCenter(o1);
298 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
299 const offset_t o2 = nit.getOffset();
300 int val2 = imWs.pixelFromOffset(o2);
302 if (o2 > o1 || o1 < o2) {
304 boost::tie(e1, in1) =
305 boost::edge(val - 1, val2 - 1, Gout.getBoostGraph());
306 boost::tie(e2, in1) =
307 boost::edge(val - 1, val2 - 1, Gtemp.getBoostGraph());
309 double val3 = imGrad.pixelFromOffset(o1);
310 double val4 = imGrad.pixelFromOffset(o2);
311 double maxi = std::max(val3, val4);
315 Gout.addEdge(val - 1, val2 - 1, maxi);
316 Gtemp.addEdge(val - 1, val2 - 1, 1);
318 Gout.edgeWeight(e1, &tmp);
319 Gout.setEdgeWeight(e1, tmp + maxi);
321 Gtemp.edgeWeight(e2, &tmp2);
322 Gtemp.setEdgeWeight(e2, tmp2 + 1);
330 std::cout <<
"number of Edges : " << numEdges << std::endl;
332 Gout_t = t_CopyGraph(Gout);
333 boost::tie(ed_it2, ed_end2) = boost::edges(Gtemp.getBoostGraph());
334 boost::tie(ed_it3, ed_end3) = boost::edges(Gclassic.getBoostGraph());
335 boost::tie(ed_it4, ed_end4) = boost::edges(Gout_t.getBoostGraph());
336 float current_max_value = 0.0f;
339 for (boost::tie(ed_it, ed_end) = boost::edges(Gout.getBoostGraph());
340 ed_it != ed_end, ed_it2 != ed_end2, ed_it3 != ed_end3,
342 ++ed_it, ++ed_it2, ++ed_it3, ++ed_it4) {
343 Gout.edgeWeight(*ed_it, &tmp);
344 Gtemp.edgeWeight(*ed_it2, &tmp2);
345 Gout_t.setEdgeWeight(*ed_it4, ((
double) tmp2));
352 Gtemp = t_CopyGraph(Treein);
354 for (boost::tie(ed_it, ed_end) = boost::edges(Gtemp.getBoostGraph());
355 ed_it != ed_end; ++ed_it) {
356 Gtemp.edgeWeight(*ed_it, &tmp);
357 val_edges.push_back(tmp);
360 std::vector<typename BoostGraph::EdgeDescriptor> removed_edges;
362 Tree_out = t_CopyGraph(Gtemp);
363 Tree_temp = t_CopyGraph(Gtemp);
364 Tree_temp2 = t_CopyGraph(Gtemp);
367 std::cout <<
"sort" << std::endl;
368 std::sort(val_edges.begin(), val_edges.end(), std::less<double>());
370 double last_edge_value = val_edges.back();
371 double last_analyzed = last_edge_value;
373 while (val_edges.size() > 1) {
374 std::cout << last_edge_value << std::endl;
377 for (boost::tie(ed_it, ed_end) = boost::edges(Gtemp.getBoostGraph());
378 ed_it != ed_end; ++ed_it) {
379 Gtemp.edgeWeight(*ed_it, &tmp);
381 if (tmp == last_edge_value) {
382 vs = Gtemp.edgeSource(*ed_it);
383 vt = Gtemp.edgeTarget(*ed_it);
384 boost::tie(e1, in1) = boost::edge(vs, vt, Gtemp.getBoostGraph());
385 removed_edges.push_back(e1);
386 Tree_temp.removeEdge(vs, vt);
391 t_LabelConnectedComponent(Tree_temp, Tree_temp2);
394 float mean_value = 0.0f;
395 float number_of_points = 0.0f;
396 float volume1 = 0.0f;
397 float volume2 = 0.0f;
399 float volume11 = 0.0f;
400 float volume22 = 0.0f;
406 float mean_val_1 = 0.0f;
407 float mean_val_2 = 0.0f;
408 float nb_val_1 = 0.0f;
409 float nb_val_2 = 0.0f;
410 float mean_value_1 = 0.0f;
411 float mean_value_2 = 0.0f;
412 float number_of_points1 = 0.0f;
413 float number_of_points2 = 0.0f;
414 float dist_histo = 0.0f;
418 while (removed_edges.size() > 0) {
425 number_of_points = 0.0f;
429 number_of_points1 = 0.0f;
430 number_of_points2 = 0.0f;
443 for (
int dim_allouee1 = 0; dim_allouee1 < 255; ++dim_allouee1) {
444 histogram_region1[dim_allouee1] = 0;
445 histogram_region2[dim_allouee1] = 0;
446 cumul_histogram_region1[dim_allouee1] = 0;
447 cumul_histogram_region2[dim_allouee1] = 0;
450 e1 = removed_edges.back();
451 removed_edges.pop_back();
454 Tree_temp2.vertexData(Tree_temp2.edgeSource(e1), &label1);
455 Tree_temp2.vertexData(Tree_temp2.edgeTarget(e1), &label2);
457 boost::tie(ed_it4, ed_end4) = boost::edges(Gout_t.getBoostGraph());
459 for (boost::tie(ed_it, ed_end) = boost::edges(Gout.getBoostGraph());
460 ed_it != ed_end, ed_it4 != ed_end4; ++ed_it, ++ed_it4) {
461 vs = Gout.edgeSource(*ed_it);
462 vt = Gout.edgeTarget(*ed_it);
465 Tree_temp2.vertexData(vs, &vdata1);
466 Tree_temp2.vertexData(vt, &vdata2);
469 if ((vdata1 == label1 && vdata2 == label2) ||
470 (vdata1 == label2 && vdata2 == label1)) {
471 Gout.edgeWeight(*ed_it, &tmp);
472 Gout_t.edgeWeight(*ed_it4, &tmp2);
474 mean_value = mean_value + (float) tmp;
475 number_of_points = number_of_points + (float) tmp2;
479 if ((vdata1 == label1 && vdata2 != label1) ||
480 (vdata2 == label1 && vdata1 != label1)) {
481 Gout.edgeWeight(*ed_it, &tmp);
482 Gout_t.edgeWeight(*ed_it4, &tmp2);
484 mean_value_1 = mean_value_1 + (float) tmp;
485 number_of_points1 = number_of_points1 + (float) tmp2;
489 if ((vdata1 == label2 && vdata2 != label2) ||
490 (vdata2 == label2 && vdata1 != label2)) {
491 Gout.edgeWeight(*ed_it, &tmp);
492 Gout_t.edgeWeight(*ed_it4, &tmp2);
494 mean_value_2 = mean_value_2 + (float) tmp;
495 number_of_points2 = number_of_points2 + (float) tmp2;
500 for (boost::tie(v_it, v_end) = boost::vertices(Gout.getBoostGraph());
501 v_it != v_end; ++v_it) {
503 Tree_temp2.vertexData(*v_it, &vdata1);
504 Gout.vertexData(*v_it, &vdata11);
506 Tree_out.setVertexData(*v_it, (
float) alpha1 * vdata11 +
508 quad_error[(
int) *v_it]);
511 if (vdata1 == label1) {
517 area1 = area1 + (float) vdata11;
518 mean_val_1 = mean_val_1 + mean_values[(int) *v_it];
519 nb_val_1 = nb_val_1 + 1.0f;
521 for (
int dim_allouee1 = 0; dim_allouee1 < 255; ++dim_allouee1) {
522 histogram_region1[dim_allouee1] =
523 histogram_region1[dim_allouee1] +
527 if (vdata1 == label2) {
533 area2 = area2 + (float) vdata11;
534 mean_val_2 = mean_val_2 + mean_values[(int) *v_it];
535 nb_val_2 = nb_val_2 + 1.0f;
537 for (
int dim_allouee1 = 0; dim_allouee1 < 255; ++dim_allouee1) {
538 histogram_region2[dim_allouee1] =
539 histogram_region2[dim_allouee1] +
545 for (
int dim_allouee1 = 0; dim_allouee1 < 255; ++dim_allouee1) {
546 for (
int dim_allouee0 = 0; dim_allouee0 <= dim_allouee1;
548 cumul_histogram_region1[dim_allouee1] =
549 cumul_histogram_region1[dim_allouee1] +
550 (float) histogram_region1[dim_allouee0] / (
float) area1;
551 cumul_histogram_region2[dim_allouee1] =
552 cumul_histogram_region2[dim_allouee1] +
553 (float) histogram_region2[dim_allouee0] / (
float) area2;
557 for (
int dim_allouee1 = 0; dim_allouee1 < 255; ++dim_allouee1) {
559 dist_histo +
std::pow(cumul_histogram_region1[dim_allouee1] -
560 cumul_histogram_region2[dim_allouee1],
565 for (boost::tie(v_it, v_end) = boost::vertices(Gout.getBoostGraph());
566 v_it != v_end; ++v_it) {
568 Tree_temp2.vertexData(*v_it, &vdata1);
571 if (vdata1 == label1) {
573 (mean_values[(int) *v_it] - mean_val_1 / nb_val_1) *
574 (mean_values[(int) *v_it] - mean_val_1 / nb_val_1);
576 if (vdata1 == label2) {
578 (mean_values[(int) *v_it] - mean_val_2 / nb_val_2) *
579 (mean_values[(int) *v_it] - mean_val_2 / nb_val_2);
583 quad1 = quad1 / nb_val_1;
584 quad2 = quad2 / nb_val_2;
586 volume11 = area1 * mean_value_1 / number_of_points1;
587 volume22 = area2 * mean_value_2 / number_of_points2;
589 float probability_volume =
598 float probability_area =
599 (1.0 -
std::pow(1.0 - area1 / ((
double) area_total), 20) -
600 std::pow(1.0 - area2 / ((
double) area_total), 20) +
601 std::pow(1.0 - (area1 + area2) / ((
double) area_total), 20));
603 std::abs((mean_val_2 / nb_val_2) - (mean_val_1 / nb_val_1));
605 probability_volume = std::min(probability_volume, 0.05f);
609 boost::tie(e2, in1) =
610 boost::edge(Tree_out.edgeSource(e1), Tree_out.edgeTarget(e1),
611 Tree_out.getBoostGraph());
612 float val_gradient_mean_t =
613 std::min(((
float) last_edge_value) / (65535.0f), 0.7f);
615 if (number_of_points > 0.0f) {
620 float value_test = 2000000.0f *
621 std::pow(val_gradient_mean_t, (
float) alpha1) *
622 std::pow(probability_volume, (
float) alpha3);
624 Tree_out.setEdgeWeight(e2, value_test);
626 if (value_test > current_max_value)
627 current_max_value = value_test;
630 Tree_out.setEdgeWeight(e2, 0.0f);
634 while (last_edge_value == last_analyzed) {
635 last_edge_value = val_edges.back();
636 val_edges.pop_back();
638 last_analyzed = last_edge_value;
641 std::cout <<
"current_max_value" << current_max_value << std::endl;
643 for (boost::tie(ed_it, ed_end) = boost::edges(Tree_out.getBoostGraph());
644 ed_it != ed_end; ++ed_it) {
645 Tree_out.edgeWeight(*ed_it, &tmp);
646 float value_test = 65535.0f *
std::pow((
float) tmp / current_max_value,
648 Tree_out.setEdgeWeight(*ed_it, value_test);
651 std::cout <<
"alpha2" << alpha2 << std::endl;
666 template <
class ImageWs,
class ImageIn,
class ImageGrad,
typename _alpha1,
667 typename _alpha2,
typename _alpha3,
class SE,
class BoostGraph>
668 RES_C t_TreeReweighting2(
const ImageWs &imWs,
const ImageIn &imIn,
669 const ImageGrad &imGrad, BoostGraph &Treein,
670 const _alpha1 alpha1,
const _alpha2 alpha2,
671 const _alpha3 alpha3,
const SE &nl,
672 BoostGraph &Tree_out)
674 MORPHEE_ENTER_FUNCTION(
"t_TreeReweighting");
676 if ((!imWs.isAllocated())) {
677 MORPHEE_REGISTER_ERROR(
"Not allocated");
678 return RES_NOT_ALLOCATED;
681 if ((!imIn.isAllocated())) {
682 MORPHEE_REGISTER_ERROR(
"Not allocated");
683 return RES_NOT_ALLOCATED;
686 if ((!imGrad.isAllocated())) {
687 MORPHEE_REGISTER_ERROR(
"Not allocated");
688 return RES_NOT_ALLOCATED;
692 typename ImageWs::const_iterator it, iend;
693 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
694 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
698 typename BoostGraph::EdgeIterator ed_it, ed_end;
699 typename BoostGraph::EdgeIterator ed_it2, ed_end2;
700 typename BoostGraph::EdgeIterator ed_it3, ed_end3;
701 typename BoostGraph::EdgeIterator ed_it4, ed_end4;
703 typename BoostGraph::VertexIterator v_it, v_end;
705 typename BoostGraph::VertexProperty vdata1, vdata2, vdata11, vdata22;
706 typename BoostGraph::VertexProperty label1, label2;
711 typename BoostGraph::EdgeDescriptor e1, e2;
712 typename BoostGraph::VertexDescriptor vs, vt;
713 typename BoostGraph::EdgeProperty tmp, tmp2, tmp3;
715 std::vector<double> val_edges;
716 std::vector<double> val_edges2;
717 val_edges.push_back(0.0);
719 std::cout <<
"build Region Adjacency Graph" << std::endl;
721 for (it = imWs.begin(), iend = imWs.end(); it != iend;
725 int val = imWs.pixelFromOffset(o0);
732 std::cout <<
"Compute mean and quaderror" << std::endl;
733 std::vector<double> mean_values;
734 std::vector<double> number_of_values;
735 std::vector<double> quad_error;
736 std::vector<double> min_gradient_values;
737 std::vector<double> max_gradient_values;
738 std::vector<double> area_values;
743 float *histogram_region1;
744 histogram_region1 =
new float[255];
746 float *histogram_region2;
747 histogram_region2 =
new float[255];
749 float *cumul_histogram_region1;
750 cumul_histogram_region1 =
new float[255];
752 float *cumul_histogram_region2;
753 cumul_histogram_region2 =
new float[255];
755 for (
int dim_allouee0 = 0; dim_allouee0 < numVert; ++dim_allouee0) {
759 for (
int dim_allouee0 = 0; dim_allouee0 < numVert; ++dim_allouee0) {
760 for (
int dim_allouee1 = 0; dim_allouee1 < 255; ++dim_allouee1) {
761 histogram[dim_allouee0][dim_allouee1] = 0;
766 for (
int i = 0; i < numVert; i++) {
767 mean_values.push_back(0);
768 number_of_values.push_back(0);
769 quad_error.push_back(0);
770 min_gradient_values.push_back(1000000000000);
771 max_gradient_values.push_back(0);
772 area_values.push_back(0);
776 for (it = imWs.begin(), iend = imWs.end(); it != iend;
780 int val = imWs.pixelFromOffset(o1);
781 double val_image = imIn.pixelFromOffset(o1);
782 double val_gradient = imGrad.pixelFromOffset(o1);
784 mean_values[val - 1] = mean_values[val - 1] + val_image;
785 number_of_values[val - 1] = number_of_values[val - 1] + 1.0;
787 if (val_gradient < min_gradient_values[val - 1])
788 min_gradient_values[val - 1] = val_gradient;
789 if (val_gradient > max_gradient_values[val - 1])
790 max_gradient_values[val - 1] = val_gradient;
792 area_values[val - 1] = area_values[val - 1] + 1;
793 int vald = (int) (val_image);
798 for (
int i = 0; i < numVert; i++) {
799 mean_values[i] = mean_values[i] / number_of_values[i];
803 float max_quad_val = 0.0f;
804 for (it = imWs.begin(), iend = imWs.end(); it != iend;
808 int val = imWs.pixelFromOffset(o1);
809 double val_image = imIn.pixelFromOffset(o1);
811 quad_error[val - 1] =
812 (double) quad_error[val - 1] +
813 std::pow(std::abs(val_image - (
double) mean_values[val - 1]), 2);
815 if (quad_error[val - 1] > max_quad_val)
816 max_quad_val = quad_error[val - 1];
819 std::cout <<
"build Region Adjacency Graph Vertices" << std::endl;
820 std::cout <<
"number of Vertices:" << numVert << std::endl;
823 Tree_out = morphee::graph::CommonGraph32(numVert);
825 BoostGraph Gout = morphee::graph::CommonGraph32(numVert);
826 BoostGraph Gout_t = morphee::graph::CommonGraph32(numVert);
827 BoostGraph Gtemp = morphee::graph::CommonGraph32(numVert);
829 BoostGraph Tree_temp = morphee::graph::CommonGraph32(numVert);
830 BoostGraph Tree_temp2 = morphee::graph::CommonGraph32(numVert);
833 BoostGraph G_area = morphee::graph::CommonGraph32(numVert);
834 morphee::morphoBase::t_NeighborhoodGraphFromMosaic(imWs, nl, G_area);
837 ImageWs ImTempSurfaces = imWs.getSame();
838 morphee::morphoBase::t_ImLabelFlatZonesWithArea(imWs, nl, ImTempSurfaces);
839 morphee::graph::t_ProjectMarkersOnGraph(ImTempSurfaces, imWs, G_area);
842 morphee::morphoBase::t_ImLabelFlatZonesWithVolume(imWs, imGrad, nl,
844 morphee::graph::t_ProjectMarkersOnGraph(ImTempSurfaces, imWs, Gout);
846 std::cout <<
"build Region Adjacency Graph Edges" << std::endl;
849 double local_volume = 0.0;
850 double area_total = 0.0;
853 for (boost::tie(v_it, v_end) = boost::vertices(Gout.getBoostGraph());
854 v_it != v_end; ++v_it) {
856 area_values[(int) *v_it] * (max_gradient_values[(
int) *v_it] -
857 min_gradient_values[(int) *v_it]);
859 Gout.vertexData(*v_it, &vdata1);
862 G_area.vertexData(*v_it, &vdata1);
863 area_total = area_total + (double) vdata1;
867 for (it = imWs.begin(), iend = imWs.end(); it != iend;
871 int val = imWs.pixelFromOffset(o1);
874 neighb.setCenter(o1);
876 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
877 const offset_t o2 = nit.getOffset();
878 int val2 = imWs.pixelFromOffset(o2);
882 boost::tie(e1, in1) =
883 boost::edge(val - 1, val2 - 1, Gout.getBoostGraph());
884 boost::tie(e2, in1) =
885 boost::edge(val - 1, val2 - 1, Gtemp.getBoostGraph());
887 double val3 = imGrad.pixelFromOffset(o1);
888 double val4 = imGrad.pixelFromOffset(o2);
889 double maxi = std::max(val3, val4);
893 Gout.addEdge(val - 1, val2 - 1, maxi);
894 Gtemp.addEdge(val - 1, val2 - 1, 1);
896 Gout.edgeWeight(e1, &tmp);
897 Gout.setEdgeWeight(e1, tmp + maxi);
899 Gtemp.edgeWeight(e2, &tmp2);
900 Gtemp.setEdgeWeight(e2, tmp2 + 1);
908 std::cout <<
"number of Edges : " << numEdges << std::endl;
910 Gout_t = t_CopyGraph(Gout);
911 boost::tie(ed_it2, ed_end2) = boost::edges(Gtemp.getBoostGraph());
912 boost::tie(ed_it3, ed_end3) = boost::edges(G_area.getBoostGraph());
913 boost::tie(ed_it4, ed_end4) = boost::edges(Gout_t.getBoostGraph());
914 float current_max_value = 0.0f;
917 for (boost::tie(ed_it, ed_end) = boost::edges(Gout.getBoostGraph());
918 ed_it != ed_end, ed_it2 != ed_end2, ed_it3 != ed_end3,
920 ++ed_it, ++ed_it2, ++ed_it3, ++ed_it4) {
921 Gout.edgeWeight(*ed_it, &tmp);
922 Gtemp.edgeWeight(*ed_it2, &tmp2);
923 Gout_t.setEdgeWeight(*ed_it4, ((
double) tmp2));
930 Gtemp = t_CopyGraph(Treein);
932 for (boost::tie(ed_it, ed_end) = boost::edges(Gtemp.getBoostGraph());
933 ed_it != ed_end; ++ed_it) {
934 Gtemp.edgeWeight(*ed_it, &tmp);
935 val_edges.push_back(tmp);
938 std::vector<typename BoostGraph::EdgeDescriptor> removed_edges;
940 Tree_out = t_CopyGraph(Gtemp);
941 Tree_temp = t_CopyGraph(Gtemp);
942 Tree_temp2 = t_CopyGraph(Gtemp);
945 std::cout <<
"sort" << std::endl;
946 std::sort(val_edges.begin(), val_edges.end(), std::less<double>());
948 double last_edge_value = val_edges.back();
949 double last_analyzed = last_edge_value;
951 while (val_edges.size() > 1) {
952 std::cout << last_edge_value << std::endl;
955 for (boost::tie(ed_it, ed_end) = boost::edges(Gtemp.getBoostGraph());
956 ed_it != ed_end; ++ed_it) {
957 Gtemp.edgeWeight(*ed_it, &tmp);
959 if (tmp == last_edge_value) {
960 vs = Gtemp.edgeSource(*ed_it);
961 vt = Gtemp.edgeTarget(*ed_it);
962 boost::tie(e1, in1) = boost::edge(vs, vt, Gtemp.getBoostGraph());
963 removed_edges.push_back(e1);
964 Tree_temp.removeEdge(vs, vt);
969 int nb_of_connected = t_LabelConnectedComponent(Tree_temp, Tree_temp2);
975 float mean_value = 0.0f;
976 float number_of_points = 0.0f;
977 float volume1 = 0.0f;
978 float volume2 = 0.0f;
980 float volume11 = 0.0f;
981 float volume22 = 0.0f;
987 float mean_val_1 = 0.0f;
988 float mean_val_2 = 0.0f;
989 float nb_val_1 = 0.0f;
990 float nb_val_2 = 0.0f;
991 float mean_value_1 = 0.0f;
992 float mean_value_2 = 0.0f;
993 float number_of_points1 = 0.0f;
994 float number_of_points2 = 0.0f;
995 float dist_histo = 0.0f;
999 while (removed_edges.size() > 0) {
1006 number_of_points = 0.0f;
1008 mean_value_1 = 0.0f;
1009 mean_value_2 = 0.0f;
1010 number_of_points1 = 0.0f;
1011 number_of_points2 = 0.0f;
1024 for (
int dim_allouee1 = 0; dim_allouee1 < 255; ++dim_allouee1) {
1025 histogram_region1[dim_allouee1] = 0;
1026 histogram_region2[dim_allouee1] = 0;
1027 cumul_histogram_region1[dim_allouee1] = 0;
1028 cumul_histogram_region2[dim_allouee1] = 0;
1031 e1 = removed_edges.back();
1032 removed_edges.pop_back();
1035 Tree_temp2.vertexData(Tree_temp2.edgeSource(e1), &label1);
1036 Tree_temp2.vertexData(Tree_temp2.edgeTarget(e1), &label2);
1038 boost::tie(ed_it4, ed_end4) = boost::edges(Gout_t.getBoostGraph());
1040 for (boost::tie(ed_it, ed_end) = boost::edges(Gout.getBoostGraph());
1041 ed_it != ed_end, ed_it4 != ed_end4; ++ed_it, ++ed_it4) {
1042 vs = Gout.edgeSource(*ed_it);
1043 vt = Gout.edgeTarget(*ed_it);
1046 Tree_temp2.vertexData(vs, &vdata1);
1047 Tree_temp2.vertexData(vt, &vdata2);
1050 if ((vdata1 == label1 && vdata2 == label2) ||
1051 (vdata1 == label2 && vdata2 == label1)) {
1052 Gout.edgeWeight(*ed_it, &tmp);
1053 Gout_t.edgeWeight(*ed_it4, &tmp2);
1055 mean_value = mean_value + (float) tmp;
1056 number_of_points = number_of_points + (float) tmp2;
1060 if ((vdata1 == label1 && vdata2 != label1) ||
1061 (vdata2 == label1 && vdata1 != label1)) {
1062 Gout.edgeWeight(*ed_it, &tmp);
1063 Gout_t.edgeWeight(*ed_it4, &tmp2);
1065 mean_value_1 = mean_value_1 + (float) tmp;
1066 number_of_points1 = number_of_points1 + (float) tmp2;
1070 if ((vdata1 == label2 && vdata2 != label2) ||
1071 (vdata2 == label2 && vdata1 != label2)) {
1072 Gout.edgeWeight(*ed_it, &tmp);
1073 Gout_t.edgeWeight(*ed_it4, &tmp2);
1075 mean_value_2 = mean_value_2 + (float) tmp;
1076 number_of_points2 = number_of_points2 + (float) tmp2;
1081 for (boost::tie(v_it, v_end) = boost::vertices(Gout.getBoostGraph());
1082 v_it != v_end; ++v_it) {
1084 Tree_temp2.vertexData(*v_it, &vdata1);
1085 Gout.vertexData(*v_it, &vdata11);
1087 Tree_out.setVertexData(*v_it, (
float) alpha1 * vdata11 +
1089 quad_error[(
int) *v_it]);
1092 if (vdata1 == label1) {
1098 area1 = area1 + (float) vdata11;
1099 mean_val_1 = mean_val_1 + mean_values[(int) *v_it];
1100 nb_val_1 = nb_val_1 + 1.0f;
1102 for (
int dim_allouee1 = 0; dim_allouee1 < 255; ++dim_allouee1) {
1103 histogram_region1[dim_allouee1] =
1104 histogram_region1[dim_allouee1] +
1108 if (vdata1 == label2) {
1114 area2 = area2 + (float) vdata11;
1115 mean_val_2 = mean_val_2 + mean_values[(int) *v_it];
1116 nb_val_2 = nb_val_2 + 1.0f;
1118 for (
int dim_allouee1 = 0; dim_allouee1 < 255; ++dim_allouee1) {
1119 histogram_region2[dim_allouee1] =
1120 histogram_region2[dim_allouee1] +
1126 for (
int dim_allouee1 = 0; dim_allouee1 < 255; ++dim_allouee1) {
1127 for (
int dim_allouee0 = 0; dim_allouee0 <= dim_allouee1;
1129 cumul_histogram_region1[dim_allouee1] =
1130 cumul_histogram_region1[dim_allouee1] +
1131 (float) histogram_region1[dim_allouee0] / (
float) area1;
1132 cumul_histogram_region2[dim_allouee1] =
1133 cumul_histogram_region2[dim_allouee1] +
1134 (float) histogram_region2[dim_allouee0] / (
float) area2;
1138 for (
int dim_allouee1 = 0; dim_allouee1 < 255; ++dim_allouee1) {
1140 dist_histo +
std::pow(cumul_histogram_region1[dim_allouee1] -
1141 cumul_histogram_region2[dim_allouee1],
1146 for (boost::tie(v_it, v_end) = boost::vertices(Gout.getBoostGraph());
1147 v_it != v_end; ++v_it) {
1149 Tree_temp2.vertexData(*v_it, &vdata1);
1152 if (vdata1 == label1) {
1154 (mean_values[(int) *v_it] - mean_val_1 / nb_val_1) *
1155 (mean_values[(int) *v_it] - mean_val_1 / nb_val_1);
1157 if (vdata1 == label2) {
1159 (mean_values[(int) *v_it] - mean_val_2 / nb_val_2) *
1160 (mean_values[(int) *v_it] - mean_val_2 / nb_val_2);
1164 quad1 = quad1 / nb_val_1;
1165 quad2 = quad2 / nb_val_2;
1170 volume11 = area1 * last_edge_value / number_of_points1;
1171 volume22 = area2 * last_edge_value / number_of_points2;
1178 float probability_volume =
1179 (1.0 -
std::pow(1.0 - volume1 / ((
double)
volume), (
int) alpha1) -
1183 float probability_area =
1185 std::pow(1.0 - area1 / ((
double) area_total), (
int) alpha1) -
1186 std::pow(1.0 - area2 / ((
double) area_total), (
int) alpha1) +
1187 std::pow(1.0 - (area1 + area2) / ((
double) area_total),
1190 float deterministic_area =
1191 std::min(area1, area2) / ((double) area_total);
1192 float deterministic_volume =
1193 std::min(volume11, volume22) / ((double)
volume);
1196 std::abs((mean_val_2 / nb_val_2) - (mean_val_1 / nb_val_1));
1197 float mean_gradient = mean_value / number_of_points;
1199 float mean_gradient_1 = mean_value_1 / number_of_points1;
1200 float mean_gradient_2 = mean_value_2 / number_of_points2;
1202 float shape_factor_1 =
1203 4.0f * M_PI * area1 / (number_of_points1 * number_of_points1);
1204 float shape_factor_2 =
1205 4.0f * M_PI * area2 / (number_of_points2 * number_of_points2);
1207 shape_factor_1 = std::min(shape_factor_1, (
float) 1.0);
1208 shape_factor_2 = std::min(shape_factor_2, (
float) 1.0);
1217 boost::tie(e2, in1) =
1218 boost::edge(Tree_out.edgeSource(e1), Tree_out.edgeTarget(e1),
1219 Tree_out.getBoostGraph());
1226 if (number_of_points > 0.0f) {
1244 float value_test = 65535.0f * (mean_gradient / 65535.0f) *
1245 std::pow(probability_area, (
float) alpha2) *
1246 std::pow(probability_volume, (
float) alpha3);
1251 Tree_out.setEdgeWeight(e2, value_test);
1253 if (value_test > current_max_value)
1254 current_max_value = value_test;
1257 Tree_out.setEdgeWeight(e2, 0.0f);
1261 while (last_edge_value == last_analyzed) {
1262 last_edge_value = val_edges.back();
1263 val_edges.pop_back();
1265 last_analyzed = last_edge_value;
1268 std::cout <<
"current_max_value" << current_max_value << std::endl;
1273 template <
class ImageWs,
class ImageIn,
class ImageGrad,
typename _alpha1,
1274 typename _alpha2,
typename _alpha3,
class SE,
class BoostGraph>
1275 RES_C t_TreeReweighting(
const ImageWs &imWs,
const ImageIn &imIn,
1276 const ImageGrad &imGrad, BoostGraph &Treein,
1277 const _alpha1 alpha1,
const _alpha2 alpha2,
1278 const _alpha3 alpha3,
const SE &nl,
1279 BoostGraph &Tree_out)
1281 MORPHEE_ENTER_FUNCTION(
"t_TreeReweighting");
1283 if ((!imWs.isAllocated())) {
1284 MORPHEE_REGISTER_ERROR(
"Not allocated");
1285 return RES_NOT_ALLOCATED;
1288 if ((!imIn.isAllocated())) {
1289 MORPHEE_REGISTER_ERROR(
"Not allocated");
1290 return RES_NOT_ALLOCATED;
1293 if ((!imGrad.isAllocated())) {
1294 MORPHEE_REGISTER_ERROR(
"Not allocated");
1295 return RES_NOT_ALLOCATED;
1299 typename ImageWs::const_iterator it, iend;
1300 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
1301 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
1305 typename BoostGraph::EdgeIterator ed_it, ed_end;
1306 typename BoostGraph::EdgeIterator ed_it2, ed_end2;
1307 typename BoostGraph::EdgeIterator ed_it3, ed_end3;
1308 typename BoostGraph::EdgeIterator ed_it4, ed_end4;
1310 typename BoostGraph::VertexIterator v_it, v_end;
1312 typename BoostGraph::VertexProperty vdata1, vdata2, vdata11, vdata22;
1313 typename BoostGraph::VertexProperty label1, label2;
1318 typename BoostGraph::EdgeDescriptor e1, e2;
1319 typename BoostGraph::VertexDescriptor vs, vt;
1320 typename BoostGraph::EdgeProperty tmp, tmp2, tmp3;
1322 std::vector<double> val_edges;
1323 std::vector<double> val_edges2;
1324 val_edges.push_back(0.0);
1326 std::cout <<
"build Region Adjacency Graph" << std::endl;
1328 for (it = imWs.begin(), iend = imWs.end(); it != iend;
1331 o0 = it.getOffset();
1332 int val = imWs.pixelFromOffset(o0);
1334 if (val > numVert) {
1339 std::cout <<
"Compute mean and quaderror" << std::endl;
1340 std::vector<double> mean_values;
1341 std::vector<double> number_of_values;
1342 std::vector<double> quad_error;
1343 std::vector<double> min_gradient_values;
1344 std::vector<double> max_gradient_values;
1345 std::vector<double> area_values;
1350 float *histogram_region1;
1351 histogram_region1 =
new float[255];
1353 float *histogram_region2;
1354 histogram_region2 =
new float[255];
1356 float *cumul_histogram_region1;
1357 cumul_histogram_region1 =
new float[255];
1359 float *cumul_histogram_region2;
1360 cumul_histogram_region2 =
new float[255];
1362 for (
int dim_allouee0 = 0; dim_allouee0 < numVert; ++dim_allouee0) {
1366 for (
int dim_allouee0 = 0; dim_allouee0 < numVert; ++dim_allouee0) {
1367 for (
int dim_allouee1 = 0; dim_allouee1 < 255; ++dim_allouee1) {
1368 histogram[dim_allouee0][dim_allouee1] = 0;
1373 for (
int i = 0; i < numVert; i++) {
1374 mean_values.push_back(0);
1375 number_of_values.push_back(0);
1376 quad_error.push_back(0);
1377 min_gradient_values.push_back(1000000000000);
1378 max_gradient_values.push_back(0);
1379 area_values.push_back(0);
1383 for (it = imWs.begin(), iend = imWs.end(); it != iend;
1386 o1 = it.getOffset();
1387 int val = imWs.pixelFromOffset(o1);
1388 double val_image = imIn.pixelFromOffset(o1);
1389 double val_gradient = imGrad.pixelFromOffset(o1);
1391 mean_values[val - 1] = mean_values[val - 1] + val_image;
1392 number_of_values[val - 1] = number_of_values[val - 1] + 1.0;
1394 if (val_gradient < min_gradient_values[val - 1])
1395 min_gradient_values[val - 1] = val_gradient;
1396 if (val_gradient > max_gradient_values[val - 1])
1397 max_gradient_values[val - 1] = val_gradient;
1399 area_values[val - 1] = area_values[val - 1] + 1;
1400 int vald = (int) (val_image);
1405 for (
int i = 0; i < numVert; i++) {
1406 mean_values[i] = mean_values[i] / number_of_values[i];
1410 float max_quad_val = 0.0f;
1411 for (it = imWs.begin(), iend = imWs.end(); it != iend;
1414 o1 = it.getOffset();
1415 int val = imWs.pixelFromOffset(o1);
1416 double val_image = imIn.pixelFromOffset(o1);
1418 quad_error[val - 1] =
1419 (double) quad_error[val - 1] +
1420 std::pow(std::abs(val_image - (
double) mean_values[val - 1]), 2);
1422 if (quad_error[val - 1] > max_quad_val)
1423 max_quad_val = quad_error[val - 1];
1426 std::cout <<
"build Region Adjacency Graph Vertices" << std::endl;
1427 std::cout <<
"number of Vertices:" << numVert << std::endl;
1430 Tree_out = morphee::graph::CommonGraph32(numVert);
1432 BoostGraph Gout = morphee::graph::CommonGraph32(numVert);
1433 BoostGraph Gout_t = morphee::graph::CommonGraph32(numVert);
1434 BoostGraph Gtemp = morphee::graph::CommonGraph32(numVert);
1436 BoostGraph Tree_temp = morphee::graph::CommonGraph32(numVert);
1437 BoostGraph Tree_temp2 = morphee::graph::CommonGraph32(numVert);
1440 BoostGraph G_area = morphee::graph::CommonGraph32(numVert);
1441 morphee::morphoBase::t_NeighborhoodGraphFromMosaic(imWs, nl, G_area);
1444 ImageWs ImTempSurfaces = imWs.getSame();
1445 morphee::morphoBase::t_ImLabelFlatZonesWithArea(imWs, nl, ImTempSurfaces);
1446 morphee::graph::t_ProjectMarkersOnGraph(ImTempSurfaces, imWs, G_area);
1449 morphee::morphoBase::t_ImLabelFlatZonesWithVolume(imWs, imGrad, nl,
1451 morphee::graph::t_ProjectMarkersOnGraph(ImTempSurfaces, imWs, Gout);
1453 std::cout <<
"build Region Adjacency Graph Edges" << std::endl;
1456 double area_total = 0.0;
1459 for (boost::tie(v_it, v_end) = boost::vertices(Gout.getBoostGraph());
1460 v_it != v_end; ++v_it) {
1461 Gout.vertexData(*v_it, &vdata1);
1464 G_area.vertexData(*v_it, &vdata1);
1465 area_total = area_total + (double) vdata1;
1469 for (it = imWs.begin(), iend = imWs.end(); it != iend;
1472 o1 = it.getOffset();
1473 int val = imWs.pixelFromOffset(o1);
1476 neighb.setCenter(o1);
1478 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
1479 const offset_t o2 = nit.getOffset();
1480 int val2 = imWs.pixelFromOffset(o2);
1484 boost::tie(e1, in1) =
1485 boost::edge(val - 1, val2 - 1, Gout.getBoostGraph());
1486 boost::tie(e2, in1) =
1487 boost::edge(val - 1, val2 - 1, Gtemp.getBoostGraph());
1489 double val3 = imGrad.pixelFromOffset(o1);
1490 double val4 = imGrad.pixelFromOffset(o2);
1491 double maxi = std::max(val3, val4);
1495 Gout.addEdge(val - 1, val2 - 1, maxi);
1496 Gtemp.addEdge(val - 1, val2 - 1, 1);
1498 Gout.edgeWeight(e1, &tmp);
1499 Gout.setEdgeWeight(e1, tmp + maxi);
1501 Gtemp.edgeWeight(e2, &tmp2);
1502 Gtemp.setEdgeWeight(e2, tmp2 + 1);
1510 std::cout <<
"number of Edges : " << numEdges << std::endl;
1512 Gout_t = t_CopyGraph(Gout);
1513 boost::tie(ed_it2, ed_end2) = boost::edges(Gtemp.getBoostGraph());
1514 boost::tie(ed_it3, ed_end3) = boost::edges(G_area.getBoostGraph());
1515 boost::tie(ed_it4, ed_end4) = boost::edges(Gout_t.getBoostGraph());
1516 float current_max_value = 0.0f;
1519 for (boost::tie(ed_it, ed_end) = boost::edges(Gout.getBoostGraph());
1520 ed_it != ed_end, ed_it2 != ed_end2, ed_it3 != ed_end3,
1522 ++ed_it, ++ed_it2, ++ed_it3, ++ed_it4) {
1523 Gout.edgeWeight(*ed_it, &tmp);
1524 Gtemp.edgeWeight(*ed_it2, &tmp2);
1525 Gout_t.setEdgeWeight(*ed_it4, ((
double) tmp2));
1528 Gtemp = t_CopyGraph(Treein);
1530 for (boost::tie(ed_it, ed_end) = boost::edges(Gtemp.getBoostGraph());
1531 ed_it != ed_end; ++ed_it) {
1532 Gtemp.edgeWeight(*ed_it, &tmp);
1533 val_edges.push_back(tmp);
1536 std::vector<typename BoostGraph::EdgeDescriptor> removed_edges;
1538 Tree_out = t_CopyGraph(Gtemp);
1539 Tree_temp = t_CopyGraph(Gtemp);
1540 Tree_temp2 = t_CopyGraph(Gtemp);
1543 std::cout <<
"sort" << std::endl;
1544 std::sort(val_edges.begin(), val_edges.end(), std::less<double>());
1545 double last_edge_value = val_edges.back();
1546 double last_analyzed = last_edge_value;
1565 while (val_edges.size() > 1) {
1566 std::cout << last_edge_value << std::endl;
1569 for (boost::tie(ed_it, ed_end) = boost::edges(Gtemp.getBoostGraph());
1570 ed_it != ed_end; ++ed_it) {
1571 Gtemp.edgeWeight(*ed_it, &tmp);
1572 vs = Gtemp.edgeSource(*ed_it);
1573 vt = Gtemp.edgeTarget(*ed_it);
1574 boost::tie(e1, in1) = boost::edge(vs, vt, Tree_temp.getBoostGraph());
1576 if (tmp >= last_edge_value && in1) {
1577 removed_edges.push_back(e1);
1578 Tree_temp.removeEdge(vs, vt);
1583 int nb_of_connected;
1584 t_LabelConnectedComponent(Tree_temp, Tree_temp2, &nb_of_connected);
1586 std::cout <<
" number of connected components = " << nb_of_connected
1590 float mean_value = 0.0f;
1591 float min_value_b = 1000000000000.0f;
1592 float number_of_points = 0.0f;
1593 float volume1 = 0.0f;
1594 float volume2 = 0.0f;
1596 float volume11 = 0.0f;
1597 float volume22 = 0.0f;
1605 float mean_val_1 = 0.0f;
1606 float mean_val_2 = 0.0f;
1608 float nb_val_1 = 0.0f;
1609 float nb_val_2 = 0.0f;
1611 float mean_value_1 = 0.0f;
1612 float mean_value_2 = 0.0f;
1614 float number_of_points1 = 0.0f;
1615 float number_of_points2 = 0.0f;
1617 float dist_histo = 0.0f;
1621 while (removed_edges.size() > 0) {
1628 number_of_points = 0.0f;
1630 mean_value_1 = 0.0f;
1631 mean_value_2 = 0.0f;
1632 number_of_points1 = 0.0f;
1633 number_of_points2 = 0.0f;
1646 for (
int dim_allouee1 = 0; dim_allouee1 < 255; ++dim_allouee1) {
1647 histogram_region1[dim_allouee1] = 0;
1648 histogram_region2[dim_allouee1] = 0;
1649 cumul_histogram_region1[dim_allouee1] = 0;
1650 cumul_histogram_region2[dim_allouee1] = 0;
1653 e1 = removed_edges.back();
1654 removed_edges.pop_back();
1657 Tree_temp2.vertexData(Tree_temp2.edgeSource(e1), &label1);
1658 Tree_temp2.vertexData(Tree_temp2.edgeTarget(e1), &label2);
1660 boost::tie(ed_it4, ed_end4) = boost::edges(Gout_t.getBoostGraph());
1662 for (boost::tie(ed_it, ed_end) = boost::edges(Gout.getBoostGraph());
1663 ed_it != ed_end, ed_it4 != ed_end4; ++ed_it, ++ed_it4) {
1664 vs = Gout.edgeSource(*ed_it);
1665 vt = Gout.edgeTarget(*ed_it);
1668 Tree_temp2.vertexData(vs, &vdata1);
1669 Tree_temp2.vertexData(vt, &vdata2);
1672 if ((vdata1 == label1 && vdata2 == label2) ||
1673 (vdata1 == label2 && vdata2 == label1)) {
1674 Gout.edgeWeight(*ed_it, &tmp);
1675 Gout_t.edgeWeight(*ed_it4, &tmp2);
1677 mean_value = mean_value + (float) tmp;
1678 number_of_points = number_of_points + (float) tmp2;
1680 if (((
float) tmp / (float) tmp2) < min_value_b)
1681 min_value_b = ((
float) tmp / (float) tmp2);
1685 if ((vdata1 == label1 && vdata2 != label1) ||
1686 (vdata2 == label1 && vdata1 != label1)) {
1687 Gout.edgeWeight(*ed_it, &tmp);
1688 Gout_t.edgeWeight(*ed_it4, &tmp2);
1690 mean_value_1 = mean_value_1 + (float) tmp;
1691 number_of_points1 = number_of_points1 + (float) tmp2;
1695 if ((vdata1 == label2 && vdata2 != label2) ||
1696 (vdata2 == label2 && vdata1 != label2)) {
1697 Gout.edgeWeight(*ed_it, &tmp);
1698 Gout_t.edgeWeight(*ed_it4, &tmp2);
1700 mean_value_2 = mean_value_2 + (float) tmp;
1701 number_of_points2 = number_of_points2 + (float) tmp2;
1706 for (boost::tie(v_it, v_end) = boost::vertices(Gout.getBoostGraph());
1707 v_it != v_end; ++v_it) {
1709 Tree_temp2.vertexData(*v_it, &vdata1);
1710 Gout.vertexData(*v_it, &vdata11);
1712 Tree_out.setVertexData(*v_it, (
float) alpha1 * vdata11 +
1714 quad_error[(
int) *v_it]);
1717 if (vdata1 == label1) {
1723 area1 = area1 + (float) vdata11;
1724 mean_val_1 = mean_val_1 + mean_values[(int) *v_it];
1725 nb_val_1 = nb_val_1 + 1.0f;
1727 for (
int dim_allouee1 = 0; dim_allouee1 < 255; ++dim_allouee1) {
1728 histogram_region1[dim_allouee1] =
1729 histogram_region1[dim_allouee1] +
1733 if (vdata1 == label2) {
1739 area2 = area2 + (float) vdata11;
1740 mean_val_2 = mean_val_2 + mean_values[(int) *v_it];
1741 nb_val_2 = nb_val_2 + 1.0f;
1743 for (
int dim_allouee1 = 0; dim_allouee1 < 255; ++dim_allouee1) {
1744 histogram_region2[dim_allouee1] =
1745 histogram_region2[dim_allouee1] +
1751 for (
int dim_allouee1 = 0; dim_allouee1 < 255; ++dim_allouee1) {
1752 for (
int dim_allouee0 = 0; dim_allouee0 <= dim_allouee1;
1754 cumul_histogram_region1[dim_allouee1] =
1755 cumul_histogram_region1[dim_allouee1] +
1756 (float) histogram_region1[dim_allouee0] / (
float) area1;
1757 cumul_histogram_region2[dim_allouee1] =
1758 cumul_histogram_region2[dim_allouee1] +
1759 (float) histogram_region2[dim_allouee0] / (
float) area2;
1763 for (
int dim_allouee1 = 0; dim_allouee1 < 255; ++dim_allouee1) {
1765 dist_histo +
std::pow(cumul_histogram_region1[dim_allouee1] -
1766 cumul_histogram_region2[dim_allouee1],
1771 for (boost::tie(v_it, v_end) = boost::vertices(Gout.getBoostGraph());
1772 v_it != v_end; ++v_it) {
1774 Tree_temp2.vertexData(*v_it, &vdata1);
1777 if (vdata1 == label1) {
1779 (mean_values[(int) *v_it] - mean_val_1 / nb_val_1) *
1780 (mean_values[(int) *v_it] - mean_val_1 / nb_val_1);
1782 if (vdata1 == label2) {
1784 (mean_values[(int) *v_it] - mean_val_2 / nb_val_2) *
1785 (mean_values[(int) *v_it] - mean_val_2 / nb_val_2);
1789 quad1 = quad1 / nb_val_1;
1790 quad2 = quad2 / nb_val_2;
1795 volume11 = area1 * mean_value / number_of_points;
1796 volume22 = area2 * mean_value / number_of_points;
1801 float probability_volume =
1802 (1.0 -
std::pow(1.0 - volume1 / ((
double)
volume), (
int) alpha1) -
1806 float probability_area =
1808 std::pow(1.0 - area1 / ((
double) area_total), (
int) alpha1) -
1809 std::pow(1.0 - area2 / ((
double) area_total), (
int) alpha1) +
1810 std::pow(1.0 - (area1 + area2) / ((
double) area_total),
1813 float deterministic_area =
1814 std::min(area1, area2) / ((double) area_total);
1815 float deterministic_volume =
1816 std::min(volume11, volume22) / ((double)
volume);
1819 std::abs((mean_val_2 / nb_val_2) - (mean_val_1 / nb_val_1));
1820 float mean_gradient = mean_value / number_of_points;
1822 float mean_gradient_1 = mean_value_1 / number_of_points1;
1823 float mean_gradient_2 = mean_value_2 / number_of_points2;
1825 dist_histo = deterministic_area * dist_histo / 255.0f;
1828 boost::tie(e2, in1) =
1829 boost::edge(Tree_out.edgeSource(e1), Tree_out.edgeTarget(e1),
1830 Tree_out.getBoostGraph());
1832 if (number_of_points > 0.0f) {
1839 std::pow(((
float) mean_gradient / 65535.0f), (
float) alpha2) *
1840 std::pow(probability_volume, (
float) alpha3);
1845 value_test = std::min(value_test, 65535.0f);
1846 Tree_out.setEdgeWeight(e2, value_test);
1848 if (value_test > current_max_value)
1849 current_max_value = value_test;
1852 Tree_out.setEdgeWeight(e2, 0.0f);
1856 while (last_edge_value == last_analyzed) {
1857 last_edge_value = val_edges.back();
1858 val_edges.pop_back();
1860 last_analyzed = last_edge_value;
1864 std::cout <<
"current_max_value" << current_max_value << std::endl;
1869 void Addition(
const affine_par_morceaux &A,
const affine_par_morceaux &B,
1870 affine_par_morceaux &S,
int Pmax)
1872 affine_par_morceaux::const_reverse_iterator ia = A.rbegin(),
1874 affine_par_morceaux::const_reverse_iterator ib = B.rbegin(),
1877 while ((ia != eia) && (ib != eib) && (S.size() < Pmax)) {
1880 if ((*ia).x >= (*ib).x) {
1884 nouveau.y += (*ib).y + (*ib).p * ((*ia).x - (*ib).x);
1886 nouveau.p += (*ib).p;
1888 S.push_front(nouveau);
1891 if ((*ia).x == (*ib).x)
1898 nouveau.y += (*ia).y + (*ia).p * ((*ib).x - (*ia).x);
1899 nouveau.p += (*ia).p;
1900 S.push_front(nouveau);
1907 if (S.front().x > 0) {
1908 S.front().y -= S.front().p * S.front().x;
1914 float Inf_affine(affine_par_morceaux &A,
const morceau &m)
1917 affine_par_morceaux::iterator i = A.end(), ei = A.begin();
1922 if (m.p == (*i).p) {
1924 float y = m.y + m.p * ((*i).x - m.x);
1930 else if (y == (*i).y) {
1941 for (; i != ei; --i) {
1942 xi = (m.x * m.p - (*i).x * (*i).p - (m.y - (*i).y)) / (m.p - (*i).p);
1947 A.erase(++i, A.end());
1951 m1.y = m.y + m.p * (xi - m.x);
1958 template <
class ImageWs,
class ImageIn,
class ImageGrad,
class SE,
1960 RES_C t_ScaleSetHierarchyReweighting(
const ImageWs &imWs,
1961 const ImageIn &imIn,
1962 const ImageGrad &imGrad,
1963 const BoostGraph &Treein,
const SE &nl,
1964 BoostGraph &Tree_out)
1966 MORPHEE_ENTER_FUNCTION(
"t_ScaleSetHierarchyReweighting");
1969 if ((!imWs.isAllocated())) {
1970 MORPHEE_REGISTER_ERROR(
"Not allocated");
1971 return RES_NOT_ALLOCATED;
1974 if ((!imIn.isAllocated())) {
1975 MORPHEE_REGISTER_ERROR(
"Not allocated");
1976 return RES_NOT_ALLOCATED;
1979 if ((!imGrad.isAllocated())) {
1980 MORPHEE_REGISTER_ERROR(
"Not allocated");
1981 return RES_NOT_ALLOCATED;
1985 typename ImageWs::const_iterator it, iend;
1986 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
1987 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
1991 typename BoostGraph::EdgeIterator ed_it, ed_end;
1992 typename BoostGraph::EdgeIterator ed_it2, ed_end2;
1993 typename BoostGraph::EdgeIterator ed_it3, ed_end3;
1994 typename BoostGraph::EdgeIterator ed_it4, ed_end4;
1996 typename BoostGraph::VertexIterator v_it, v_end;
1998 typename BoostGraph::VertexProperty vdata1, vdata2, vdata11, vdata22;
1999 typename BoostGraph::VertexProperty label1, label2;
2004 typename BoostGraph::EdgeDescriptor e1, e2, ef;
2005 typename BoostGraph::VertexDescriptor vs, vt;
2006 typename BoostGraph::EdgeProperty tmp, tmp2, tmp3;
2008 std::vector<double> val_edges;
2009 val_edges.push_back(0.0);
2013 std::cout <<
"Compute number of regions" << std::endl;
2015 for (it = imWs.begin(), iend = imWs.end(); it != iend;
2018 o0 = it.getOffset();
2019 int val = imWs.pixelFromOffset(o0);
2021 if (val > numVert) {
2027 Tree_out = morphee::graph::CommonGraph32(numVert);
2028 Tree_out = t_CopyGraph(Treein);
2030 BoostGraph Tree_temp = morphee::graph::CommonGraph32(numVert);
2031 BoostGraph Tree_ordered = morphee::graph::CommonGraph32(numVert);
2032 BoostGraph Tree_temp2 = morphee::graph::CommonGraph32(numVert);
2033 BoostGraph Tree_old_label = morphee::graph::CommonGraph32(numVert);
2036 BoostGraph G_gradient = morphee::graph::CommonGraph32(numVert);
2038 BoostGraph G_blength = morphee::graph::CommonGraph32(numVert);
2040 std::cout <<
"1) Compute statistics of each regions : ... " << std::endl;
2041 std::vector<double> mean_values;
2042 std::vector<double> number_of_values;
2043 std::vector<double> quad_error;
2045 float x_star = 0.0f;
2046 float sum_gradient = 0.0f;
2047 float current_max_value = 0.0f;
2052 float *histogram_region1;
2053 histogram_region1 =
new float[256];
2055 float *histogram_region_merged_1;
2056 histogram_region_merged_1 =
new float[256];
2058 float *histogram_region_merged_2;
2059 histogram_region_merged_2 =
new float[256];
2061 for (
int dim_allouee1 = 0; dim_allouee1 < 256; ++dim_allouee1) {
2062 histogram_region1[dim_allouee1] = 0;
2063 histogram_region_merged_1[dim_allouee1] = 0;
2064 histogram_region_merged_2[dim_allouee1] = 0;
2067 for (
int dim_allouee0 = 0; dim_allouee0 < numVert; ++dim_allouee0) {
2071 for (
int dim_allouee0 = 0; dim_allouee0 < numVert; ++dim_allouee0) {
2072 for (
int dim_allouee1 = 0; dim_allouee1 < 256; ++dim_allouee1) {
2073 histogram[dim_allouee0][dim_allouee1] = 0;
2078 for (
int i = 0; i < numVert; i++) {
2079 mean_values.push_back(0);
2080 number_of_values.push_back(0);
2081 quad_error.push_back(0);
2085 for (it = imWs.begin(), iend = imWs.end(); it != iend;
2088 o1 = it.getOffset();
2089 int val = imWs.pixelFromOffset(o1);
2090 double val_image = imIn.pixelFromOffset(o1);
2092 mean_values[val - 1] = mean_values[val - 1] + val_image;
2093 number_of_values[val - 1] = number_of_values[val - 1] + 1.0;
2095 int vald = (int) (val_image);
2100 for (
int i = 0; i < numVert; i++) {
2101 mean_values[i] = mean_values[i] / number_of_values[i];
2105 float max_quad_val = 0.0f;
2106 for (it = imWs.begin(), iend = imWs.end(); it != iend;
2109 o1 = it.getOffset();
2110 int val = imWs.pixelFromOffset(o1);
2111 double val_image = imIn.pixelFromOffset(o1);
2114 Tree_out.setVertexData(val - 1, 0);
2117 quad_error[val - 1] =
2118 (double) quad_error[val - 1] +
2119 std::pow(std::abs(val_image - (
double) mean_values[val - 1]), 2);
2123 neighb.setCenter(o1);
2125 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
2126 const offset_t o2 = nit.getOffset();
2127 int val2 = imWs.pixelFromOffset(o2);
2131 boost::tie(e1, in1) =
2132 boost::edge(val - 1, val2 - 1, G_gradient.getBoostGraph());
2133 boost::tie(e2, in1) =
2134 boost::edge(val - 1, val2 - 1, G_blength.getBoostGraph());
2136 double val3 = imGrad.pixelFromOffset(o1);
2137 double val4 = imGrad.pixelFromOffset(o2);
2138 double maxi = std::max(val3, val4);
2140 sum_gradient + 1.0f / (1.0f + 100.0f * (maxi / 65535.0f) *
2145 G_gradient.addEdge(val - 1, val2 - 1,
2146 1.0f / (1.0f + 100.0f * (maxi / 65535.0f) *
2147 (maxi / 65535.0f)));
2148 G_blength.addEdge(val - 1, val2 - 1, 1);
2150 G_gradient.edgeWeight(e1, &tmp);
2151 G_gradient.setEdgeWeight(
2152 e1, tmp + 1.0f / (1.0f + 100.0f * (maxi / 65535.0f) *
2153 (maxi / 65535.0f)));
2155 G_blength.edgeWeight(e2, &tmp2);
2156 G_blength.setEdgeWeight(e2, tmp2 + 1);
2164 std::cout <<
"1) Compute statistics of each regions : done !"
2167 std::cout <<
"Number of of Tree Edges : " << numEdges << std::endl;
2169 std::cout <<
"2) Go through hierarchy in ascendant order ... "
2175 Tree_ordered = t_CopyGraph(Tree_out);
2177 boost::tie(ed_it2, ed_end2) = boost::edges(Tree_out.getBoostGraph());
2179 for (boost::tie(ed_it, ed_end) =
2180 boost::edges(Tree_ordered.getBoostGraph());
2181 ed_it != ed_end, ed_it2 != ed_end2; ++ed_it, ++ed_it2) {
2182 Tree_ordered.edgeWeight(*ed_it, &tmp);
2183 val_edges.push_back(tmp);
2184 Tree_out.setEdgeWeight(*ed_it2, 0);
2187 BoostGraph Tree_out2 = t_CopyGraph(Tree_out);
2190 Tree_temp = morphee::graph::CommonGraph32(numVert);
2192 std::vector<typename BoostGraph::EdgeDescriptor> added_edges;
2193 std::vector<typename BoostGraph::EdgeDescriptor> frontiers_edges;
2194 std::vector<typename BoostGraph::EdgeDescriptor> inside_edges;
2195 std::vector<typename BoostGraph::VertexDescriptor> merged_nodes1;
2196 std::vector<typename BoostGraph::VertexDescriptor> merged_nodes2;
2199 std::cout <<
"sort edges of tree" << std::endl;
2200 std::sort(val_edges.begin(), val_edges.end(), std::greater<double>());
2202 double last_edge_value = val_edges.back();
2203 double last_analyzed = last_edge_value;
2204 int current_label = numVert - 1;
2205 int number_of_connected_components = numVert;
2207 while (val_edges.size() > 0 || number_of_connected_components >= 2) {
2208 std::cout << last_edge_value << std::endl;
2210 int number_of_old_connected_components;
2211 t_LabelConnectedComponent(Tree_temp, Tree_old_label,
2212 &number_of_old_connected_components);
2215 for (boost::tie(ed_it, ed_end) =
2216 boost::edges(Tree_ordered.getBoostGraph());
2217 ed_it != ed_end; ++ed_it) {
2218 Tree_ordered.edgeWeight(*ed_it, &tmp);
2220 if (tmp == last_edge_value) {
2222 vs = Tree_ordered.edgeSource(*ed_it);
2223 vt = Tree_ordered.edgeTarget(*ed_it);
2224 boost::tie(e1, in1) =
2225 boost::add_edge(vs, vt, Tree_temp.getBoostGraph());
2226 added_edges.push_back(e1);
2231 t_LabelConnectedComponent(Tree_temp, Tree_temp2,
2232 &number_of_connected_components);
2233 std::cout <<
"number_of_connected_components = "
2234 << number_of_connected_components << std::endl;
2238 while (added_edges.size() > 0) {
2240 float int_value_1 = 0.0f;
2241 float number_of_bpoints1 = 0.0f;
2242 float mean_value_in1 = 0.0f;
2243 float quad_error_in1 = 0.0f;
2244 float nb_val_in1 = 0.0f;
2248 int merged_region_1 = 0;
2249 float int_value_merged_region_1 = 0.0f;
2250 float number_of_bpoints_merged_region_1 = 0.0f;
2251 float mean_value_in_merged_region_1 = 0.0f;
2252 float quad_error_in_merged_region_1 = 0.0f;
2253 float nb_val_in_merged_region_1 = 0.0f;
2255 int merged_region_2 = 0;
2256 float int_value_merged_region_2 = 0.0f;
2257 float number_of_bpoints_merged_region_2 = 0.0f;
2258 float mean_value_in_merged_region_2 = 0.0f;
2259 float quad_error_in_merged_region_2 = 0.0f;
2260 float nb_val_in_merged_region_2 = 0.0f;
2263 e1 = added_edges.back();
2264 added_edges.pop_back();
2267 Tree_temp2.vertexData(Tree_ordered.edgeSource(e1), &label1);
2268 Tree_temp2.vertexData(Tree_ordered.edgeTarget(e1), &label2);
2272 Tree_old_label.vertexData(Tree_ordered.edgeSource(e1), &vdata1);
2273 merged_region_1 = (int) vdata1;
2275 Tree_old_label.vertexData(Tree_ordered.edgeTarget(e1), &vdata2);
2276 merged_region_2 = (int) vdata2;
2279 boost::tie(ed_it2, ed_end2) =
2280 boost::edges(G_gradient.getBoostGraph());
2282 for (boost::tie(ed_it, ed_end) =
2283 boost::edges(G_blength.getBoostGraph());
2284 ed_it != ed_end, ed_it2 != ed_end2; ++ed_it, ++ed_it2) {
2285 vs = G_blength.edgeSource(*ed_it);
2286 vt = G_blength.edgeTarget(*ed_it);
2289 Tree_temp2.vertexData(vs, &vdata1);
2290 Tree_temp2.vertexData(vt, &vdata2);
2293 Tree_old_label.vertexData(vs, &vdata11);
2294 Tree_old_label.vertexData(vt, &vdata22);
2296 if ((vdata1 == label1 && vdata2 == label1))
2297 inside_edges.push_back(*ed_it);
2299 if ((vdata11 == merged_region_1 && vdata22 != merged_region_1 &&
2300 vdata22 != merged_region_2) ||
2301 (vdata22 == merged_region_1 && vdata11 != merged_region_1 &&
2302 vdata11 != merged_region_2)) {
2303 frontiers_edges.push_back(*ed_it);
2307 if ((vdata1 == label1 && vdata2 != label1) ||
2308 (vdata2 == label1 && vdata1 != label1)) {
2309 G_gradient.edgeWeight(*ed_it2, &tmp);
2310 G_blength.edgeWeight(*ed_it, &tmp2);
2311 int_value_1 = int_value_1 + (float) tmp;
2312 number_of_bpoints1 = number_of_bpoints1 + (float) tmp2;
2317 if ((vdata11 == merged_region_1 && vdata22 != merged_region_1) ||
2318 (vdata22 == merged_region_1 && vdata11 != merged_region_1)) {
2319 G_gradient.edgeWeight(*ed_it2, &tmp);
2320 G_blength.edgeWeight(*ed_it, &tmp2);
2321 int_value_merged_region_1 =
2322 int_value_merged_region_1 + (float) tmp;
2323 number_of_bpoints_merged_region_1 =
2324 number_of_bpoints_merged_region_1 + (float) tmp2;
2328 if ((vdata11 == merged_region_2 && vdata22 != merged_region_2) ||
2329 (vdata22 == merged_region_2 && vdata11 != merged_region_2)) {
2330 G_gradient.edgeWeight(*ed_it2, &tmp);
2331 G_blength.edgeWeight(*ed_it, &tmp2);
2332 int_value_merged_region_2 =
2333 int_value_merged_region_2 + (float) tmp;
2334 number_of_bpoints_merged_region_2 =
2335 number_of_bpoints_merged_region_2 + (float) tmp2;
2340 for (boost::tie(v_it, v_end) =
2341 boost::vertices(G_blength.getBoostGraph());
2342 v_it != v_end; ++v_it) {
2344 Tree_temp2.vertexData(*v_it, &vdata1);
2345 Tree_old_label.vertexData(*v_it, &vdata11);
2348 if (vdata1 == label1) {
2350 for (
int dim_allouee1 = 0; dim_allouee1 < 256; ++dim_allouee1) {
2351 histogram_region1[dim_allouee1] =
2352 histogram_region1[dim_allouee1] +
2356 if (vdata11 == merged_region_1) {
2357 merged_nodes1.push_back(*v_it);
2360 for (
int dim_allouee1 = 0; dim_allouee1 < 256;
2362 histogram_region_merged_1[dim_allouee1] =
2363 histogram_region_merged_1[dim_allouee1] +
2368 if (vdata11 == merged_region_2) {
2369 merged_nodes2.push_back(*v_it);
2372 for (
int dim_allouee1 = 0; dim_allouee1 < 256;
2374 histogram_region_merged_2[dim_allouee1] =
2375 histogram_region_merged_2[dim_allouee1] +
2382 for (
int dim_allouee1 = 0; dim_allouee1 < 256; ++dim_allouee1) {
2384 mean_value_in1 + (float) dim_allouee1 *
2385 (
float) histogram_region1[dim_allouee1];
2386 nb_val_in1 = nb_val_in1 + (float) histogram_region1[dim_allouee1];
2388 mean_value_in_merged_region_1 =
2389 mean_value_in_merged_region_1 +
2390 (float) dim_allouee1 *
2391 (
float) histogram_region_merged_1[dim_allouee1];
2392 nb_val_in_merged_region_1 =
2393 nb_val_in_merged_region_1 +
2394 (float) histogram_region_merged_1[dim_allouee1];
2396 mean_value_in_merged_region_2 =
2397 mean_value_in_merged_region_2 +
2398 (float) dim_allouee1 *
2399 (
float) histogram_region_merged_2[dim_allouee1];
2400 nb_val_in_merged_region_2 =
2401 nb_val_in_merged_region_2 +
2402 (float) histogram_region_merged_2[dim_allouee1];
2406 mean_value_in1 = mean_value_in1 / nb_val_in1;
2407 mean_value_in_merged_region_1 =
2408 mean_value_in_merged_region_1 / nb_val_in_merged_region_1;
2409 mean_value_in_merged_region_2 =
2410 mean_value_in_merged_region_2 / nb_val_in_merged_region_2;
2412 for (
int dim_allouee1 = 0; dim_allouee1 < 256; ++dim_allouee1) {
2414 quad_error_in1 + (float) histogram_region1[dim_allouee1] *
2415 ((
float) dim_allouee1 - mean_value_in1) *
2416 ((
float) dim_allouee1 - mean_value_in1);
2417 histogram_region1[dim_allouee1] = 0;
2419 quad_error_in_merged_region_1 =
2420 quad_error_in_merged_region_1 +
2421 (float) histogram_region_merged_1[dim_allouee1] *
2422 ((
float) dim_allouee1 - mean_value_in_merged_region_1) *
2423 ((
float) dim_allouee1 - mean_value_in_merged_region_1);
2424 histogram_region_merged_1[dim_allouee1] = 0;
2426 quad_error_in_merged_region_2 =
2427 quad_error_in_merged_region_2 +
2428 (float) histogram_region_merged_2[dim_allouee1] *
2429 ((
float) dim_allouee1 - mean_value_in_merged_region_2) *
2430 ((
float) dim_allouee1 - mean_value_in_merged_region_2);
2431 histogram_region_merged_2[dim_allouee1] = 0;
2451 quad_error_in1 = quad_error_in1 / 100.0f;
2452 quad_error_in_merged_region_1 =
2453 quad_error_in_merged_region_1 / 100.0f;
2454 quad_error_in_merged_region_2 =
2455 quad_error_in_merged_region_2 / 100.0f;
2458 float D1 = quad_error_in1;
2465 float DS1 = quad_error_in_merged_region_1;
2467 int_value_merged_region_1 +
2468 number_of_bpoints_merged_region_1;
2473 float DS2 = quad_error_in_merged_region_2;
2475 int_value_merged_region_2 +
2476 number_of_bpoints_merged_region_2;
2486 std::max(1000.0f * -(DS1 + DS2 - D1) / (CS1 + CS2 - C1), 0.0f);
2489 100.0f * (D1 + C1 * (-(DS1 + DS2 - D1) / (CS1 + CS2 - C1)));
2490 float lambda_star = 10000.0f * valk;
2533 std::cout << lambda_star << std::endl;
2543 while (frontiers_edges.size() > 0) {
2544 ef = frontiers_edges.back();
2545 frontiers_edges.pop_back();
2547 boost::tie(e1, in1) = boost::edge(G_blength.edgeSource(ef),
2548 G_blength.edgeTarget(ef),
2549 Tree_out2.getBoostGraph());
2552 Tree_out2.edgeWeight(e1, &tmp);
2554 Tree_out2.setEdgeWeight(e1, lambda_star);
2556 Tree_out2.setEdgeWeight(
2557 e1, std::max((
float) lambda_star, (
float) tmp));
2563 while (inside_edges.size() > 0) {
2564 ef = inside_edges.back();
2565 inside_edges.pop_back();
2567 boost::tie(e1, in1) = boost::edge(G_blength.edgeSource(ef),
2568 G_blength.edgeTarget(ef),
2569 Tree_out2.getBoostGraph());
2572 Tree_out2.edgeWeight(e1, &tmp);
2573 if (tmp > lambda_star)
2574 Tree_out2.setEdgeWeight(e1, lambda_star);
2578 inside_edges.clear();
2579 frontiers_edges.clear();
2580 merged_nodes1.clear();
2581 merged_nodes2.clear();
2583 for (
int dim_allouee1 = 0; dim_allouee1 < 256; ++dim_allouee1) {
2584 histogram_region1[dim_allouee1] = 0;
2585 histogram_region_merged_1[dim_allouee1] = 0;
2586 histogram_region_merged_2[dim_allouee1] = 0;
2593 while (last_edge_value == last_analyzed) {
2594 last_edge_value = val_edges.back();
2595 val_edges.pop_back();
2597 last_analyzed = last_edge_value;
2600 std::cout <<
"current_max_value = " << current_max_value << std::endl;
2602 morphee::Morpho_Graph::t_Order_Edges_Weights(Tree_out2, Tree_out);
2604 for (boost::tie(ed_it, ed_end) = boost::edges(Tree_out.getBoostGraph());
2605 ed_it != ed_end; ++ed_it) {
2606 Tree_out.edgeWeight(*ed_it, &tmp);
2607 if (tmp > current_max_value)
2608 current_max_value = tmp;
2611 for (boost::tie(ed_it, ed_end) = boost::edges(Tree_out.getBoostGraph());
2612 ed_it != ed_end; ++ed_it) {
2613 Tree_out.edgeWeight(*ed_it, &tmp);
2614 float value_test = 65535.0f * ((float) tmp / current_max_value);
2615 Tree_out.setEdgeWeight(*ed_it, value_test);
2636 template <
class ImageWs,
class ImageIn,
class ImageGrad,
typename _alpha1,
2637 class SE,
class BoostGraph>
2638 RES_C t_MSMinCutInHierarchy(
const ImageWs &imWs,
const ImageIn &imIn,
2639 const ImageGrad &imGrad,
const _alpha1 alpha1,
2640 const BoostGraph &Treein,
const SE &nl,
2641 BoostGraph &Tree_out)
2643 MORPHEE_ENTER_FUNCTION(
"t_MSMinCutInHierarchy");
2646 if ((!imWs.isAllocated())) {
2647 MORPHEE_REGISTER_ERROR(
"Not allocated");
2648 return RES_NOT_ALLOCATED;
2651 if ((!imIn.isAllocated())) {
2652 MORPHEE_REGISTER_ERROR(
"Not allocated");
2653 return RES_NOT_ALLOCATED;
2656 if ((!imGrad.isAllocated())) {
2657 MORPHEE_REGISTER_ERROR(
"Not allocated");
2658 return RES_NOT_ALLOCATED;
2662 typename ImageWs::const_iterator it, iend;
2663 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
2664 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
2668 typename BoostGraph::EdgeIterator ed_it, ed_end;
2669 typename BoostGraph::EdgeIterator ed_it2, ed_end2;
2670 typename BoostGraph::EdgeIterator ed_it3, ed_end3;
2671 typename BoostGraph::EdgeIterator ed_it4, ed_end4;
2673 typename BoostGraph::VertexIterator v_it, v_end;
2675 typename BoostGraph::VertexProperty vdata1, vdata2, vdata11, vdata22;
2676 typename BoostGraph::VertexProperty label1, label2;
2681 typename BoostGraph::EdgeDescriptor e1, e2;
2682 typename BoostGraph::VertexDescriptor vs, vt;
2683 typename BoostGraph::EdgeProperty tmp, tmp2, tmp3;
2685 std::vector<double> val_edges;
2686 val_edges.push_back(0.0);
2688 float lambda = (float) alpha1;
2690 std::cout <<
"Compute number of regions" << std::endl;
2692 for (it = imWs.begin(), iend = imWs.end(); it != iend;
2695 o0 = it.getOffset();
2696 int val = imWs.pixelFromOffset(o0);
2698 if (val > numVert) {
2703 INT32 imsize = imWs.getXSize() * imWs.getYSize() * imWs.getZSize();
2704 F_DOUBLE var_norm_factor =
2705 1. / (
static_cast<F_DOUBLE
>(imsize) * 256. * 256.);
2708 Tree_out = morphee::graph::CommonGraph32(numVert);
2709 Tree_out = t_CopyGraph(Treein);
2711 BoostGraph Tree_temp = morphee::graph::CommonGraph32(numVert);
2712 BoostGraph Tree_ordered = morphee::graph::CommonGraph32(numVert);
2713 BoostGraph Tree_temp2 = morphee::graph::CommonGraph32(numVert);
2714 BoostGraph Tree_old_label = morphee::graph::CommonGraph32(numVert);
2717 BoostGraph G_gradient = morphee::graph::CommonGraph32(numVert);
2719 BoostGraph G_blength = morphee::graph::CommonGraph32(numVert);
2721 std::cout <<
"1) Compute statistics of each regions : ... " << std::endl;
2722 std::vector<double> mean_values;
2723 std::vector<double> number_of_values;
2724 std::vector<double> quad_error;
2726 float x_star = 0.0f;
2727 float sum_gradient = 0.0f;
2732 float *histogram_region1;
2733 histogram_region1 =
new float[256];
2735 float *histogram_region_merged_1;
2736 histogram_region_merged_1 =
new float[256];
2738 float *histogram_region_merged_2;
2739 histogram_region_merged_2 =
new float[256];
2741 for (
int dim_allouee1 = 0; dim_allouee1 < 256; ++dim_allouee1) {
2742 histogram_region1[dim_allouee1] = 0;
2743 histogram_region_merged_1[dim_allouee1] = 0;
2744 histogram_region_merged_2[dim_allouee1] = 0;
2747 for (
int dim_allouee0 = 0; dim_allouee0 < numVert; ++dim_allouee0) {
2751 for (
int dim_allouee0 = 0; dim_allouee0 < numVert; ++dim_allouee0) {
2752 for (
int dim_allouee1 = 0; dim_allouee1 < 256; ++dim_allouee1) {
2753 histogram[dim_allouee0][dim_allouee1] = 0;
2758 for (
int i = 0; i < numVert; i++) {
2759 mean_values.push_back(0);
2760 number_of_values.push_back(0);
2761 quad_error.push_back(0);
2765 for (it = imWs.begin(), iend = imWs.end(); it != iend;
2768 o1 = it.getOffset();
2769 int val = imWs.pixelFromOffset(o1);
2770 double val_image = imIn.pixelFromOffset(o1);
2772 mean_values[val - 1] = mean_values[val - 1] + val_image;
2773 number_of_values[val - 1] = number_of_values[val - 1] + 1.0;
2775 int vald = (int) (val_image);
2780 for (
int i = 0; i < numVert; i++) {
2781 mean_values[i] = mean_values[i] / number_of_values[i];
2785 float max_quad_val = 0.0f;
2786 int total_length = 0;
2787 for (it = imWs.begin(), iend = imWs.end(); it != iend;
2790 o1 = it.getOffset();
2791 int val = imWs.pixelFromOffset(o1);
2792 double val_image = imIn.pixelFromOffset(o1);
2795 Tree_out.setVertexData(val - 1, val);
2798 quad_error[val - 1] =
2799 (double) quad_error[val - 1] +
2800 std::pow(std::abs(val_image - (
double) mean_values[val - 1]), 2);
2804 neighb.setCenter(o1);
2806 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
2807 const offset_t o2 = nit.getOffset();
2808 int val2 = imWs.pixelFromOffset(o2);
2812 boost::tie(e1, in1) =
2813 boost::edge(val - 1, val2 - 1, G_gradient.getBoostGraph());
2814 boost::tie(e2, in1) =
2815 boost::edge(val - 1, val2 - 1, G_blength.getBoostGraph());
2817 double val3 = imGrad.pixelFromOffset(o1);
2818 double val4 = imGrad.pixelFromOffset(o2);
2819 double maxi = std::max(val3, val4);
2821 sum_gradient + 1.0f / (1.0f + 100.0f * (maxi / 65535.0f) *
2826 G_gradient.addEdge(val - 1, val2 - 1,
2827 1.0f / (1.0f + 100.0f * (maxi / 65535.0f) *
2828 (maxi / 65535.0f)));
2829 G_blength.addEdge(val - 1, val2 - 1, 1);
2832 G_gradient.edgeWeight(e1, &tmp);
2833 G_gradient.setEdgeWeight(
2834 e1, tmp + 1.0f / (1.0f + 100.0f * (maxi / 65535.0f) *
2835 (maxi / 65535.0f)));
2837 G_blength.edgeWeight(e2, &tmp2);
2838 G_blength.setEdgeWeight(e2, tmp2 + 1);
2846 F_DOUBLE length_norm_factor = 1. / total_length;
2847 std::cout <<
"length_factor = " << length_norm_factor <<
"\n";
2849 std::cout <<
"1) Compute statistics of each regions : done !"
2852 std::cout <<
"Number of of Tree Edges : " << numEdges << std::endl;
2854 std::cout <<
"2) Go through hierarchy in ascendant order ... "
2858 morphee::Morpho_Graph::t_Order_Edges_Weights(Treein, Tree_ordered);
2860 boost::tie(ed_it2, ed_end2) = boost::edges(Tree_out.getBoostGraph());
2862 for (boost::tie(ed_it, ed_end) =
2863 boost::edges(Tree_ordered.getBoostGraph());
2864 ed_it != ed_end, ed_it2 != ed_end2; ++ed_it, ++ed_it2) {
2865 Tree_ordered.edgeWeight(*ed_it, &tmp);
2866 val_edges.push_back(tmp);
2867 Tree_out.setEdgeWeight(*ed_it2, 0);
2871 Tree_temp = morphee::graph::CommonGraph32(numVert);
2873 std::vector<typename BoostGraph::EdgeDescriptor> added_edges;
2874 std::vector<typename BoostGraph::VertexDescriptor> merged_nodes1;
2875 std::vector<typename BoostGraph::VertexDescriptor> merged_nodes2;
2878 std::cout <<
"sort edges of tree" << std::endl;
2879 std::sort(val_edges.begin(), val_edges.end(), std::greater<double>());
2881 double last_edge_value = val_edges.back();
2882 double last_analyzed = last_edge_value;
2883 int current_label = numVert - 1;
2884 int number_of_connected_components = numVert;
2886 while (val_edges.size() > 0 || number_of_connected_components >= 2) {
2889 int number_of_old_connected_components;
2890 t_LabelConnectedComponent(Tree_temp, Tree_old_label,
2891 &number_of_old_connected_components);
2894 for (boost::tie(ed_it, ed_end) =
2895 boost::edges(Tree_ordered.getBoostGraph());
2896 ed_it != ed_end; ++ed_it) {
2897 Tree_ordered.edgeWeight(*ed_it, &tmp);
2899 if (tmp == last_edge_value) {
2901 vs = Tree_ordered.edgeSource(*ed_it);
2902 vt = Tree_ordered.edgeTarget(*ed_it);
2903 boost::tie(e1, in1) =
2904 boost::add_edge(vs, vt, Tree_temp.getBoostGraph());
2905 added_edges.push_back(e1);
2910 t_LabelConnectedComponent(Tree_temp, Tree_temp2,
2911 &number_of_connected_components);
2917 while (added_edges.size() > 0) {
2919 float int_value_1 = 0.0f;
2920 float number_of_bpoints1 = 0.0f;
2921 float mean_value_in1 = 0.0f;
2922 float quad_error_in1 = 0.0f;
2923 float nb_val_in1 = 0.0f;
2927 int merged_region_1 = 0;
2928 float int_value_merged_region_1 = 0.0f;
2929 float number_of_bpoints_merged_region_1 = 0.0f;
2930 float mean_value_in_merged_region_1 = 0.0f;
2931 float quad_error_in_merged_region_1 = 0.0f;
2932 float nb_val_in_merged_region_1 = 0.0f;
2934 int merged_region_2 = 0;
2935 float int_value_merged_region_2 = 0.0f;
2936 float number_of_bpoints_merged_region_2 = 0.0f;
2937 float mean_value_in_merged_region_2 = 0.0f;
2938 float quad_error_in_merged_region_2 = 0.0f;
2939 float nb_val_in_merged_region_2 = 0.0f;
2942 e1 = added_edges.back();
2943 added_edges.pop_back();
2946 Tree_temp2.vertexData(Tree_ordered.edgeSource(e1), &label1);
2947 Tree_temp2.vertexData(Tree_ordered.edgeTarget(e1), &label2);
2951 Tree_old_label.vertexData(Tree_ordered.edgeSource(e1), &vdata1);
2952 merged_region_1 = (int) vdata1;
2954 Tree_old_label.vertexData(Tree_ordered.edgeTarget(e1), &vdata2);
2955 merged_region_2 = (int) vdata2;
2958 boost::tie(ed_it2, ed_end2) =
2959 boost::edges(G_gradient.getBoostGraph());
2961 for (boost::tie(ed_it, ed_end) =
2962 boost::edges(G_blength.getBoostGraph());
2963 ed_it != ed_end, ed_it2 != ed_end2; ++ed_it, ++ed_it2) {
2964 vs = G_blength.edgeSource(*ed_it);
2965 vt = G_blength.edgeTarget(*ed_it);
2968 Tree_temp2.vertexData(vs, &vdata1);
2969 Tree_temp2.vertexData(vt, &vdata2);
2972 Tree_old_label.vertexData(vs, &vdata11);
2973 Tree_old_label.vertexData(vt, &vdata22);
2976 if ((vdata1 == label1 && vdata2 != label1) ||
2977 (vdata2 == label1 && vdata1 != label1)) {
2978 G_gradient.edgeWeight(*ed_it2, &tmp);
2979 G_blength.edgeWeight(*ed_it, &tmp2);
2980 int_value_1 = int_value_1 + (float) tmp;
2981 number_of_bpoints1 = number_of_bpoints1 + (float) tmp2;
2985 if ((vdata11 == merged_region_1 && vdata22 != merged_region_1) ||
2986 (vdata22 == merged_region_1 && vdata11 != merged_region_1)) {
2987 G_gradient.edgeWeight(*ed_it2, &tmp);
2988 G_blength.edgeWeight(*ed_it, &tmp2);
2989 int_value_merged_region_1 =
2990 int_value_merged_region_1 + (float) tmp;
2991 number_of_bpoints_merged_region_1 =
2992 number_of_bpoints_merged_region_1 + (float) tmp2;
2996 if ((vdata11 == merged_region_2 && vdata22 != merged_region_2) ||
2997 (vdata22 == merged_region_2 && vdata11 != merged_region_2)) {
2998 G_gradient.edgeWeight(*ed_it2, &tmp);
2999 G_blength.edgeWeight(*ed_it, &tmp2);
3000 int_value_merged_region_2 =
3001 int_value_merged_region_2 + (float) tmp;
3002 number_of_bpoints_merged_region_2 =
3003 number_of_bpoints_merged_region_2 + (float) tmp2;
3008 for (boost::tie(v_it, v_end) =
3009 boost::vertices(G_blength.getBoostGraph());
3010 v_it != v_end; ++v_it) {
3012 Tree_temp2.vertexData(*v_it, &vdata1);
3013 Tree_old_label.vertexData(*v_it, &vdata11);
3016 if (vdata1 == label1) {
3018 for (
int dim_allouee1 = 0; dim_allouee1 < 256; ++dim_allouee1) {
3019 histogram_region1[dim_allouee1] =
3020 histogram_region1[dim_allouee1] +
3024 if (vdata11 == merged_region_1) {
3025 merged_nodes1.push_back(*v_it);
3028 for (
int dim_allouee1 = 0; dim_allouee1 < 256;
3030 histogram_region_merged_1[dim_allouee1] =
3031 histogram_region_merged_1[dim_allouee1] +
3036 if (vdata11 == merged_region_2) {
3037 merged_nodes2.push_back(*v_it);
3040 for (
int dim_allouee1 = 0; dim_allouee1 < 256;
3042 histogram_region_merged_2[dim_allouee1] =
3043 histogram_region_merged_2[dim_allouee1] +
3050 for (
int dim_allouee1 = 0; dim_allouee1 < 256; ++dim_allouee1) {
3052 mean_value_in1 + (float) dim_allouee1 *
3053 (
float) histogram_region1[dim_allouee1];
3054 nb_val_in1 = nb_val_in1 + (float) histogram_region1[dim_allouee1];
3056 mean_value_in_merged_region_1 =
3057 mean_value_in_merged_region_1 +
3058 (float) dim_allouee1 *
3059 (
float) histogram_region_merged_1[dim_allouee1];
3060 nb_val_in_merged_region_1 =
3061 nb_val_in_merged_region_1 +
3062 (float) histogram_region_merged_1[dim_allouee1];
3064 mean_value_in_merged_region_2 =
3065 mean_value_in_merged_region_2 +
3066 (float) dim_allouee1 *
3067 (
float) histogram_region_merged_2[dim_allouee1];
3068 nb_val_in_merged_region_2 =
3069 nb_val_in_merged_region_2 +
3070 (float) histogram_region_merged_2[dim_allouee1];
3074 mean_value_in1 = mean_value_in1 / nb_val_in1;
3075 mean_value_in_merged_region_1 =
3076 mean_value_in_merged_region_1 / nb_val_in_merged_region_1;
3077 mean_value_in_merged_region_2 =
3078 mean_value_in_merged_region_2 / nb_val_in_merged_region_2;
3080 for (
int dim_allouee1 = 0; dim_allouee1 < 256; ++dim_allouee1) {
3082 quad_error_in1 + (float) histogram_region1[dim_allouee1] *
3083 ((
float) dim_allouee1 - mean_value_in1) *
3084 ((
float) dim_allouee1 - mean_value_in1);
3085 histogram_region1[dim_allouee1] = 0;
3087 quad_error_in_merged_region_1 =
3088 quad_error_in_merged_region_1 +
3089 (float) histogram_region_merged_1[dim_allouee1] *
3090 ((
float) dim_allouee1 - mean_value_in_merged_region_1) *
3091 ((
float) dim_allouee1 - mean_value_in_merged_region_1);
3092 histogram_region_merged_1[dim_allouee1] = 0;
3094 quad_error_in_merged_region_2 =
3095 quad_error_in_merged_region_2 +
3096 (float) histogram_region_merged_2[dim_allouee1] *
3097 ((
float) dim_allouee1 - mean_value_in_merged_region_2) *
3098 ((
float) dim_allouee1 - mean_value_in_merged_region_2);
3099 histogram_region_merged_2[dim_allouee1] = 0;
3102 quad_error_in1 = quad_error_in1 / 100.0f;
3103 quad_error_in_merged_region_1 =
3104 quad_error_in_merged_region_1 / 100.0f;
3105 quad_error_in_merged_region_2 =
3106 quad_error_in_merged_region_2 / 100.0f;
3109 float D1 = quad_error_in1;
3110 float C1 = number_of_bpoints1 +
3113 float E1 = D1 + lambda * C1;
3116 float DS1 = quad_error_in_merged_region_1;
3118 number_of_bpoints_merged_region_1 +
3119 int_value_merged_region_1;
3122 float ES1 = DS1 + lambda * CS1;
3124 float DS2 = quad_error_in_merged_region_2;
3126 number_of_bpoints_merged_region_2 +
3127 int_value_merged_region_2;
3130 float ES2 = DS2 + lambda * CS2;
3133 float beta = 0.0005;
3135 D1 = quad_error_in1 * var_norm_factor;
3136 C1 = (number_of_bpoints1 + int_value_1) *
3140 E1 = D1 + lambda * C1;
3143 quad_error_in_merged_region_1 * var_norm_factor;
3144 CS1 = (number_of_bpoints_merged_region_1 +
3145 int_value_merged_region_1) *
3149 ES1 = DS1 + lambda * CS1;
3152 quad_error_in_merged_region_2 * var_norm_factor;
3153 CS2 = (number_of_bpoints_merged_region_2 +
3154 int_value_merged_region_2) *
3158 ES2 = DS2 + lambda * CS2;
3165 if (E1 < ES1 + ES2) {
3184 while (merged_nodes1.size() > 0) {
3185 vs = merged_nodes1.back();
3186 merged_nodes1.pop_back();
3187 Tree_out.vertexData(vs, &vdata1);
3188 Tree_out.setVertexData(vs, current_label);
3191 while (merged_nodes2.size() > 0) {
3192 vs = merged_nodes2.back();
3193 merged_nodes2.pop_back();
3194 Tree_out.vertexData(vs, &vdata1);
3195 Tree_out.setVertexData(vs, current_label);
3200 boost::tie(e1, in1) = boost::edge(Tree_ordered.edgeSource(e1),
3201 Tree_ordered.edgeTarget(e1),
3202 Tree_out.getBoostGraph());
3203 Tree_out.setEdgeWeight(e1, E1 - (ES1 + ES2));
3206 merged_nodes1.clear();
3207 merged_nodes2.clear();
3209 for (
int dim_allouee1 = 0; dim_allouee1 < 256; ++dim_allouee1) {
3210 histogram_region1[dim_allouee1] = 0;
3211 histogram_region_merged_1[dim_allouee1] = 0;
3212 histogram_region_merged_2[dim_allouee1] = 0;
3219 while (last_edge_value == last_analyzed) {
3220 last_edge_value = val_edges.back();
3221 val_edges.pop_back();
3223 last_analyzed = last_edge_value;
3229 template <
class ImageIn,
class ImageGrad,
class SE,
class BoostGraph>
3230 RES_C t_AverageLinkageTree_minimean(
const ImageIn &imIn,
3231 const ImageGrad &imGrad,
const SE &nl,
3234 MORPHEE_ENTER_FUNCTION(
"t_AverageLinkageTree");
3236 if ((!imIn.isAllocated())) {
3237 MORPHEE_REGISTER_ERROR(
"Not allocated");
3238 return RES_NOT_ALLOCATED;
3241 if ((!imGrad.isAllocated())) {
3242 MORPHEE_REGISTER_ERROR(
"Not allocated");
3243 return RES_NOT_ALLOCATED;
3247 typename ImageIn::const_iterator it, iend;
3248 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
3249 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
3253 typename BoostGraph::EdgeIterator ed_it, ed_end;
3254 typename BoostGraph::EdgeIterator ed_it2, ed_end2;
3255 typename BoostGraph::EdgeIterator ed_it3, ed_end3;
3257 typename BoostGraph::VertexProperty vdata1, vdata2;
3258 typename BoostGraph::VertexDescriptor v1_remove, v2_remove;
3259 typename BoostGraph::VertexProperty label1, label2;
3264 typename BoostGraph::EdgeDescriptor e1, e2, e3;
3265 typename BoostGraph::EdgeProperty tmp, tmp2, tmp3, tmp4;
3267 std::cout <<
"build Region Adjacency Graph" << std::endl;
3269 for (it = imIn.begin(), iend = imIn.end(); it != iend;
3272 o0 = it.getOffset();
3273 int val = imIn.pixelFromOffset(o0);
3275 if (val > numVert) {
3280 std::cout <<
"build Region Adjacency Graph Vertices" << std::endl;
3281 std::cout <<
"number of Vertices:" << numVert << std::endl;
3283 Tout = morphee::graph::CommonGraph32(numVert);
3284 BoostGraph Tout_label = morphee::graph::CommonGraph32(numVert);
3285 BoostGraph Tout_current = morphee::graph::CommonGraph32(numVert);
3286 BoostGraph Tout_current_temp = morphee::graph::CommonGraph32(numVert);
3287 BoostGraph Tout_current_mini = morphee::graph::CommonGraph32(numVert);
3289 BoostGraph Ttemp = morphee::graph::CommonGraph32(numVert);
3290 BoostGraph Ttemp_2 = morphee::graph::CommonGraph32(numVert);
3291 BoostGraph Ttemp_3 = morphee::graph::CommonGraph32(numVert);
3293 int number_of_edges = 0;
3295 std::cout <<
"build Region Adjacency Graph Edges" << std::endl;
3297 for (it = imIn.begin(), iend = imIn.end(); it != iend;
3300 o1 = it.getOffset();
3301 int val = imIn.pixelFromOffset(o1);
3304 neighb.setCenter(o1);
3306 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
3307 const offset_t o2 = nit.getOffset();
3308 int val2 = imIn.pixelFromOffset(o2);
3312 boost::tie(e1, in1) =
3313 boost::edge(val - 1, val2 - 1, Ttemp.getBoostGraph());
3314 boost::tie(e2, in1) =
3315 boost::edge(val - 1, val2 - 1, Ttemp_2.getBoostGraph());
3316 boost::tie(e3, in1) =
3317 boost::edge(val - 1, val2 - 1, Ttemp_3.getBoostGraph());
3319 double val3 = imGrad.pixelFromOffset(o1);
3320 double val4 = imGrad.pixelFromOffset(o2);
3321 double mini = std::max(val3, val4);
3324 Ttemp.addEdge(val - 1, val2 - 1, mini);
3325 Ttemp_2.addEdge(val - 1, val2 - 1, 1);
3326 Ttemp_3.addEdge(val - 1, val2 - 1, mini);
3328 number_of_edges = number_of_edges + 1;
3330 Ttemp.edgeWeight(e1, &tmp);
3331 Ttemp.setEdgeWeight(e1, tmp + mini);
3333 Ttemp_2.edgeWeight(e2, &tmp2);
3334 Ttemp_2.setEdgeWeight(e2, tmp2 + 1);
3336 Ttemp_3.edgeWeight(e3, &tmp3);
3338 Ttemp_3.setEdgeWeight(e3, mini);
3346 std::cout <<
"Done" << std::endl;
3348 std::vector<double> val_edges;
3349 int current_index = 1;
3351 int num_connected_component;
3352 t_LabelConnectedComponent(Tout, Tout_label, &num_connected_component);
3354 Tout_current = morphee::graph::CommonGraph32(num_connected_component);
3356 morphee::graph::CommonGraph32(num_connected_component);
3358 morphee::graph::CommonGraph32(num_connected_component);
3360 std::cout <<
"num_connected_component : " << num_connected_component
3362 std::cout <<
"Tout.numEdges() :" << Tout.numEdges() << std::endl;
3364 std::cout <<
"construct region adjacency graph of connected components"
3367 boost::tie(ed_it2, ed_end2) = boost::edges(Ttemp_2.getBoostGraph());
3368 boost::tie(ed_it3, ed_end3) = boost::edges(Ttemp_3.getBoostGraph());
3370 for (boost::tie(ed_it, ed_end) = boost::edges(Ttemp.getBoostGraph());
3371 ed_it != ed_end, ed_it2 != ed_end2, ed_it3 != ed_end3;
3372 ++ed_it, ++ed_it2, ++ed_it3) {
3373 Tout_label.vertexData(Ttemp.edgeSource(*ed_it), &vdata1);
3374 Tout_label.vertexData(Ttemp.edgeTarget(*ed_it), &vdata2);
3376 if (vdata1 != vdata2) {
3377 boost::tie(e1, in1) = boost::edge((
int) vdata1 - 1, (
int) vdata2 - 1,
3378 Tout_current.getBoostGraph());
3379 boost::tie(e2, in1) = boost::edge((
int) vdata1 - 1, (
int) vdata2 - 1,
3380 Tout_current_temp.getBoostGraph());
3381 boost::tie(e3, in1) = boost::edge((
int) vdata1 - 1, (
int) vdata2 - 1,
3382 Tout_current_mini.getBoostGraph());
3384 Ttemp.edgeWeight(*ed_it, &tmp);
3385 Ttemp_2.edgeWeight(*ed_it2, &tmp2);
3386 Ttemp_3.edgeWeight(*ed_it3, &tmp4);
3389 Tout_current.addEdge((
int) vdata1 - 1, (
int) vdata2 - 1,
3391 Tout_current_temp.addEdge((
int) vdata1 - 1, (
int) vdata2 - 1,
3393 Tout_current_mini.addEdge((
int) vdata1 - 1, (
int) vdata2 - 1,
3396 Tout_current.edgeWeight(e1, &tmp3);
3397 Tout_current.setEdgeWeight(e1, (
double) tmp3 + ((
double) tmp));
3399 Tout_current_temp.edgeWeight(e2, &tmp3);
3400 Tout_current_temp.setEdgeWeight(e2,
3401 (
double) tmp3 + ((
double) tmp2));
3403 Tout_current_mini.edgeWeight(e3, &tmp3);
3405 Tout_current_temp.setEdgeWeight(e3, tmp4);
3410 std::cout <<
"get edges values and sort them" << std::endl;
3413 double min_edge_value = 10000000000000.0f;
3414 double minimum_value = 10000000000000.0f;
3416 boost::tie(ed_it2, ed_end2) =
3417 boost::edges(Tout_current_temp.getBoostGraph());
3418 boost::tie(ed_it3, ed_end3) =
3419 boost::edges(Tout_current_mini.getBoostGraph());
3421 for (boost::tie(ed_it, ed_end) =
3422 boost::edges(Tout_current.getBoostGraph());
3423 ed_it != ed_end, ed_it2 != ed_end2, ed_it3 != ed_end3;
3424 ++ed_it, ++ed_it2, ++ed_it3) {
3425 Tout_current.edgeWeight(*ed_it, &tmp);
3426 Tout_current_temp.edgeWeight(*ed_it2, &tmp2);
3427 Tout_current_mini.edgeWeight(*ed_it3, &tmp3);
3429 if (
sqrt(((
double) tmp3) * ((
double) tmp / (
double) tmp2)) <
3432 sqrt(((
double) tmp3) * ((
double) tmp / (
double) tmp2));
3433 v1_remove = Tout_current.edgeSource(*ed_it);
3434 v2_remove = Tout_current.edgeTarget(*ed_it);
3438 std::cout <<
"label trees" << std::endl;
3440 while (num_connected_component > 1) {
3441 std::cout << num_connected_component << std::endl;
3443 bool added_edge =
false;
3445 while (added_edge ==
false) {
3446 boost::tie(ed_it2, ed_end2) = boost::edges(Ttemp_2.getBoostGraph());
3448 for (boost::tie(ed_it, ed_end) = boost::edges(Ttemp.getBoostGraph());
3449 ed_it != ed_end, ed_it2 != ed_end2; ++ed_it, ++ed_it2) {
3450 Tout_label.vertexData(Ttemp.edgeSource(*ed_it), &vdata1);
3451 Tout_label.vertexData(Ttemp.edgeTarget(*ed_it), &vdata2);
3453 if (((vdata1 - 1) == v1_remove && (vdata2 - 1) == v2_remove) ||
3454 ((vdata2 - 1) == v1_remove && (vdata1 - 1) == v2_remove)) {
3456 Tout.addEdge(Ttemp.edgeSource(*ed_it), Ttemp.edgeTarget(*ed_it),
3458 current_index = current_index + 1;
3461 if (added_edge ==
true)
3467 t_LabelConnectedComponent(Tout, Tout_label, &num_connected_component);
3469 Tout_current = morphee::graph::CommonGraph32(num_connected_component);
3471 morphee::graph::CommonGraph32(num_connected_component);
3473 morphee::graph::CommonGraph32(num_connected_component);
3476 boost::tie(ed_it2, ed_end2) = boost::edges(Ttemp_2.getBoostGraph());
3477 boost::tie(ed_it3, ed_end3) = boost::edges(Ttemp_3.getBoostGraph());
3479 for (boost::tie(ed_it, ed_end) = boost::edges(Ttemp.getBoostGraph());
3480 ed_it != ed_end, ed_it2 != ed_end2, ed_it3 != ed_end3;
3481 ++ed_it, ++ed_it2, ++ed_it3) {
3482 Tout_label.vertexData(Ttemp.edgeSource(*ed_it), &vdata1);
3483 Tout_label.vertexData(Ttemp.edgeTarget(*ed_it), &vdata2);
3485 if (vdata1 != vdata2) {
3486 boost::tie(e1, in1) =
3487 boost::edge((
int) vdata1 - 1, (
int) vdata2 - 1,
3488 Tout_current.getBoostGraph());
3489 boost::tie(e2, in1) =
3490 boost::edge((
int) vdata1 - 1, (
int) vdata2 - 1,
3491 Tout_current_temp.getBoostGraph());
3492 boost::tie(e3, in1) =
3493 boost::edge((
int) vdata1 - 1, (
int) vdata2 - 1,
3494 Tout_current_mini.getBoostGraph());
3496 Ttemp.edgeWeight(*ed_it, &tmp);
3497 Ttemp_2.edgeWeight(*ed_it2, &tmp2);
3498 Ttemp_3.edgeWeight(*ed_it3, &tmp4);
3501 Tout_current.addEdge((
int) vdata1 - 1, (
int) vdata2 - 1,
3503 Tout_current_temp.addEdge((
int) vdata1 - 1, (
int) vdata2 - 1,
3505 Tout_current_mini.addEdge((
int) vdata1 - 1, (
int) vdata2 - 1,
3508 Tout_current.edgeWeight(e1, &tmp3);
3509 Tout_current.setEdgeWeight(e1, (
double) tmp3 + ((
double) tmp));
3511 Tout_current_temp.edgeWeight(e2, &tmp3);
3512 Tout_current_temp.setEdgeWeight(e2,
3513 (
double) tmp3 + ((
double) tmp2));
3515 Tout_current_mini.edgeWeight(e3, &tmp3);
3517 Tout_current_temp.setEdgeWeight(e3, tmp4);
3522 min_edge_value = 10000000000000.0f;
3523 boost::tie(ed_it2, ed_end2) =
3524 boost::edges(Tout_current_temp.getBoostGraph());
3525 boost::tie(ed_it3, ed_end3) =
3526 boost::edges(Tout_current_mini.getBoostGraph());
3528 for (boost::tie(ed_it, ed_end) =
3529 boost::edges(Tout_current.getBoostGraph());
3530 ed_it != ed_end, ed_it2 != ed_end2, ed_it3 != ed_end3;
3531 ++ed_it, ++ed_it2, ++ed_it3) {
3532 Tout_current.edgeWeight(*ed_it, &tmp);
3533 Tout_current_temp.edgeWeight(*ed_it2, &tmp2);
3534 Tout_current_mini.edgeWeight(*ed_it3, &tmp3);
3536 if (
sqrt(((
double) tmp3) * ((
double) tmp / (
double) tmp2)) <
3539 sqrt(((
double) tmp3) * ((
double) tmp / (
double) tmp2));
3540 v1_remove = Tout_current.edgeSource(*ed_it);
3541 v2_remove = Tout_current.edgeTarget(*ed_it);
3549 template <
class ImageIn,
class ImageGrad,
class SE,
class BoostGraph>
3550 RES_C t_AverageLinkageTree(
const ImageIn &imIn,
const ImageGrad &imGrad,
3551 const SE &nl, BoostGraph &Tout)
3553 MORPHEE_ENTER_FUNCTION(
"t_AverageLinkageTree");
3555 if ((!imIn.isAllocated())) {
3556 MORPHEE_REGISTER_ERROR(
"Not allocated");
3557 return RES_NOT_ALLOCATED;
3560 if ((!imGrad.isAllocated())) {
3561 MORPHEE_REGISTER_ERROR(
"Not allocated");
3562 return RES_NOT_ALLOCATED;
3566 typename ImageIn::const_iterator it, iend;
3567 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
3568 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
3572 typename BoostGraph::EdgeIterator ed_it, ed_end;
3573 typename BoostGraph::EdgeIterator ed_it2, ed_end2;
3575 typename BoostGraph::VertexProperty vdata1, vdata2;
3576 typename BoostGraph::VertexDescriptor v1_remove, v2_remove;
3577 typename BoostGraph::VertexProperty label1, label2;
3582 typename BoostGraph::EdgeDescriptor e1, e2;
3583 typename BoostGraph::EdgeProperty tmp, tmp2, tmp3;
3585 std::cout <<
"build Region Adjacency Graph" << std::endl;
3587 for (it = imIn.begin(), iend = imIn.end(); it != iend;
3590 o0 = it.getOffset();
3591 int val = imIn.pixelFromOffset(o0);
3593 if (val > numVert) {
3598 std::cout <<
"build Region Adjacency Graph Vertices" << std::endl;
3599 std::cout <<
"number of Vertices:" << numVert << std::endl;
3601 Tout = morphee::graph::CommonGraph32(numVert);
3602 BoostGraph Tout_label = morphee::graph::CommonGraph32(numVert);
3603 BoostGraph Tout_current = morphee::graph::CommonGraph32(numVert);
3604 BoostGraph Tout_current_temp = morphee::graph::CommonGraph32(numVert);
3606 BoostGraph Ttemp = morphee::graph::CommonGraph32(numVert);
3607 BoostGraph Ttemp_2 = morphee::graph::CommonGraph32(numVert);
3609 int number_of_edges = 0;
3611 std::cout <<
"build Region Adjacency Graph Edges" << std::endl;
3613 for (it = imIn.begin(), iend = imIn.end(); it != iend;
3616 o1 = it.getOffset();
3617 int val = imIn.pixelFromOffset(o1);
3620 neighb.setCenter(o1);
3622 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
3623 const offset_t o2 = nit.getOffset();
3624 int val2 = imIn.pixelFromOffset(o2);
3628 boost::tie(e1, in1) =
3629 boost::edge(val - 1, val2 - 1, Ttemp.getBoostGraph());
3630 boost::tie(e2, in1) =
3631 boost::edge(val - 1, val2 - 1, Ttemp_2.getBoostGraph());
3633 double val3 = imGrad.pixelFromOffset(o1);
3634 double val4 = imGrad.pixelFromOffset(o2);
3635 double mini = std::max(val3, val4);
3638 Ttemp.addEdge(val - 1, val2 - 1, mini);
3639 Ttemp_2.addEdge(val - 1, val2 - 1, 1);
3640 number_of_edges = number_of_edges + 1;
3642 Ttemp.edgeWeight(e1, &tmp);
3643 Ttemp.setEdgeWeight(e1, tmp + mini);
3645 Ttemp_2.edgeWeight(e2, &tmp2);
3646 Ttemp_2.setEdgeWeight(e2, tmp2 + 1);
3654 std::cout <<
"Done" << std::endl;
3656 std::vector<double> val_edges;
3657 int current_index = 1;
3658 int num_connected_component;
3659 t_LabelConnectedComponent(Tout, Tout_label, &num_connected_component);
3661 Tout_current = morphee::graph::CommonGraph32(num_connected_component);
3663 morphee::graph::CommonGraph32(num_connected_component);
3665 std::cout <<
"num_connected_component : " << num_connected_component
3667 std::cout <<
"Tout.numEdges() :" << Tout.numEdges() << std::endl;
3669 std::cout <<
"construct region adjacency graph of connected components"
3672 boost::tie(ed_it2, ed_end2) = boost::edges(Ttemp_2.getBoostGraph());
3674 for (boost::tie(ed_it, ed_end) = boost::edges(Ttemp.getBoostGraph());
3675 ed_it != ed_end, ed_it2 != ed_end2; ++ed_it, ++ed_it2) {
3676 Tout_label.vertexData(Ttemp.edgeSource(*ed_it), &vdata1);
3677 Tout_label.vertexData(Ttemp.edgeTarget(*ed_it), &vdata2);
3679 if (vdata1 != vdata2) {
3680 boost::tie(e1, in1) = boost::edge((
int) vdata1 - 1, (
int) vdata2 - 1,
3681 Tout_current.getBoostGraph());
3682 boost::tie(e2, in1) = boost::edge((
int) vdata1 - 1, (
int) vdata2 - 1,
3683 Tout_current_temp.getBoostGraph());
3685 Ttemp.edgeWeight(*ed_it, &tmp);
3686 Ttemp_2.edgeWeight(*ed_it2, &tmp2);
3689 Tout_current.addEdge((
int) vdata1 - 1, (
int) vdata2 - 1,
3691 Tout_current_temp.addEdge((
int) vdata1 - 1, (
int) vdata2 - 1,
3694 Tout_current.edgeWeight(e1, &tmp3);
3695 Tout_current.setEdgeWeight(e1, (
double) tmp3 + ((
double) tmp));
3697 Tout_current_temp.edgeWeight(e2, &tmp3);
3698 Tout_current_temp.setEdgeWeight(e2,
3699 (
double) tmp3 + ((
double) tmp2));
3704 std::cout <<
"get edges values and sort them" << std::endl;
3707 double min_edge_value = 10000000000000.0f;
3708 double minimum_value = 10000000000000.0f;
3710 boost::tie(ed_it2, ed_end2) =
3711 boost::edges(Tout_current_temp.getBoostGraph());
3712 for (boost::tie(ed_it, ed_end) =
3713 boost::edges(Tout_current.getBoostGraph());
3714 ed_it != ed_end, ed_it2 != ed_end2; ++ed_it, ++ed_it2) {
3715 Tout_current.edgeWeight(*ed_it, &tmp);
3716 Tout_current_temp.edgeWeight(*ed_it2, &tmp2);
3718 if (((
double) tmp / (
double) tmp2) < min_edge_value) {
3719 min_edge_value = (double) tmp / (
double) tmp2;
3720 v1_remove = Tout_current.edgeSource(*ed_it);
3721 v2_remove = Tout_current.edgeTarget(*ed_it);
3725 std::cout <<
"label trees" << std::endl;
3727 while (num_connected_component > 1) {
3729 std::cout << num_connected_component << std::endl;
3731 bool added_edge =
false;
3733 while (added_edge ==
false) {
3734 boost::tie(ed_it2, ed_end2) = boost::edges(Ttemp_2.getBoostGraph());
3736 for (boost::tie(ed_it, ed_end) = boost::edges(Ttemp.getBoostGraph());
3737 ed_it != ed_end, ed_it2 != ed_end2; ++ed_it, ++ed_it2) {
3738 Tout_label.vertexData(Ttemp.edgeSource(*ed_it), &vdata1);
3739 Tout_label.vertexData(Ttemp.edgeTarget(*ed_it), &vdata2);
3741 if (((vdata1 - 1) == v1_remove && (vdata2 - 1) == v2_remove) ||
3742 ((vdata2 - 1) == v1_remove && (vdata1 - 1) == v2_remove)) {
3744 std::cout <<
"ADDED_EDGE" << vdata1 <<
"," << vdata2 << std::endl;
3746 Tout.addEdge(Ttemp.edgeSource(*ed_it), Ttemp.edgeTarget(*ed_it),
3748 current_index = current_index + 1;
3751 if (added_edge ==
true)
3757 t_LabelConnectedComponent(Tout, Tout_label, &num_connected_component);
3758 std::cout <<
"after clear edges" << num_connected_component
3761 Tout_current = morphee::graph::CommonGraph32(num_connected_component);
3763 morphee::graph::CommonGraph32(num_connected_component);
3766 boost::tie(ed_it2, ed_end2) = boost::edges(Ttemp_2.getBoostGraph());
3767 for (boost::tie(ed_it, ed_end) = boost::edges(Ttemp.getBoostGraph());
3768 ed_it != ed_end, ed_it2 != ed_end2; ++ed_it, ++ed_it2) {
3769 Tout_label.vertexData(Ttemp.edgeSource(*ed_it), &vdata1);
3770 Tout_label.vertexData(Ttemp.edgeTarget(*ed_it), &vdata2);
3772 if (vdata1 != vdata2) {
3773 boost::tie(e1, in1) =
3774 boost::edge((
int) vdata1 - 1, (
int) vdata2 - 1,
3775 Tout_current.getBoostGraph());
3776 boost::tie(e2, in1) =
3777 boost::edge((
int) vdata1 - 1, (
int) vdata2 - 1,
3778 Tout_current_temp.getBoostGraph());
3780 Ttemp.edgeWeight(*ed_it, &tmp);
3781 Ttemp_2.edgeWeight(*ed_it2, &tmp2);
3784 Tout_current.addEdge((
int) vdata1 - 1, (
int) vdata2 - 1,
3786 Tout_current_temp.addEdge((
int) vdata1 - 1, (
int) vdata2 - 1,
3789 Tout_current.edgeWeight(e1, &tmp3);
3790 Tout_current.setEdgeWeight(e1, (
double) tmp3 + ((
double) tmp));
3792 Tout_current_temp.edgeWeight(e2, &tmp3);
3793 Tout_current_temp.setEdgeWeight(e2,
3794 (
double) tmp3 + ((
double) tmp2));
3799 min_edge_value = 10000000000000.0f;
3800 boost::tie(ed_it2, ed_end2) =
3801 boost::edges(Tout_current_temp.getBoostGraph());
3803 for (boost::tie(ed_it, ed_end) =
3804 boost::edges(Tout_current.getBoostGraph());
3805 ed_it != ed_end, ed_it2 != ed_end2; ++ed_it, ++ed_it2) {
3806 Tout_current.edgeWeight(*ed_it, &tmp);
3807 Tout_current_temp.edgeWeight(*ed_it2, &tmp2);
3809 if (((
double) tmp / (
double) tmp2) < min_edge_value) {
3810 min_edge_value = (double) tmp / (
double) tmp2;
3811 v1_remove = Tout_current.edgeSource(*ed_it);
3812 v2_remove = Tout_current.edgeTarget(*ed_it);
3821 template <
class ImageIn,
class BoostGraph>
3822 RES_C t_Centrality_Edges_Weighting(
const ImageIn &imIn, std::vector<int> p,
3823 int vRoot, BoostGraph &Tout)
3825 MORPHEE_ENTER_FUNCTION(
"t_Centrality_Edges_Weighting");
3827 typename BoostGraph::VertexIterator v_it, v_end;
3828 typename BoostGraph::VertexIterator v_it2, v_end2;
3830 typename BoostGraph::VertexProperty vdata1, vdata2;
3831 typename BoostGraph::VertexDescriptor v1_remove, v2_remove, current_node;
3832 typename BoostGraph::VertexProperty label1, label2;
3834 typename BoostGraph::EdgeIterator ed_it, ed_end;
3835 typename BoostGraph::EdgeIterator ed_it2, ed_end2;
3837 typename BoostGraph::EdgeDescriptor e1, e2;
3838 typename BoostGraph::EdgeProperty tmp, tmp2, tmp3;
3841 typename ImageIn::const_iterator it, iend;
3842 offset_t o0, current_offset;
3846 ImageIn imOut = imIn.getSame();
3849 for (it = imIn.begin(), iend = imIn.end(); it != iend;
3852 current_offset = it.getOffset();
3854 imOut.setPixel(current_offset, 0);
3858 for (it = imIn.begin(), iend = imIn.end(); it != iend;
3861 current_offset = it.getOffset();
3865 int marker = imIn.pixelFromOffset(current_offset);
3866 int pixout = imOut.pixelFromOffset(current_offset);
3868 if (p[current_offset] != vRoot && p[current_offset] > 0)
3869 imOut.setPixel(p[current_offset], pixout + 1);
3873 for (it = imIn.begin(), iend = imIn.end(); it != iend;
3876 current_offset = it.getOffset();
3880 int pixout = imOut.pixelFromOffset(current_offset);
3881 int marker = imIn.pixelFromOffset(current_offset);
3884 while (current_offset > 0 && current_offset != vRoot && marker == 0) {
3885 boost::tie(e1, in1) = boost::edge(current_offset, p[current_offset],
3886 Tout.getBoostGraph());
3889 Tout.addEdge(current_offset, p[current_offset], 1);
3891 Tout.edgeWeight(e1, &tmp);
3892 Tout.setEdgeWeight(e1, tmp + 1);
3901 template <
class ImageWs,
class ImageIn,
class ImageGrad,
class SE,
3903 RES_C t_AverageLinkageTree_MS(
const ImageWs &imWs,
const ImageIn &imIn,
3904 const ImageGrad &imGrad,
const SE &nl,
3907 MORPHEE_ENTER_FUNCTION(
"t_AverageLinkageTree_MS");
3910 if ((!imWs.isAllocated())) {
3911 MORPHEE_REGISTER_ERROR(
"Not allocated");
3912 return RES_NOT_ALLOCATED;
3916 if ((!imIn.isAllocated())) {
3917 MORPHEE_REGISTER_ERROR(
"Not allocated");
3918 return RES_NOT_ALLOCATED;
3922 if ((!imGrad.isAllocated())) {
3923 MORPHEE_REGISTER_ERROR(
"Not allocated");
3924 return RES_NOT_ALLOCATED;
3927 typename BoostGraph::VertexIterator v_it, v_end;
3928 typename BoostGraph::VertexIterator v_it2, v_end2;
3930 typename BoostGraph::VertexProperty vdata1, vdata2;
3931 typename BoostGraph::VertexDescriptor v1_remove, v2_remove, current_node;
3932 typename BoostGraph::VertexProperty label1, label2;
3934 typename BoostGraph::EdgeIterator ed_it, ed_end;
3935 typename BoostGraph::EdgeIterator ed_it2, ed_end2;
3936 typename BoostGraph::EdgeDescriptor et1, et2;
3937 typename BoostGraph::EdgeProperty tmp, tmp2, tmp3;
3940 typename ImageIn::const_iterator it, iend;
3941 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
3942 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
3946 std::queue<int> temp_q;
3951 typedef boost::adjacency_list<
3952 boost::vecS, boost::vecS, boost::undirectedS,
3953 boost::property<boost::vertex_distance_t, double>,
3954 boost::property<boost::edge_capacity_t, double>>
3959 boost::property_map<Graph_d, boost::edge_capacity_t>::type weightmap =
3960 boost::get(boost::edge_capacity, g);
3963 Graph_d::edge_descriptor e1, e2, e3, e4;
3964 Graph_d::vertex_descriptor vRoot;
3970 std::cout <<
"build graph vertices" << std::endl;
3972 for (it = imIn.begin(), iend = imIn.end(); it != iend;
3975 o0 = it.getOffset();
3976 boost::add_vertex(g);
3980 BoostGraph T_temp = morphee::graph::CommonGraph32(numVert);
3982 vRoot = boost::add_vertex(g);
3984 std::cout <<
"number of vertices: " << numVert << std::endl;
3986 std::cout <<
"build graph edges" << std::endl;
3988 for (it = imIn.begin(), iend = imIn.end(); it != iend;
3991 o1 = it.getOffset();
3992 double val = imGrad.pixelFromOffset(o1);
3993 int marker = imWs.pixelFromOffset(o1);
3996 boost::tie(e4, in1) = boost::add_edge(vRoot, o1, g);
3997 weightmap[e4] = 0.1;
4000 neighb.setCenter(o1);
4002 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
4003 const offset_t o2 = nit.getOffset();
4007 double val2 = imGrad.pixelFromOffset(o2);
4008 double cost = std::max(val, val2);
4009 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
4010 weightmap[e4] = cost;
4015 std::cout <<
"Compute Minimum Spanning Forest" << std::endl;
4017 std::vector<boost::graph_traits<Graph_d>::vertex_descriptor> p(
4018 boost::num_vertices(g));
4019 boost::property_map<Graph_d, boost::vertex_distance_t>::type distancemap =
4020 boost::get(boost::vertex_distance, g);
4021 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap2 =
4022 boost::get(boost::vertex_index, g);
4024 dijkstra_shortest_paths(g, vRoot, &p[0], distancemap, weightmap,
4025 indexmap2, std::less<double>(),
4026 boost::detail::maximum<double>(),
4027 (std::numeric_limits<double>::max)(), 0,
4028 boost::default_dijkstra_visitor());
4030 std::vector<int> p_int;
4032 for (
int i = 0; i < numVert; i++) {
4033 p_int[i] = (int) p[i];
4036 t_Centrality_Edges_Weighting(imWs, p_int, (
int) vRoot, Tout);
4041 template <
class ImageIn,
class ImageGrad,
class SE,
class BoostGraph>
4042 RES_C t_NeighborhoodGraphFromMosaic_WithMinValue(
const ImageIn &imIn,
4043 const ImageGrad &imGrad,
4047 MORPHEE_ENTER_FUNCTION(
"t_NeighborhoodGraphFromMosaic_WithMinValue");
4049 if ((!imIn.isAllocated())) {
4050 MORPHEE_REGISTER_ERROR(
"Not allocated");
4051 return RES_NOT_ALLOCATED;
4054 if ((!imGrad.isAllocated())) {
4055 MORPHEE_REGISTER_ERROR(
"Not allocated");
4056 return RES_NOT_ALLOCATED;
4060 typename ImageIn::const_iterator it, iend;
4061 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
4062 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
4066 typename BoostGraph::EdgeIterator ed_it, ed_end;
4067 typename BoostGraph::EdgeIterator ed_it2, ed_end2;
4072 typename BoostGraph::EdgeDescriptor e1, e2;
4073 typename BoostGraph::EdgeProperty tmp, tmp2;
4075 std::cout <<
"build Region Adjacency Graph" << std::endl;
4077 for (it = imIn.begin(), iend = imIn.end(); it != iend;
4080 o0 = it.getOffset();
4081 int val = imIn.pixelFromOffset(o0);
4083 if (val > numVert) {
4088 std::cout <<
"build Region Adjacency Graph Vertices" << std::endl;
4089 std::cout <<
"number of Vertices:" << numVert << std::endl;
4091 Gout = morphee::graph::CommonGraph32(numVert);
4092 BoostGraph Gtemp = morphee::graph::CommonGraph32(numVert);
4094 std::cout <<
"build Region Adjacency Graph Edges" << std::endl;
4096 for (it = imIn.begin(), iend = imIn.end(); it != iend;
4099 o1 = it.getOffset();
4100 int val = imIn.pixelFromOffset(o1);
4103 neighb.setCenter(o1);
4105 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
4106 const offset_t o2 = nit.getOffset();
4107 int val2 = imIn.pixelFromOffset(o2);
4111 boost::tie(e1, in1) =
4112 boost::edge(val - 1, val2 - 1, Gout.getBoostGraph());
4113 boost::tie(e2, in1) =
4114 boost::edge(val - 1, val2 - 1, Gtemp.getBoostGraph());
4116 double val3 = imGrad.pixelFromOffset(o1);
4117 double val4 = imGrad.pixelFromOffset(o2);
4118 double mini = std::max(val3, val4);
4121 Gout.addEdge(val - 1, val2 - 1, mini);
4122 Gtemp.addEdge(val - 1, val2 - 1, 1);
4124 Gout.edgeWeight(e1, &tmp);
4125 Gout.setEdgeWeight(e1, tmp + mini);
4127 Gtemp.edgeWeight(e2, &tmp2);
4128 Gtemp.setEdgeWeight(e2, tmp2 + 1);
4136 boost::tie(ed_it2, ed_end2) = boost::edges(Gtemp.getBoostGraph());
4138 for (boost::tie(ed_it, ed_end) = boost::edges(Gout.getBoostGraph());
4139 ed_it != ed_end, ed_it2 != ed_end2; ++ed_it, ++ed_it2) {
4140 Gout.edgeWeight(*ed_it, &tmp);
4141 Gtemp.edgeWeight(*ed_it2, &tmp2);
4143 Gout.setEdgeWeight(*ed_it,
4144 ((
double) tmp) / ((
double) tmp2));
4150 template <
class ImageIn,
class ImageGrad,
class ImageVal,
typename _alpha,
4151 class SE,
class BoostGraph>
4152 RES_C t_NeighborhoodGraphFromMosaic_WithMeanGradientValue_AndQuadError(
4153 const ImageIn &imIn,
const ImageGrad &imGrad,
const ImageVal &imVal,
4154 const _alpha alpha,
const SE &nl, BoostGraph &Gout)
4156 MORPHEE_ENTER_FUNCTION(
4157 "t_NeighborhoodGraphFromMosaic_WithMeanGradientValue_AndQuadError");
4159 if ((!imIn.isAllocated())) {
4160 MORPHEE_REGISTER_ERROR(
"Not allocated");
4161 return RES_NOT_ALLOCATED;
4164 if ((!imGrad.isAllocated())) {
4165 MORPHEE_REGISTER_ERROR(
"Not allocated");
4166 return RES_NOT_ALLOCATED;
4169 if ((!imVal.isAllocated())) {
4170 MORPHEE_REGISTER_ERROR(
"Not allocated");
4171 return RES_NOT_ALLOCATED;
4175 typename ImageIn::const_iterator it, iend;
4176 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
4177 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
4181 typename BoostGraph::EdgeIterator ed_it, ed_end;
4182 typename BoostGraph::VertexIterator v_it, v_end;
4183 typename BoostGraph::EdgeIterator ed_it2, ed_end2;
4188 typename BoostGraph::EdgeDescriptor e1, e2;
4189 typename BoostGraph::EdgeProperty tmp, tmp2;
4190 typename BoostGraph::VertexProperty quad1, quad2;
4192 std::cout <<
"build Region Adjacency Graph" << std::endl;
4194 for (it = imIn.begin(), iend = imIn.end(); it != iend;
4197 o0 = it.getOffset();
4198 int val = imIn.pixelFromOffset(o0);
4200 if (val > numVert) {
4205 std::vector<double> mean_values;
4206 std::vector<double> number_of_values;
4207 std::vector<double> quad_error;
4209 for (
int i = 0; i < numVert; i++) {
4210 mean_values.push_back(0);
4211 number_of_values.push_back(0);
4212 quad_error.push_back(0);
4215 for (it = imIn.begin(), iend = imIn.end(); it != iend;
4218 o1 = it.getOffset();
4219 int val = imIn.pixelFromOffset(o1);
4220 double val_image = imVal.pixelFromOffset(o1);
4221 mean_values[val - 1] = mean_values[val - 1] + val_image;
4222 number_of_values[val - 1] = number_of_values[val - 1] + 1.0;
4225 for (
int i = 0; i < numVert; i++) {
4226 mean_values[i] = mean_values[i] / number_of_values[i];
4229 std::cout <<
"build Region Adjacency Graph Vertices" << std::endl;
4230 std::cout <<
"number of Vertices:" << numVert << std::endl;
4232 Gout = morphee::graph::CommonGraph32(numVert);
4233 BoostGraph Gtemp = morphee::graph::CommonGraph32(numVert);
4235 std::cout <<
"build Region Adjacency Graph Edges" << std::endl;
4237 for (it = imIn.begin(), iend = imIn.end(); it != iend;
4240 o1 = it.getOffset();
4241 int val = imIn.pixelFromOffset(o1);
4242 double val_image = imVal.pixelFromOffset(o1);
4244 quad_error[val - 1] =
4245 (double) quad_error[val - 1] +
4246 std::pow(std::abs(val_image - (
double) mean_values[val - 1]), 2);
4249 neighb.setCenter(o1);
4251 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
4252 const offset_t o2 = nit.getOffset();
4253 int val2 = imIn.pixelFromOffset(o2);
4257 boost::tie(e1, in1) =
4258 boost::edge(val - 1, val2 - 1, Gout.getBoostGraph());
4259 boost::tie(e2, in1) =
4260 boost::edge(val - 1, val2 - 1, Gtemp.getBoostGraph());
4262 double val3 = imGrad.pixelFromOffset(o1);
4263 double val4 = imGrad.pixelFromOffset(o2);
4264 double maxi = std::max(val3, val4);
4268 Gout.addEdge(val - 1, val2 - 1, maxi);
4269 Gtemp.addEdge(val - 1, val2 - 1, 1);
4271 Gout.edgeWeight(e1, &tmp);
4272 Gout.setEdgeWeight(e1, (
double) tmp + maxi);
4274 Gtemp.edgeWeight(e2, &tmp2);
4275 Gtemp.setEdgeWeight(e2, (
double) tmp2 + 1);
4283 std::cout <<
"number of Edges : " << numEdges << std::endl;
4285 int current_vertex = 0;
4286 double max_value = 0;
4288 for (boost::tie(v_it, v_end) = boost::vertices(Gout.getBoostGraph());
4289 v_it != v_end; ++v_it) {
4293 if (max_value < quad_error[current_vertex])
4294 max_value = quad_error[current_vertex];
4296 Gout.setVertexData(*v_it, mean_values[current_vertex]);
4297 current_vertex = current_vertex + 1;
4301 boost::tie(ed_it2, ed_end2) = boost::edges(Gtemp.getBoostGraph());
4303 for (boost::tie(ed_it, ed_end) = boost::edges(Gout.getBoostGraph());
4304 ed_it != ed_end, ed_it2 != ed_end2; ++ed_it, ++ed_it2) {
4305 Gout.edgeWeight(*ed_it, &tmp);
4306 Gtemp.edgeWeight(*ed_it2, &tmp2);
4308 Gout.vertexData(Gout.edgeSource(*ed_it), &quad1);
4309 Gout.vertexData(Gout.edgeTarget(*ed_it), &quad2);
4311 double area_1 = number_of_values[(int) Gout.edgeSource(*ed_it)] +
4312 quad_error[(int) Gout.edgeSource(*ed_it)];
4313 double area_2 = number_of_values[(int) Gout.edgeTarget(*ed_it)] +
4314 quad_error[(int) Gout.edgeTarget(*ed_it)];
4317 (((double) tmp) / ((double) tmp2));
4325 Gout.setEdgeWeight(*ed_it, cost);
4329 for (boost::tie(v_it, v_end) = boost::vertices(Gout.getBoostGraph());
4330 v_it != v_end; ++v_it) {
4331 Gout.setVertexData(*v_it, (
double) number_of_values[current_vertex] +
4332 (double) quad_error[current_vertex]);
4333 current_vertex = current_vertex + 1;
4432 template <
class ImageIn,
class ImageGradx,
class ImageGrady,
4433 class ImageMarker,
class SE,
class ImageOut>
4434 RES_C t_geoCutsParametric(
const ImageIn &imIn,
const ImageGradx &imGradx,
4435 const ImageGrady &imGrady,
4436 const ImageMarker &imMarker,
const SE &nl,
4439 MORPHEE_ENTER_FUNCTION(
"t_geoCutsParametric");
4441 std::cout <<
"Enter function t_geoCutsParametric" << std::endl;
4443 if ((!imOut.isAllocated())) {
4444 MORPHEE_REGISTER_ERROR(
"Not allocated");
4445 return RES_NOT_ALLOCATED;
4448 if ((!imIn.isAllocated())) {
4449 MORPHEE_REGISTER_ERROR(
"Not allocated");
4450 return RES_NOT_ALLOCATED;
4453 if ((!imGradx.isAllocated())) {
4454 MORPHEE_REGISTER_ERROR(
"Not allocated");
4455 return RES_NOT_ALLOCATED;
4458 if ((!imGrady.isAllocated())) {
4459 MORPHEE_REGISTER_ERROR(
"Not allocated");
4460 return RES_NOT_ALLOCATED;
4463 if ((!imMarker.isAllocated())) {
4464 MORPHEE_REGISTER_ERROR(
"Not allocated");
4465 return RES_NOT_ALLOCATED;
4469 typename ImageIn::const_iterator it, iend;
4470 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
4471 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
4476 typedef boost::adjacency_list_traits<boost::vecS, boost::vecS,
4479 typedef boost::adjacency_list<
4480 boost::vecS, boost::vecS, boost::directedS,
4481 boost::property<boost::vertex_name_t, std::string>,
4483 boost::edge_capacity_t, float,
4484 boost::property<boost::edge_residual_capacity_t, float,
4485 boost::property<boost::edge_reverse_t,
4486 Traits::edge_descriptor>>>>
4493 boost::property_map<Graph_d, boost::edge_capacity_t>::type capacity =
4494 boost::get(boost::edge_capacity, g);
4496 boost::property_map<Graph_d, boost::edge_reverse_t>::type rev =
4497 get(boost::edge_reverse, g);
4499 boost::property_map<Graph_d, boost::edge_residual_capacity_t>::type
4500 residual_capacity = get(boost::edge_residual_capacity, g);
4503 Graph_d::edge_descriptor e1, e2, e3, e4;
4504 Graph_d::vertex_descriptor vSource, vSink;
4509 std::cout <<
"build graph vertices" << std::endl;
4510 for (it = imIn.begin(), iend = imIn.end(); it != iend;
4513 o0 = it.getOffset();
4514 boost::add_vertex(g);
4518 vSource = boost::add_vertex(g);
4519 vSink = boost::add_vertex(g);
4522 std::cout <<
"build graph edges" << std::endl;
4523 for (it = imIn.begin(), iend = imIn.end(); it != iend;
4526 o1 = it.getOffset();
4528 boost::tie(e4, in1) = boost::add_edge(vSource, o1, g);
4529 boost::tie(e3, in1) = boost::add_edge(o1, vSource, g);
4534 boost::tie(e3, in1) = boost::add_edge(o1, vSink, g);
4535 boost::tie(e4, in1) = boost::add_edge(vSink, o1, g);
4540 neighb.setCenter(o1);
4542 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
4543 const offset_t o2 = nit.getOffset();
4547 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
4548 boost::tie(e3, in1) = boost::add_edge(o2, o1, g);
4556 std::cout <<
"graph is built" << std::endl;
4558 std::cout <<
"number of vertices: " << numVert << std::endl;
4559 std::cout <<
"number of edges: " << numEdges << std::endl;
4561 std::cout <<
"weight edges" << std::endl;
4563 double value_object = 0.7;
4564 double value_background = 0.0;
4567 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap =
4568 boost::get(boost::vertex_index, g);
4569 std::vector<boost::default_color_type> color(boost::num_vertices(g));
4573 for (lambda = 0.25, iter = 0; lambda <= 0.75, iter <= 500;
4574 lambda = lambda + 0.001, iter++) {
4575 for (it = imIn.begin(), iend = imIn.end(); it != iend;
4578 o1 = it.getOffset();
4581 float val = (float) imMarker.pixelFromOffset(o1);
4585 boost::tie(e4, in1) = boost::edge(vSource, o1, g);
4586 boost::tie(e3, in1) = boost::edge(o1, vSource, g);
4592 boost::tie(e3, in1) = boost::edge(o1, vSink, g);
4593 boost::tie(e4, in1) = boost::edge(vSink, o1, g);
4596 capacity[e3] = abs(val);
4597 capacity[e4] = abs(val);
4600 neighb.setCenter(o1);
4603 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
4604 const offset_t o2 = nit.getOffset();
4608 float val2 = (1.0 / 255.0) * (
float) imIn.pixelFromOffset(o2);
4610 lambda / ((1.0 / 255.0) + (val - val2) + (val - val2));
4611 boost::tie(e4, in1) = boost::edge(o1, o2, g);
4612 boost::tie(e3, in1) = boost::edge(o2, o1, g);
4613 capacity[e4] = cost;
4614 capacity[e3] = cost;
4619 std::cout <<
"Compute Max flow" << std::endl;
4620 #if BOOST_VERSION >= 104700
4622 boykov_kolmogorov_max_flow(g, capacity, residual_capacity, rev,
4623 &color[0], indexmap, vSource, vSink);
4625 double flow = kolmogorov_max_flow(g, capacity, residual_capacity, rev,
4626 &color[0], indexmap, vSource, vSink);
4628 std::cout <<
"c The total flow:" << std::endl;
4629 std::cout <<
"s " << flow << std::endl << std::endl;
4632 for (it = imIn.begin(), iend = imIn.end(); it != iend;
4635 o1 = it.getOffset();
4636 if (color[o1] == color[vSource])
4637 imOut.setPixel(o1, 0);
4639 imOut.setPixel(o1, 0);
4640 if (color[o1] == color[vSink])
4641 imOut.setPixel(o1, 100.0 * (lambda + 0.1));
4644 for (it = imIn.begin(), iend = imIn.end(); it != iend;
4647 o1 = it.getOffset();
4649 int oldval = imOut.pixelFromOffset(o1);
4651 std::min((
double) 100.0 * (lambda + 0.1), (
double) oldval);
4652 int oldval3 = 100.0 * (lambda + 0.1);
4654 if (color[o1] == color[vSink] && oldval > 0)
4655 imOut.setPixel(o1, (
int) oldval2);
4657 else if (color[o1] == color[vSink] && oldval == 0)
4658 imOut.setPixel(o1, (
int) oldval3);
4666 template <
class ImageIn,
class ImageMarker,
class ImageMarker2,
class SE,
4668 RES_C t_geoCutsBoundary_Constrained_MinSurfaces(
4669 const ImageIn &imIn,
const ImageMarker &imMarker,
4670 const ImageMarker2 &imMarker2,
const SE &nl, ImageOut &imOut)
4672 MORPHEE_ENTER_FUNCTION(
"t_geoCutsBoundary_Constrained_MinSurfaces");
4674 std::cout <<
"Enter function Geo-Cuts " << std::endl;
4676 if ((!imOut.isAllocated())) {
4677 MORPHEE_REGISTER_ERROR(
"Not allocated");
4678 return RES_NOT_ALLOCATED;
4681 if ((!imIn.isAllocated())) {
4682 MORPHEE_REGISTER_ERROR(
"Not allocated");
4683 return RES_NOT_ALLOCATED;
4686 if ((!imMarker.isAllocated())) {
4687 MORPHEE_REGISTER_ERROR(
"Not allocated");
4688 return RES_NOT_ALLOCATED;
4691 if ((!imMarker2.isAllocated())) {
4692 MORPHEE_REGISTER_ERROR(
"Not allocated");
4693 return RES_NOT_ALLOCATED;
4697 typename ImageIn::const_iterator it, iend;
4698 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
4699 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
4704 typedef boost::adjacency_list_traits<boost::vecS, boost::vecS,
4707 typedef boost::adjacency_list<
4708 boost::listS, boost::vecS, boost::directedS,
4709 boost::property<boost::vertex_name_t, std::string>,
4711 boost::edge_capacity_t, double,
4712 boost::property<boost::edge_residual_capacity_t, double,
4713 boost::property<boost::edge_reverse_t,
4714 Traits::edge_descriptor>>>>
4721 boost::property_map<Graph_d, boost::edge_capacity_t>::type capacity =
4722 boost::get(boost::edge_capacity, g);
4724 boost::property_map<Graph_d, boost::edge_reverse_t>::type rev =
4725 get(boost::edge_reverse, g);
4727 boost::property_map<Graph_d, boost::edge_residual_capacity_t>::type
4728 residual_capacity = get(boost::edge_residual_capacity, g);
4731 Graph_d::edge_descriptor e1, e2, e3, e4;
4732 Graph_d::vertex_descriptor vSource, vSink;
4736 std::cout <<
"build graph vertices" << std::endl;
4737 for (it = imIn.begin(), iend = imIn.end(); it != iend;
4740 o0 = it.getOffset();
4741 boost::add_vertex(g);
4745 std::cout <<
"number of vertices: " << numVert << std::endl;
4747 vSource = boost::add_vertex(g);
4748 vSink = boost::add_vertex(g);
4749 int source_sink_found = 0;
4751 std::cout <<
"build graph edges" << std::endl;
4752 for (it = imIn.begin(), iend = imIn.end(); it != iend;
4755 o1 = it.getOffset();
4756 double val = imIn.pixelFromOffset(o1);
4757 int marker = imMarker.pixelFromOffset(o1);
4783 neighb.setCenter(o1);
4785 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
4786 const offset_t o2 = nit.getOffset();
4787 int marker2 = imMarker.pixelFromOffset(o2);
4789 if (marker == 255 && marker2 == 128) {
4790 boost::tie(e4, in1) = boost::add_edge(vSource, o2, g);
4791 boost::tie(e3, in1) = boost::add_edge(o2, vSource, g);
4792 capacity[e4] = (std::numeric_limits<double>::max)();
4793 capacity[e3] = (std::numeric_limits<double>::max)();
4798 boost::tie(e4, in1) = boost::add_edge(vSink, o1, g);
4799 boost::tie(e3, in1) = boost::add_edge(o1, vSink, g);
4800 capacity[e4] = (std::numeric_limits<double>::max)();
4801 capacity[e3] = (std::numeric_limits<double>::max)();
4810 if (o2 > o1 && (marker == 0 || marker2 == 0 || marker == marker2)) {
4812 double val2 = imIn.pixelFromOffset(o2);
4813 double maxi = std::abs(val2 - val);
4814 double cost = 256 / (1 +
std::pow(maxi, 2));
4816 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
4817 boost::tie(e3, in1) = boost::add_edge(o2, o1, g);
4818 capacity[e4] = cost;
4819 capacity[e3] = cost;
4826 std::cout <<
"number of Edges : " << numEdges << std::endl;
4828 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap =
4829 boost::get(boost::vertex_index, g);
4830 std::vector<boost::default_color_type> color(boost::num_vertices(g));
4832 std::cout <<
"Compute Max flow " << std::endl;
4835 kolmogorov_max_flow_min_cost(g, capacity, residual_capacity, rev,
4836 &color[0], indexmap, vSink, vSource);
4837 std::cout <<
"c The total flow found :" << std::endl;
4838 std::cout <<
"s " << flow2 << std::endl << std::endl;
4840 for (it = imIn.begin(), iend = imIn.end(); it != iend;
4843 o1 = it.getOffset();
4844 if (color[o1] == color[vSource])
4845 imOut.setPixel(o1, 2);
4846 else if (color[o1] == color[vSink])
4847 imOut.setPixel(o1, 3);
4849 imOut.setPixel(o1, 4);
4855 template <
class ImageIn,
class ImageVal,
class BoostGraph,
4856 typename _nbmarkers,
class SE,
class ImageOut>
4857 RES_C t_geoCutsStochastic_Watershed_Graph(
const ImageIn &imIn,
4858 const ImageVal &imVal,
4860 const _nbmarkers nbmarkers,
4861 const SE &nl, ImageOut &imOut)
4863 MORPHEE_ENTER_FUNCTION(
"t_geoCutsStochastic_Watershed_Graph");
4865 std::cout <<
"Enter function Geo-Cuts Stochastic Watershed graph"
4868 if ((!imOut.isAllocated())) {
4869 std::cout <<
"imOut Not allocated" << std::endl;
4870 MORPHEE_REGISTER_ERROR(
"Not allocated");
4871 return RES_NOT_ALLOCATED;
4874 if ((!imIn.isAllocated())) {
4875 std::cout <<
"imIn Not allocated" << std::endl;
4876 MORPHEE_REGISTER_ERROR(
"Not allocated");
4877 return RES_NOT_ALLOCATED;
4880 if ((!imVal.isAllocated())) {
4881 std::cout <<
"imVal Not allocated" << std::endl;
4882 MORPHEE_REGISTER_ERROR(
"Not allocated");
4883 return RES_NOT_ALLOCATED;
4886 double markers = (double) nbmarkers;
4888 int size = imIn.getXSize() * imIn.getYSize() * imIn.getZSize();
4889 std::cout << size << std::endl;
4891 typename ImageOut::const_iterator it, iend;
4892 typename ImageIn::const_iterator it2, iend2;
4893 morphee::selement::Neighborhood<SE, ImageOut> neighb(imOut, nl);
4894 typename morphee::selement::Neighborhood<SE, ImageOut>::iterator nit,
4898 typedef typename BoostGraph::EdgeIterator EdgeIterator;
4899 typedef typename BoostGraph::VertexIterator VertexIterator;
4900 typedef typename BoostGraph::EdgeDescriptor EdgeDescriptor;
4901 typedef typename BoostGraph::VertexDescriptor VertexDescriptor;
4902 typename morphee::graph::CommonGraph32::EdgeProperty tmp;
4903 typename morphee::graph::CommonGraph32::VertexProperty vdata1;
4904 typename morphee::graph::CommonGraph32::VertexProperty label1, label2;
4905 EdgeIterator ed_it, ed_end, ed_it2, ed_end2;
4906 VertexIterator v_it, v_end;
4907 EdgeDescriptor last_edge, e1, e2;
4911 std::vector<double> val_edges;
4912 val_edges.push_back(0.0);
4913 double last_edge_value = 0.0;
4915 std::vector<EdgeDescriptor> removed_edges;
4917 morphee::graph::CommonGraph32 G(0);
4918 morphee::graph::CommonGraph32 G2(0);
4919 morphee::graph::CommonGraph32 Tree(0);
4921 std::cout <<
" Get NeighborhoodGraph " << std::endl;
4922 morphee::morphoBase::t_NeighborhoodGraphFromMosaic(imIn, nl, G);
4924 std::cout <<
"Copy Minimum Spanning Tree" << std::endl;
4925 Tree = t_CopyGraph(Gin);
4927 std::cout <<
"Done" << std::endl;
4932 morphee::graph::t_ProjectMarkersOnGraph(imVal, imIn, Tree);
4933 typename morphee::graph::CommonGraph32 Tree_temp = t_CopyGraph(Tree);
4934 typename morphee::graph::CommonGraph32 Tree_out = t_CopyGraph(Tree);
4935 typename morphee::graph::CommonGraph32 Tree2 = t_CopyGraph(Tree_temp);
4938 for (boost::tie(v_it, v_end) = boost::vertices(G.getBoostGraph());
4939 v_it != v_end; ++v_it) {
4940 Tree.vertexData(*v_it, &vdata1);
4944 for (boost::tie(ed_it, ed_end) = boost::edges(G.getBoostGraph());
4945 ed_it != ed_end; ++ed_it) {
4946 G.setEdgeWeight(*ed_it, 0);
4949 for (boost::tie(ed_it, ed_end) = boost::edges(Tree.getBoostGraph());
4950 ed_it != ed_end; ++ed_it) {
4951 Tree.edgeWeight(*ed_it, &tmp);
4953 val_edges.push_back(tmp);
4954 last_edge_value = (double) tmp;
4957 std::cout <<
"sort" << std::endl;
4959 std::sort(val_edges.begin(), val_edges.end(), std::less<double>());
4961 last_edge_value = val_edges.back();
4962 float max_value = last_edge_value;
4963 double last_analyzed = last_edge_value;
4965 while (val_edges.size() > 1) {
4970 for (boost::tie(ed_it, ed_end) = boost::edges(Tree.getBoostGraph());
4971 ed_it != ed_end; ++ed_it) {
4973 Tree.edgeWeight(*ed_it, &tmp);
4975 if (tmp == last_edge_value) {
4976 boost::tie(e1, in1) =
4977 boost::edge(Tree.edgeSource(*ed_it), Tree.edgeTarget(*ed_it),
4978 Tree_temp.getBoostGraph());
4979 removed_edges.push_back(e1);
4980 Tree_temp.removeEdge(Tree.edgeSource(*ed_it),
4981 Tree.edgeTarget(*ed_it));
4988 t_LabelConnectedComponent(Tree_temp, Tree2);
4990 while (removed_edges.size() > 0) {
4991 e1 = removed_edges.back();
4992 removed_edges.pop_back();
4994 Tree2.vertexData(Tree.edgeSource(e1), &label1);
4995 Tree2.vertexData(Tree.edgeTarget(e1), &label2);
4997 Tree.edgeWeight(e1, &tmp);
5004 for (boost::tie(v_it, v_end) = boost::vertices(Tree2.getBoostGraph());
5005 v_it != v_end; ++v_it) {
5006 Tree2.vertexData(*v_it, &vdata1);
5007 if (vdata1 == label1) {
5008 Tree.vertexData(*v_it, &vdata1);
5009 S1 = S1 + (double) vdata1;
5010 }
else if (vdata1 == label2) {
5011 Tree.vertexData(*v_it, &vdata1);
5012 S2 = S2 + (double) vdata1;
5020 double probability =
5029 if (probability > 0.0) {
5030 RES_C res = Tree_out.edgeFromVertices(Tree.edgeSource(e1),
5031 Tree.edgeTarget(e1), &e2);
5032 Tree_out.setEdgeWeight(e2, 65535.0 * probability);
5034 RES_C res = Tree_out.edgeFromVertices(Tree.edgeSource(e1),
5035 Tree.edgeTarget(e1), &e2);
5036 Tree_out.setEdgeWeight(e2, 0.0);
5040 while (last_edge_value == last_analyzed) {
5041 last_edge_value = val_edges.back();
5042 val_edges.pop_back();
5044 last_analyzed = last_edge_value;
5047 std::cout <<
"project on graphs" << std::endl;
5048 Tree2 = t_CopyGraph(Tree_out);
5049 Tree_temp = t_CopyGraph(Tree_out);
5053 for (boost::tie(ed_it, ed_end) = boost::edges(Tree_out.getBoostGraph());
5054 ed_it != ed_end; ++ed_it) {
5055 Tree_out.edgeWeight(*ed_it, &tmp);
5056 double value = (double) tmp;
5059 Tree_temp.removeEdge(Tree_out.edgeSource(*ed_it),
5060 Tree_out.edgeTarget(*ed_it));
5064 t_LabelConnectedComponent(Tree_temp, Tree2);
5066 Tree_temp.addEdge(Tree_out.edgeSource(*ed_it),
5067 Tree_out.edgeTarget(*ed_it), tmp);
5069 for (boost::tie(ed_it2, ed_end2) = boost::edges(G.getBoostGraph());
5070 ed_it2 != ed_end2; ++ed_it2) {
5071 Tree2.vertexData(G.edgeSource(*ed_it2), &label1);
5072 Tree2.vertexData(G.edgeTarget(*ed_it2), &label2);
5074 if (label1 != label2) {
5075 G.edgeWeight(*ed_it2, &tmp);
5076 G.setEdgeWeight(*ed_it2, std::max((
double) tmp, value));
5080 std::cout << lio <<
" / " << Tree_out.numEdges() << std::endl;
5084 Gin = morphee::graph::MinimumSpanningTreeFromGraph(G);
5086 std::cout <<
"project on image the pdf" << std::endl;
5088 for (it = imOut.begin(), iend = imOut.end(); it != iend;
5091 o0 = it.getOffset();
5092 imOut.setPixel(o0, 0);
5095 std::cout <<
"init done" << std::endl;
5097 for (it = imOut.begin(), iend = imOut.end(); it != iend;
5100 o0 = it.getOffset();
5101 int val1 = imIn.pixelFromOffset(o0);
5102 double valout1 = imOut.pixelFromOffset(o0);
5104 neighb.setCenter(o0);
5106 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
5107 o1 = nit.getOffset();
5110 int val2 = imIn.pixelFromOffset(o1);
5111 double valout2 = imOut.pixelFromOffset(o1);
5114 RES_C res = G.edgeFromVertices(val1 - 1, val2 - 1, &e1);
5115 if (res == RES_OK) {
5116 G.edgeWeight(e1, &tmp);
5118 imOut.setPixel(o0, std::max(valout1, (
double) tmp));
5119 imOut.setPixel(o1, std::max(valout2, (
double) tmp));
5122 RES_C res = G.edgeFromVertices(val2 - 1, val1 - 1, &e1);
5123 if (res == RES_OK) {
5124 G.edgeWeight(e1, &tmp);
5126 imOut.setPixel(o0, std::max(valout1, (
double) tmp));
5127 imOut.setPixel(o1, std::max(valout2, (
double) tmp));
5139 template <
class ImageIn,
class ImageVal,
class BoostGraph,
5140 typename _nbmarkers,
class SE,
class ImageOut>
5141 RES_C t_geoCutsStochastic_Watershed_Graph_NP(
const ImageIn &imIn,
5142 const ImageVal &imVal,
5144 const _nbmarkers nbmarkers,
5145 const SE &nl, ImageOut &imOut)
5147 MORPHEE_ENTER_FUNCTION(
"t_geoCutsStochastic_Watershed_Graph_NP");
5150 <<
"Enter function Geo-Cuts Stochastic Watershed graph non ponctual"
5153 if ((!imOut.isAllocated())) {
5154 std::cout <<
"imOut Not allocated" << std::endl;
5155 MORPHEE_REGISTER_ERROR(
"Not allocated");
5156 return RES_NOT_ALLOCATED;
5159 if ((!imIn.isAllocated())) {
5160 std::cout <<
"imIn Not allocated" << std::endl;
5161 MORPHEE_REGISTER_ERROR(
"Not allocated");
5162 return RES_NOT_ALLOCATED;
5165 if ((!imVal.isAllocated())) {
5166 std::cout <<
"imVal Not allocated" << std::endl;
5167 MORPHEE_REGISTER_ERROR(
"Not allocated");
5168 return RES_NOT_ALLOCATED;
5171 double markers = (double) nbmarkers;
5173 int size = imIn.getXSize() * imIn.getYSize() * imIn.getZSize();
5174 std::cout << size << std::endl;
5176 typename ImageOut::const_iterator it, iend;
5177 typename ImageIn::const_iterator it2, iend2;
5178 morphee::selement::Neighborhood<SE, ImageOut> neighb(imOut, nl);
5179 typename morphee::selement::Neighborhood<SE, ImageOut>::iterator nit,
5183 typedef typename BoostGraph::EdgeIterator EdgeIterator;
5184 typedef typename BoostGraph::VertexIterator VertexIterator;
5185 typedef typename BoostGraph::EdgeDescriptor EdgeDescriptor;
5186 typedef typename BoostGraph::VertexDescriptor VertexDescriptor;
5187 typename morphee::graph::CommonGraph32::EdgeProperty tmp;
5188 typename morphee::graph::CommonGraph32::VertexProperty vdata1;
5189 typename morphee::graph::CommonGraph32::VertexProperty label1, label2;
5190 EdgeIterator ed_it, ed_end, ed_it2, ed_end2;
5191 VertexIterator v_it, v_end;
5192 EdgeDescriptor last_edge, e1, e2;
5196 std::vector<double> val_edges;
5197 val_edges.push_back(0.0);
5198 double last_edge_value = 0.0;
5200 std::vector<EdgeDescriptor> removed_edges;
5202 morphee::graph::CommonGraph32 G(0);
5203 morphee::graph::CommonGraph32 G2(0);
5204 morphee::graph::CommonGraph32 Tree(0);
5206 std::cout <<
" Get NeighborhoodGraph " << std::endl;
5207 morphee::morphoBase::t_NeighborhoodGraphFromMosaic(imIn, nl, G);
5209 std::cout <<
"Copy Minimum Spanning Tree" << std::endl;
5210 Tree = t_CopyGraph(Gin);
5212 std::cout <<
"Done" << std::endl;
5217 morphee::graph::t_ProjectMarkersOnGraph(imVal, imIn, Tree);
5218 morphee::graph::CommonGraph32 Tree_temp = t_CopyGraph(Tree);
5219 morphee::graph::CommonGraph32 Tree_out = t_CopyGraph(Tree);
5220 morphee::graph::CommonGraph32 Tree2 = t_CopyGraph(Tree_temp);
5223 for (boost::tie(v_it, v_end) = boost::vertices(G.getBoostGraph());
5224 v_it != v_end; ++v_it) {
5225 Tree.vertexData(*v_it, &vdata1);
5229 for (boost::tie(ed_it, ed_end) = boost::edges(G.getBoostGraph());
5230 ed_it != ed_end; ++ed_it) {
5231 G.setEdgeWeight(*ed_it, 0);
5234 for (boost::tie(ed_it, ed_end) = boost::edges(Tree.getBoostGraph());
5235 ed_it != ed_end; ++ed_it) {
5236 Tree.edgeWeight(*ed_it, &tmp);
5238 val_edges.push_back(tmp);
5239 last_edge_value = (double) tmp;
5242 std::cout <<
"sort" << std::endl;
5244 std::sort(val_edges.begin(), val_edges.end(), std::less<double>());
5246 last_edge_value = val_edges.back();
5247 float max_value = last_edge_value;
5248 double last_analyzed = last_edge_value;
5250 while (val_edges.size() > 1) {
5255 for (boost::tie(ed_it, ed_end) = boost::edges(Tree.getBoostGraph());
5256 ed_it != ed_end; ++ed_it) {
5258 Tree.edgeWeight(*ed_it, &tmp);
5260 if (tmp == last_edge_value) {
5261 boost::tie(e1, in1) =
5262 boost::edge(Tree.edgeSource(*ed_it), Tree.edgeTarget(*ed_it),
5263 Tree_temp.getBoostGraph());
5264 removed_edges.push_back(e1);
5265 Tree_temp.removeEdge(Tree.edgeSource(*ed_it),
5266 Tree.edgeTarget(*ed_it));
5272 t_LabelConnectedComponent(Tree_temp, Tree2);
5274 while (removed_edges.size() > 0) {
5275 e1 = removed_edges.back();
5276 removed_edges.pop_back();
5278 Tree2.vertexData(Tree.edgeSource(e1), &label1);
5279 Tree2.vertexData(Tree.edgeTarget(e1), &label2);
5281 Tree.edgeWeight(e1, &tmp);
5288 for (boost::tie(v_it, v_end) = boost::vertices(Tree2.getBoostGraph());
5289 v_it != v_end; ++v_it) {
5290 Tree2.vertexData(*v_it, &vdata1);
5291 if (vdata1 == label1) {
5292 Tree.vertexData(*v_it, &vdata1);
5293 S1 = S1 + (double) vdata1;
5294 }
else if (vdata1 == label2) {
5295 Tree.vertexData(*v_it, &vdata1);
5296 S2 = S2 + (double) vdata1;
5304 double probability =
5313 if (probability > 0.0) {
5314 RES_C res = Tree_out.edgeFromVertices(Tree.edgeSource(e1),
5315 Tree.edgeTarget(e1), &e2);
5316 Tree_out.setEdgeWeight(e2, 65535.0 * probability);
5318 RES_C res = Tree_out.edgeFromVertices(Tree.edgeSource(e1),
5319 Tree.edgeTarget(e1), &e2);
5320 Tree_out.setEdgeWeight(e2, 0.0);
5324 while (last_edge_value == last_analyzed) {
5325 last_edge_value = val_edges.back();
5326 val_edges.pop_back();
5328 last_analyzed = last_edge_value;
5331 std::cout <<
"project on graphs" << std::endl;
5334 Tree2 = t_CopyGraph(Tree_out);
5335 Tree_temp = t_CopyGraph(Tree_out);
5339 for (boost::tie(ed_it, ed_end) = boost::edges(Tree_out.getBoostGraph());
5340 ed_it != ed_end; ++ed_it) {
5341 Tree_out.edgeWeight(*ed_it, &tmp);
5342 double value = (double) tmp;
5345 Tree_temp.removeEdge(Tree_out.edgeSource(*ed_it),
5346 Tree_out.edgeTarget(*ed_it));
5350 t_LabelConnectedComponent(Tree_temp, Tree2);
5352 Tree_temp.addEdge(Tree_out.edgeSource(*ed_it),
5353 Tree_out.edgeTarget(*ed_it), tmp);
5355 for (boost::tie(ed_it2, ed_end2) = boost::edges(G.getBoostGraph());
5356 ed_it2 != ed_end2; ++ed_it2) {
5357 Tree2.vertexData(G.edgeSource(*ed_it2), &label1);
5358 Tree2.vertexData(G.edgeTarget(*ed_it2), &label2);
5360 if (label1 != label2) {
5361 G.edgeWeight(*ed_it2, &tmp);
5362 G.setEdgeWeight(*ed_it2, std::max((
double) tmp, value));
5366 std::cout << lio <<
" / " << Tree_out.numEdges() << std::endl;
5370 Gin = morphee::graph::MinimumSpanningTreeFromGraph(G);
5372 std::cout <<
"project on image the pdf" << std::endl;
5374 for (it = imOut.begin(), iend = imOut.end(); it != iend;
5377 o0 = it.getOffset();
5378 imOut.setPixel(o0, 0);
5381 std::cout <<
"init done" << std::endl;
5383 for (it = imOut.begin(), iend = imOut.end(); it != iend;
5386 o0 = it.getOffset();
5387 int val1 = imIn.pixelFromOffset(o0);
5388 double valout1 = imOut.pixelFromOffset(o0);
5390 neighb.setCenter(o0);
5392 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
5393 o1 = nit.getOffset();
5396 int val2 = imIn.pixelFromOffset(o1);
5397 double valout2 = imOut.pixelFromOffset(o1);
5400 RES_C res = G.edgeFromVertices(val1 - 1, val2 - 1, &e1);
5401 if (res == RES_OK) {
5402 G.edgeWeight(e1, &tmp);
5404 imOut.setPixel(o0, std::max(valout1, (
double) tmp));
5405 imOut.setPixel(o1, std::max(valout2, (
double) tmp));
5408 RES_C res = G.edgeFromVertices(val2 - 1, val1 - 1, &e1);
5409 if (res == RES_OK) {
5410 G.edgeWeight(e1, &tmp);
5412 imOut.setPixel(o0, std::max(valout1, (
double) tmp));
5413 imOut.setPixel(o1, std::max(valout2, (
double) tmp));
5425 template <
class BoostGraph>
5426 RES_C t_UpdateSpanningTreeFromForest(
const BoostGraph &ForestIn,
5427 const BoostGraph &TIn,
5430 MORPHEE_ENTER_FUNCTION(
"t_UpdateSpanningTreeFromForest");
5432 std::cout <<
"Enter function t_UpdateSpanningTreeFromForest "
5435 typedef typename BoostGraph::EdgeIterator EdgeIterator;
5436 typedef typename BoostGraph::VertexIterator VertexIterator;
5437 typedef typename BoostGraph::EdgeDescriptor EdgeDescriptor;
5438 typedef typename BoostGraph::VertexDescriptor VertexDescriptor;
5439 typename BoostGraph::EdgeProperty tmp;
5440 EdgeIterator ed_it, ed_end, ed_it2, ed_end2;
5441 VertexIterator v_it, v_end;
5442 EdgeDescriptor last_edge, e1, e2;
5446 Tout = t_CopyGraph(TIn);
5448 for (boost::tie(ed_it, ed_end) = boost::edges(ForestIn.getBoostGraph());
5449 ed_it != ed_end; ++ed_it) {
5450 ForestIn.edgeWeight(*ed_it, &tmp);
5451 boost::tie(e1, in1) =
5452 boost::edge(ForestIn.edgeSource(*ed_it),
5453 ForestIn.edgeTarget(*ed_it), Tout.getBoostGraph());
5454 Tout.setEdgeWeight(e1, 0);
5460 template <
class ImageIn,
class BoostGraph,
class SE,
class ImageOut>
5461 RES_C t_GetUltrametricContourMap(
const ImageIn &imIn,
5462 const BoostGraph &Tree,
const SE &nl,
5465 MORPHEE_ENTER_FUNCTION(
"t_GetUltrametricContourMap");
5467 std::cout <<
"Enter t_GetUltrametricContourMap " << std::endl;
5469 if ((!imOut.isAllocated())) {
5470 MORPHEE_REGISTER_ERROR(
"Not allocated");
5471 return RES_NOT_ALLOCATED;
5474 if ((!imIn.isAllocated())) {
5475 MORPHEE_REGISTER_ERROR(
"Not allocated");
5476 return RES_NOT_ALLOCATED;
5479 int size = imIn.getXSize() * imIn.getYSize() * imIn.getZSize();
5482 typename BoostGraph::VertexProperty label1, label2;
5483 typedef typename BoostGraph::EdgeProperty EdgeProperty;
5484 typedef typename BoostGraph::EdgeDescriptor EdgeDescriptor;
5485 typedef typename BoostGraph::VertexDescriptor VertexDescriptor;
5489 typedef typename BoostGraph::EdgeIterator EdgeIterator;
5490 EdgeIterator ed_it, ed_end, ed_it2, ed_end2;
5492 std::cout <<
"project on image the graph" << std::endl;
5494 typename ImageOut::const_iterator it, iend;
5495 morphee::selement::Neighborhood<SE, ImageOut> neighb(imOut, nl);
5496 typename morphee::selement::Neighborhood<SE, ImageOut>::iterator nit,
5500 for (it = imOut.begin(), iend = imOut.end(); it != iend;
5503 o0 = it.getOffset();
5504 imOut.setPixel(o0, 0);
5507 std::cout <<
"project on graphs" << std::endl;
5508 BoostGraph G, Tree2, Tree_temp;
5510 Tree2 = t_CopyGraph(Tree);
5511 Tree_temp = t_CopyGraph(Tree);
5513 std::cout <<
"t_NeighborhoodGraphFromMosaic" << std::endl;
5514 morphee::morphoBase::t_NeighborhoodGraphFromMosaic(imIn, nl, G);
5516 for (boost::tie(ed_it, ed_end) = boost::edges(G.getBoostGraph());
5517 ed_it != ed_end; ++ed_it) {
5518 G.setEdgeWeight(*ed_it, 0);
5521 std::cout <<
"iterate..." << std::endl;
5525 for (boost::tie(ed_it, ed_end) = boost::edges(Tree.getBoostGraph());
5526 ed_it != ed_end; ++ed_it) {
5527 Tree.edgeWeight(*ed_it, &tmp);
5528 double value = (double) tmp;
5534 Tree_temp.removeEdge(
5535 typename BoostGraph::VertexDescriptor(Tree.edgeSource(*ed_it)),
5536 typename BoostGraph::VertexDescriptor(Tree.edgeTarget(*ed_it)));
5538 t_LabelConnectedComponent(Tree_temp, Tree2);
5541 typename BoostGraph::VertexDescriptor(Tree.edgeSource(*ed_it)),
5542 typename BoostGraph::VertexDescriptor(Tree.edgeTarget(*ed_it)),
5545 for (boost::tie(ed_it2, ed_end2) = boost::edges(G.getBoostGraph());
5546 ed_it2 != ed_end2; ++ed_it2) {
5548 typename BoostGraph::VertexDescriptor(G.edgeSource(*ed_it2)),
5551 typename BoostGraph::VertexDescriptor(G.edgeTarget(*ed_it2)),
5554 if (label1 != label2) {
5555 G.edgeWeight(*ed_it2, &tmp);
5556 G.setEdgeWeight(*ed_it2, std::max((
double) tmp, value));
5561 std::cout << lio <<
" / " << Tree.numEdges() << std::endl;
5565 std::cout <<
"project on image " << std::endl;
5566 for (it = imOut.begin(), iend = imOut.end(); it != iend;
5569 o0 = it.getOffset();
5571 int val1 = imIn.pixelFromOffset(o0);
5573 double valout1 = imOut.pixelFromOffset(o0);
5575 neighb.setCenter(o0);
5577 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
5578 o1 = nit.getOffset();
5581 int val2 = imIn.pixelFromOffset(o1);
5583 double valout2 = imOut.pixelFromOffset(o1);
5586 RES_C res = G.edgeFromVertices(val1 - 1, val2 - 1, &e1);
5588 if (res == RES_OK) {
5589 G.edgeWeight(e1, &tmp);
5593 o0, std::max(valout2, std::max(valout1, (
double) tmp)));
5599 RES_C res = G.edgeFromVertices(val2 - 1, val1 - 1, &e1);
5600 if (res == RES_OK) {
5601 G.edgeWeight(e1, &tmp);
5604 o0, std::max(valout2, std::max(valout1, (
double) tmp)));
5617 template <
class ImageIn,
class BoostGraph,
class SE,
class ImageOut>
5618 RES_C t_GetScaleSetUltrametricContourMap(
const ImageIn &imIn,
5619 const BoostGraph &Tree,
5620 const SE &nl, ImageOut &imOut)
5622 MORPHEE_ENTER_FUNCTION(
"t_GetScaleSetUltrametricContourMap");
5624 std::cout <<
"Enter t_GetScaleSetUltrametricContourMap " << std::endl;
5626 if ((!imOut.isAllocated())) {
5627 MORPHEE_REGISTER_ERROR(
"Not allocated");
5628 return RES_NOT_ALLOCATED;
5631 if ((!imIn.isAllocated())) {
5632 MORPHEE_REGISTER_ERROR(
"Not allocated");
5633 return RES_NOT_ALLOCATED;
5636 int size = imIn.getXSize() * imIn.getYSize() * imIn.getZSize();
5639 typename BoostGraph::VertexProperty label1, label2, vdata1, vdata2;
5640 typedef typename BoostGraph::EdgeProperty EdgeProperty;
5641 typedef typename BoostGraph::EdgeDescriptor EdgeDescriptor;
5642 typedef typename BoostGraph::VertexDescriptor VertexDescriptor;
5645 VertexDescriptor vs, vt;
5648 typedef typename BoostGraph::EdgeIterator EdgeIterator;
5649 EdgeIterator ed_it, ed_end, ed_it2, ed_end2;
5651 std::cout <<
"project on image the graph" << std::endl;
5653 typename ImageIn::const_iterator it2, iend2;
5655 typename ImageOut::const_iterator it, iend;
5656 morphee::selement::Neighborhood<SE, ImageOut> neighb(imOut, nl);
5657 typename morphee::selement::Neighborhood<SE, ImageOut>::iterator nit,
5661 for (it = imOut.begin(), iend = imOut.end(); it != iend;
5664 o0 = it.getOffset();
5665 imOut.setPixel(o0, 0);
5668 std::cout <<
"project on graphs" << std::endl;
5671 std::cout <<
"t_NeighborhoodGraphFromMosaic" << std::endl;
5672 morphee::morphoBase::t_NeighborhoodGraphFromMosaic(imIn, nl, G);
5674 for (boost::tie(ed_it, ed_end) = boost::edges(G.getBoostGraph());
5675 ed_it != ed_end; ++ed_it) {
5676 G.setEdgeWeight(*ed_it, 0);
5679 std::vector<double> val_edges;
5681 for (boost::tie(ed_it, ed_end) = boost::edges(Tree.getBoostGraph());
5682 ed_it != ed_end; ++ed_it) {
5683 Tree.edgeWeight(*ed_it, &tmp);
5684 val_edges.push_back(tmp);
5689 for (it2 = imIn.begin(), iend2 = imIn.end(); it2 != iend2;
5692 o0 = it2.getOffset();
5693 int val = imIn.pixelFromOffset(o0);
5695 if (val > numVert) {
5700 std::cout <<
"nb of regions = " << numVert << std::endl;
5703 BoostGraph Tree_label = morphee::graph::CommonGraph32(numVert);
5704 BoostGraph Tree_temp = morphee::graph::CommonGraph32(numVert);
5706 std::cout <<
"sort edges of tree" << std::endl;
5707 std::sort(val_edges.begin(), val_edges.end(), std::greater<double>());
5709 double last_edge_value = val_edges.back();
5710 double last_analyzed = last_edge_value;
5711 val_edges.pop_back();
5713 std::cout <<
"iterate..." << std::endl;
5716 while (val_edges.size() > 0) {
5718 for (boost::tie(ed_it, ed_end) = boost::edges(Tree.getBoostGraph());
5719 ed_it != ed_end; ++ed_it) {
5720 Tree.edgeWeight(*ed_it, &tmp);
5722 if (tmp == last_edge_value) {
5724 vs = Tree.edgeSource(*ed_it);
5725 vt = Tree.edgeTarget(*ed_it);
5726 boost::tie(e1, in1) =
5727 boost::add_edge(vs, vt, Tree_temp.getBoostGraph());
5732 int number_of_connected_components;
5733 t_LabelConnectedComponent(Tree_temp, Tree_label,
5734 &number_of_connected_components);
5735 std::cout <<
"number_of_connected_components = "
5736 << number_of_connected_components << std::endl;
5738 for (boost::tie(ed_it, ed_end) = boost::edges(G.getBoostGraph());
5739 ed_it != ed_end; ++ed_it) {
5740 vs = G.edgeSource(*ed_it);
5741 vt = G.edgeTarget(*ed_it);
5744 Tree_label.vertexData(vs, &vdata1);
5745 Tree_label.vertexData(vt, &vdata2);
5747 G.edgeWeight(*ed_it, &tmp);
5749 if (vdata1 != vdata2) {
5750 G.setEdgeWeight(*ed_it,
5751 std::max((
float) tmp, (
float) last_edge_value));
5755 while (last_edge_value == last_analyzed) {
5756 last_edge_value = val_edges.back();
5757 val_edges.pop_back();
5759 last_analyzed = last_edge_value;
5762 std::cout <<
"project on image " << std::endl;
5763 for (it = imOut.begin(), iend = imOut.end(); it != iend;
5766 o0 = it.getOffset();
5768 int val1 = imIn.pixelFromOffset(o0);
5770 double valout1 = imOut.pixelFromOffset(o0);
5772 neighb.setCenter(o0);
5774 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
5775 o1 = nit.getOffset();
5778 int val2 = imIn.pixelFromOffset(o1);
5780 double valout2 = imOut.pixelFromOffset(o1);
5783 RES_C res = G.edgeFromVertices(val1 - 1, val2 - 1, &e1);
5785 if (res == RES_OK) {
5786 G.edgeWeight(e1, &tmp);
5790 o0, std::max(valout2, std::max(valout1, (
double) tmp)));
5796 RES_C res = G.edgeFromVertices(val2 - 1, val1 - 1, &e1);
5797 if (res == RES_OK) {
5798 G.edgeWeight(e1, &tmp);
5801 o0, std::max(valout2, std::max(valout1, (
double) tmp)));
5814 template <
class ImageIn,
class ImageIn2,
class ImageVal,
5815 typename _nbmarkers,
typename _alpha,
class SE,
class ImageOut>
5816 RES_C t_geoCutsStochastic_Watershed_Variance(
const ImageIn &imIn,
5817 const ImageIn2 &imVal,
5818 const ImageVal &imGrad,
5819 const _nbmarkers nbmarkers,
5821 const SE &nl, ImageOut &imOut)
5823 MORPHEE_ENTER_FUNCTION(
"t_geoCutsStochastic_Watershed");
5825 std::cout <<
"Enter function Geo-Cuts Stochastic Watershed" << std::endl;
5827 if ((!imOut.isAllocated())) {
5828 MORPHEE_REGISTER_ERROR(
"Not allocated");
5829 return RES_NOT_ALLOCATED;
5832 if ((!imIn.isAllocated())) {
5833 MORPHEE_REGISTER_ERROR(
"Not allocated");
5834 return RES_NOT_ALLOCATED;
5837 if ((!imVal.isAllocated())) {
5838 MORPHEE_REGISTER_ERROR(
"Not allocated");
5839 return RES_NOT_ALLOCATED;
5842 if ((!imGrad.isAllocated())) {
5843 MORPHEE_REGISTER_ERROR(
"Not allocated");
5844 return RES_NOT_ALLOCATED;
5847 double markers = (double) nbmarkers;
5849 int size = imIn.getXSize() * imIn.getYSize() * imIn.getZSize();
5852 typedef typename morphee::graph::CommonGraph32::_boostGraph BoostGraph;
5854 typename boost::graph_traits<BoostGraph>::edge_iterator EdgeIterator;
5855 typedef typename boost::graph_traits<BoostGraph>::vertex_iterator
5857 typedef typename boost::graph_traits<BoostGraph>::edge_descriptor
5859 typedef typename boost::graph_traits<BoostGraph>::vertex_descriptor
5861 typename morphee::graph::CommonGraph32::EdgeProperty tmp;
5862 typename morphee::graph::CommonGraph32::VertexProperty vdata1;
5863 typename morphee::graph::CommonGraph32::VertexProperty label1, label2,
5865 EdgeIterator ed_it, ed_end, ed_it2, ed_end2;
5866 VertexIterator v_it, v_end;
5867 EdgeDescriptor last_edge, e1, e2;
5871 std::vector<double> val_edges;
5872 val_edges.push_back(0.0);
5873 double last_edge_value = 0.0;
5875 std::vector<EdgeDescriptor> removed_edges;
5877 morphee::graph::CommonGraph32 G(0);
5878 morphee::graph::CommonGraph32 G2(0);
5879 morphee::graph::CommonGraph32 Tree(0);
5883 morphee::graphalgo::
5884 t_NeighborhoodGraphFromMosaic_WithMeanGradientValue_AndQuadError(
5885 imIn, imGrad, imVal, alpha, nl, G);
5887 Tree = morphee::graph::MinimumSpanningTreeFromGraph(G);
5889 ImageIn ImTempSurfaces = imIn.getSame();
5891 morphee::morphoBase::t_ImLabelFlatZonesWithArea(imIn, nl, ImTempSurfaces);
5896 morphee::graph::CommonGraph32 Tree_temp = t_CopyGraph(Tree);
5897 morphee::graph::CommonGraph32 Tree_out = t_CopyGraph(Tree);
5898 morphee::graph::CommonGraph32 Tree2 = t_CopyGraph(Tree_temp);
5900 morphee::graph::t_ProjectMarkersOnGraph(ImTempSurfaces, imIn, Tree);
5903 for (boost::tie(v_it, v_end) = boost::vertices(Tree.getBoostGraph());
5904 v_it != v_end; ++v_it) {
5905 Tree.vertexData(*v_it, &vdata1);
5909 for (boost::tie(ed_it, ed_end) = boost::edges(G.getBoostGraph());
5910 ed_it != ed_end; ++ed_it) {
5911 G.setEdgeWeight(*ed_it, 0);
5914 for (boost::tie(ed_it, ed_end) = boost::edges(Tree.getBoostGraph());
5915 ed_it != ed_end; ++ed_it) {
5916 Tree.edgeWeight(*ed_it, &tmp);
5919 val_edges.push_back(tmp);
5920 last_edge_value = (double) tmp;
5925 std::cout <<
"sort" << std::endl;
5927 std::sort(val_edges.begin(), val_edges.end(), std::less<int>());
5929 last_edge_value = val_edges.back();
5930 double last_analyzed = last_edge_value;
5932 while (val_edges.size() > 1) {
5937 for (boost::tie(ed_it, ed_end) = boost::edges(Tree.getBoostGraph());
5938 ed_it != ed_end; ++ed_it) {
5940 Tree.edgeWeight(*ed_it, &tmp);
5942 if (tmp == last_edge_value) {
5943 boost::tie(e1, in1) =
5944 boost::edge(Tree.edgeSource(*ed_it), Tree.edgeTarget(*ed_it),
5945 Tree_temp.getBoostGraph());
5946 removed_edges.push_back(e1);
5947 Tree_temp.removeEdge(
5948 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
5949 Tree.edgeSource(*ed_it)),
5950 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
5951 Tree.edgeTarget(*ed_it)));
5957 t_LabelConnectedComponent(Tree_temp, Tree2);
5959 while (removed_edges.size() > 0) {
5960 e1 = removed_edges.back();
5961 removed_edges.pop_back();
5964 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
5965 Tree.edgeSource(e1)),
5968 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
5969 Tree.edgeTarget(e1)),
5974 double perimeter_1 = 0;
5975 double perimeter_2 = 0;
5977 for (boost::tie(v_it, v_end) = boost::vertices(Tree2.getBoostGraph());
5978 v_it != v_end; ++v_it) {
5979 Tree2.vertexData(*v_it, &vdata1);
5980 if (vdata1 == label1) {
5981 Tree.vertexData(*v_it, &vdata1);
5982 S1 = S1 + (double) vdata1;
5983 }
else if (vdata1 == label2) {
5984 Tree.vertexData(*v_it, &vdata1);
5985 S2 = S2 + (double) vdata1;
5989 for (boost::tie(ed_it2, ed_end2) = boost::edges(G.getBoostGraph());
5990 ed_it2 != ed_end2; ++ed_it2) {
5992 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
5993 Tree.edgeSource(*ed_it2)),
5996 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
5997 Tree.edgeTarget(*ed_it2)),
6000 if ((label21 == label1 && label22 != label1) ||
6001 (label21 != label1 && label22 == label1)) {
6002 perimeter_1 = perimeter_1 + 1.0;
6004 if ((label21 != label2 && label22 == label2) ||
6005 (label21 == label2 && label22 != label2)) {
6006 perimeter_2 = perimeter_2 + 1.0;
6022 double probability =
6029 if (probability > 0.0) {
6030 RES_C res = Tree_out.edgeFromVertices(
6031 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6032 Tree.edgeSource(e1)),
6033 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6034 Tree.edgeTarget(e1)),
6036 Tree_out.setEdgeWeight(e2, 255.0 * probability);
6038 RES_C res = Tree_out.edgeFromVertices(
6039 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6040 Tree.edgeSource(e1)),
6041 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6042 Tree.edgeTarget(e1)),
6044 Tree_out.setEdgeWeight(e2, 0.0);
6048 while (last_edge_value == last_analyzed) {
6049 last_edge_value = val_edges.back();
6050 val_edges.pop_back();
6052 last_analyzed = last_edge_value;
6055 std::cout <<
"project on graphs" << std::endl;
6059 Tree2 = t_CopyGraph(Tree_out);
6060 Tree_temp = t_CopyGraph(Tree_out);
6062 for (boost::tie(ed_it, ed_end) = boost::edges(Tree_out.getBoostGraph());
6063 ed_it != ed_end; ++ed_it) {
6064 Tree_out.edgeWeight(*ed_it, &tmp);
6065 double value = (double) tmp;
6068 Tree_temp.removeEdge(
6069 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6070 Tree_out.edgeSource(*ed_it)),
6071 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6072 Tree_out.edgeTarget(*ed_it)));
6076 t_LabelConnectedComponent(Tree_temp, Tree2);
6079 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6080 Tree_out.edgeSource(*ed_it)),
6081 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6082 Tree_out.edgeTarget(*ed_it)),
6085 for (boost::tie(ed_it2, ed_end2) = boost::edges(G.getBoostGraph());
6086 ed_it2 != ed_end2; ++ed_it2) {
6088 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6089 G.edgeSource(*ed_it2)),
6092 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6093 G.edgeTarget(*ed_it2)),
6096 if (label1 != label2) {
6097 G.edgeWeight(*ed_it2, &tmp);
6098 G.setEdgeWeight(*ed_it2, std::max((
double) tmp, value));
6105 std::cout <<
"project on image the pdf" << std::endl;
6107 typename ImageOut::const_iterator it, iend;
6108 morphee::selement::Neighborhood<SE, ImageOut> neighb(imOut, nl);
6109 typename morphee::selement::Neighborhood<SE, ImageOut>::iterator nit,
6113 for (it = imOut.begin(), iend = imOut.end(); it != iend;
6116 o0 = it.getOffset();
6117 imOut.setPixel(o0, 0);
6120 for (it = imOut.begin(), iend = imOut.end(); it != iend;
6123 o0 = it.getOffset();
6124 int val1 = imIn.pixelFromOffset(o0);
6125 double valout1 = imOut.pixelFromOffset(o0);
6127 neighb.setCenter(o0);
6129 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
6130 o1 = nit.getOffset();
6133 int val2 = imIn.pixelFromOffset(o1);
6134 double valout2 = imOut.pixelFromOffset(o1);
6137 RES_C res = G.edgeFromVertices(val1 - 1, val2 - 1, &e1);
6138 if (res == RES_OK) {
6139 G.edgeWeight(e1, &tmp);
6141 imOut.setPixel(o0, std::max(valout1, (
double) tmp));
6142 imOut.setPixel(o1, std::max(valout2, (
double) tmp));
6145 RES_C res = G.edgeFromVertices(val2 - 1, val1 - 1, &e1);
6146 if (res == RES_OK) {
6147 G.edgeWeight(e1, &tmp);
6149 imOut.setPixel(o0, std::max(valout1, (
double) tmp));
6150 imOut.setPixel(o1, std::max(valout2, (
double) tmp));
6166 template <
class BoostGraph>
6167 const BoostGraph t_CopyGraph(
const BoostGraph &graphIn)
6169 MORPHEE_ENTER_FUNCTION(
"t_CopyGraph");
6171 typedef typename BoostGraph::EdgeIterator EdgeIterator;
6172 typedef typename BoostGraph::VertexIterator VertexIterator;
6174 EdgeIterator ed_it, ed_end;
6175 VertexIterator v_it, v_end;
6177 BoostGraph GCopy(graphIn.numVertices());
6178 typename BoostGraph::EdgeProperty tmp;
6179 typename BoostGraph::VertexProperty vdata1;
6181 for (boost::tie(ed_it, ed_end) = boost::edges(graphIn.getBoostGraph());
6182 ed_it != ed_end; ++ed_it) {
6183 graphIn.edgeWeight(*ed_it,
6186 typename BoostGraph::VertexDescriptor(graphIn.edgeSource(*ed_it)),
6187 typename BoostGraph::VertexDescriptor(graphIn.edgeTarget(*ed_it)),
6191 for (boost::tie(v_it, v_end) = boost::vertices(graphIn.getBoostGraph());
6192 v_it != v_end; ++v_it) {
6193 graphIn.vertexData(*v_it, &vdata1);
6194 GCopy.setVertexData(*v_it, vdata1);
6200 template <
class Graph>
6201 RES_C t_LabelConnectedComponent(
const Graph &GIn, Graph &Gout)
6203 MORPHEE_ENTER_FUNCTION(
"t_LabelConnectedComponent");
6205 typedef typename Graph::VertexDescriptor VertexDescriptor;
6206 typedef typename Graph::_boostGraph BoostGraph;
6208 typename boost::graph_traits<BoostGraph>::edge_iterator EdgeIterator;
6209 typedef typename boost::graph_traits<BoostGraph>::vertex_iterator
6211 typedef typename boost::graph_traits<BoostGraph>::edge_descriptor
6214 EdgeIterator ed_it, ed_end;
6215 VertexIterator u_iter, u_end;
6217 Gout = t_CopyGraph(GIn);
6219 std::vector<int> component(boost::num_vertices(GIn.getBoostGraph()));
6220 int num = connected_components(GIn.getBoostGraph(), &component[0]);
6223 for (boost::tie(u_iter, u_end) = boost::vertices(GIn.getBoostGraph());
6224 u_iter != u_end; ++u_iter) {
6225 Gout.setVertexData(*u_iter, component[*u_iter] + 1);
6232 template <
class Graph>
6233 RES_C t_LabelConnectedComponent(
const Graph &GIn, Graph &Gout,
int *num)
6235 MORPHEE_ENTER_FUNCTION(
"t_LabelConnectedComponent");
6237 typedef typename Graph::VertexDescriptor VertexDescriptor;
6238 typedef typename Graph::_boostGraph BoostGraph;
6240 typename boost::graph_traits<BoostGraph>::edge_iterator EdgeIterator;
6241 typedef typename boost::graph_traits<BoostGraph>::vertex_iterator
6243 typedef typename boost::graph_traits<BoostGraph>::edge_descriptor
6246 EdgeIterator ed_it, ed_end;
6247 VertexIterator u_iter, u_end;
6249 Gout = t_CopyGraph(GIn);
6251 std::vector<int> component(boost::num_vertices(GIn.getBoostGraph()));
6252 *num = connected_components(GIn.getBoostGraph(), &component[0]);
6255 for (boost::tie(u_iter, u_end) = boost::vertices(GIn.getBoostGraph());
6256 u_iter != u_end; ++u_iter) {
6257 Gout.setVertexData(*u_iter, component[*u_iter] + 1);
6264 template <
class ImageIn,
class ImageGradx,
class ImageGrady,
6265 class ImageMarker,
class SE,
class ImageOut>
6266 RES_C t_geoCuts(
const ImageIn &imIn,
const ImageGradx &imGradx,
6267 const ImageGrady &imGrady,
const ImageMarker &imMarker,
6268 const SE &nl, ImageOut &imOut)
6270 MORPHEE_ENTER_FUNCTION(
"t_GeoCuts");
6272 std::cout <<
"Enter function Geo-Cuts" << std::endl;
6274 if ((!imOut.isAllocated())) {
6275 MORPHEE_REGISTER_ERROR(
"Not allocated");
6276 return RES_NOT_ALLOCATED;
6279 if ((!imIn.isAllocated())) {
6280 MORPHEE_REGISTER_ERROR(
"Not allocated");
6281 return RES_NOT_ALLOCATED;
6284 if ((!imGradx.isAllocated())) {
6285 MORPHEE_REGISTER_ERROR(
"Not allocated");
6286 return RES_NOT_ALLOCATED;
6289 if ((!imGrady.isAllocated())) {
6290 MORPHEE_REGISTER_ERROR(
"Not allocated");
6291 return RES_NOT_ALLOCATED;
6294 if ((!imMarker.isAllocated())) {
6295 MORPHEE_REGISTER_ERROR(
"Not allocated");
6296 return RES_NOT_ALLOCATED;
6300 typename ImageIn::const_iterator it, iend;
6301 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
6302 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
6307 typedef boost::adjacency_list_traits<boost::vecS, boost::vecS,
6310 typedef boost::adjacency_list<
6311 boost::listS, boost::vecS, boost::directedS,
6312 boost::property<boost::vertex_name_t, std::string>,
6314 boost::edge_capacity_t, double,
6315 boost::property<boost::edge_residual_capacity_t, double,
6316 boost::property<boost::edge_reverse_t,
6317 Traits::edge_descriptor>>>>
6324 boost::property_map<Graph_d, boost::edge_capacity_t>::type capacity =
6325 boost::get(boost::edge_capacity, g);
6327 boost::property_map<Graph_d, boost::edge_reverse_t>::type rev =
6328 get(boost::edge_reverse, g);
6330 boost::property_map<Graph_d, boost::edge_residual_capacity_t>::type
6331 residual_capacity = get(boost::edge_residual_capacity, g);
6334 Graph_d::edge_descriptor e1, e2, e3, e4;
6335 Graph_d::vertex_descriptor vSource, vSink;
6338 std::cout <<
"build graph vertices" << std::endl;
6339 for (it = imIn.begin(), iend = imIn.end(); it != iend;
6342 o0 = it.getOffset();
6343 boost::add_vertex(g);
6347 std::cout <<
"number of vertices: " << numVert << std::endl;
6349 vSource = boost::add_vertex(g);
6350 vSink = boost::add_vertex(g);
6352 std::cout <<
"build graph edges" << std::endl;
6353 for (it = imIn.begin(), iend = imIn.end(); it != iend;
6356 o1 = it.getOffset();
6357 double val = imIn.pixelFromOffset(o1);
6358 int marker = imMarker.pixelFromOffset(o1);
6366 boost::tie(e4, in1) = boost::add_edge(vSource, o1, g);
6367 boost::tie(e3, in1) = boost::add_edge(o1, vSource, g);
6368 capacity[e4] = (std::numeric_limits<double>::max)();
6369 capacity[e3] = (std::numeric_limits<double>::max)();
6372 }
else if (marker == 3) {
6373 boost::tie(e3, in1) = boost::add_edge(o1, vSink, g);
6374 boost::tie(e4, in1) = boost::add_edge(vSink, o1, g);
6375 capacity[e3] = (std::numeric_limits<double>::max)();
6376 capacity[e4] = (std::numeric_limits<double>::max)();
6381 neighb.setCenter(o1);
6383 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
6384 const offset_t o2 = nit.getOffset();
6388 double val2 = imIn.pixelFromOffset(o2);
6389 double cost = 1000 / (1 + 1.5 * (val - val2) * (val - val2));
6391 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
6392 boost::tie(e3, in1) = boost::add_edge(o2, o1, g);
6393 capacity[e4] = cost;
6394 capacity[e3] = cost;
6400 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
6401 const offset_t o2 = nit.getOffset();
6404 else if (o2 == o1 - 1)
6405 valleft = (int) (imGradx.pixelFromOffset(o2));
6406 else if (o2 == o1 + 1)
6407 valright = (int) (imGradx.pixelFromOffset(o2));
6408 else if (o2 > o1 + 1)
6409 valdown = (int) (imGrady.pixelFromOffset(o2));
6410 else if (o2 < o1 - 1)
6411 valup = (int) (imGrady.pixelFromOffset(o2));
6416 double divergence = 10 * ((valleft - valright) + (valup - valdown));
6418 if (divergence < 0 && marker == 0) {
6419 boost::tie(e4, in1) = boost::add_edge(vSource, o1, g);
6420 boost::tie(e3, in1) = boost::add_edge(o1, vSource, g);
6421 capacity[e4] = std::abs(divergence);
6422 capacity[e3] = std::abs(divergence);
6425 }
else if (divergence > 0 && marker == 0) {
6426 boost::tie(e4, in1) = boost::add_edge(o1, vSink, g);
6427 boost::tie(e3, in1) = boost::add_edge(vSink, o1, g);
6428 capacity[e4] = divergence;
6429 capacity[e3] = divergence;
6435 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap =
6436 boost::get(boost::vertex_index, g);
6437 std::vector<boost::default_color_type> color(boost::num_vertices(g));
6439 std::cout <<
"Compute Max flow" << std::endl;
6440 #if BOOST_VERSION >= 104700
6442 boykov_kolmogorov_max_flow(g, capacity, residual_capacity, rev,
6443 &color[0], indexmap, vSource, vSink);
6445 double flow = kolmogorov_max_flow(g, capacity, residual_capacity, rev,
6446 &color[0], indexmap, vSource, vSink);
6449 std::cout <<
"c The total flow:" << std::endl;
6450 std::cout <<
"s " << flow << std::endl << std::endl;
6452 for (it = imIn.begin(), iend = imIn.end(); it != iend;
6455 o1 = it.getOffset();
6456 if (color[o1] == color[vSource])
6457 imOut.setPixel(o1, 2);
6459 imOut.setPixel(o1, 4);
6460 if (color[o1] == color[vSink])
6461 imOut.setPixel(o1, 3);
6467 template <
class ImageIn,
class ImageMarker,
class SE,
class ImageOut>
6468 RES_C t_geoCutsMinSurfaces(
const ImageIn &imIn,
6469 const ImageMarker &imMarker,
const SE &nl,
6472 MORPHEE_ENTER_FUNCTION(
"t_geoCutsMinSurfaces");
6474 std::cout <<
"Enter function Geo-Cuts " << std::endl;
6476 if ((!imOut.isAllocated())) {
6477 MORPHEE_REGISTER_ERROR(
"Not allocated");
6478 return RES_NOT_ALLOCATED;
6481 if ((!imIn.isAllocated())) {
6482 MORPHEE_REGISTER_ERROR(
"Not allocated");
6483 return RES_NOT_ALLOCATED;
6486 if ((!imMarker.isAllocated())) {
6487 MORPHEE_REGISTER_ERROR(
"Not allocated");
6488 return RES_NOT_ALLOCATED;
6492 typename ImageIn::const_iterator it, iend;
6493 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
6494 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
6499 typedef boost::adjacency_list_traits<boost::vecS, boost::vecS,
6502 typedef boost::adjacency_list<
6503 boost::listS, boost::vecS, boost::directedS,
6504 boost::property<boost::vertex_name_t, std::string>,
6506 boost::edge_capacity_t, double,
6507 boost::property<boost::edge_residual_capacity_t, double,
6508 boost::property<boost::edge_reverse_t,
6509 Traits::edge_descriptor>>>>
6516 boost::property_map<Graph_d, boost::edge_capacity_t>::type capacity =
6517 boost::get(boost::edge_capacity, g);
6519 boost::property_map<Graph_d, boost::edge_reverse_t>::type rev =
6520 get(boost::edge_reverse, g);
6522 boost::property_map<Graph_d, boost::edge_residual_capacity_t>::type
6523 residual_capacity = get(boost::edge_residual_capacity, g);
6526 Graph_d::edge_descriptor e1, e2, e3, e4;
6527 Graph_d::vertex_descriptor vSource, vSink;
6531 std::cout <<
"build graph vertices" << std::endl;
6532 for (it = imIn.begin(), iend = imIn.end(); it != iend;
6535 o0 = it.getOffset();
6536 boost::add_vertex(g);
6540 std::cout <<
"number of vertices: " << numVert << std::endl;
6542 vSource = boost::add_vertex(g);
6543 vSink = boost::add_vertex(g);
6545 std::cout <<
"build graph edges" << std::endl;
6546 for (it = imIn.begin(), iend = imIn.end(); it != iend;
6549 o1 = it.getOffset();
6550 double val = imIn.pixelFromOffset(o1);
6551 int marker = imMarker.pixelFromOffset(o1);
6559 boost::tie(e4, in1) = boost::add_edge(vSource, o1, g);
6560 boost::tie(e3, in1) = boost::add_edge(o1, vSource, g);
6561 capacity[e4] = (std::numeric_limits<double>::max)();
6562 capacity[e3] = (std::numeric_limits<double>::max)();
6565 }
else if (marker == 3) {
6566 boost::tie(e4, in1) = boost::add_edge(o1, vSink, g);
6567 boost::tie(e3, in1) = boost::add_edge(vSink, o1, g);
6568 capacity[e4] = (std::numeric_limits<double>::max)();
6569 capacity[e3] = (std::numeric_limits<double>::max)();
6574 neighb.setCenter(o1);
6576 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
6577 const offset_t o2 = nit.getOffset();
6582 double val2 = imIn.pixelFromOffset(o2);
6583 double maxi = std::abs(val2 - val);
6584 double cost = 256 / (1 +
std::pow(maxi, 2));
6585 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
6586 boost::tie(e3, in1) = boost::add_edge(o2, o1, g);
6587 capacity[e4] = cost;
6588 capacity[e3] = cost;
6595 std::cout <<
"number of Edges : " << numEdges << std::endl;
6597 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap =
6598 boost::get(boost::vertex_index, g);
6599 std::vector<boost::default_color_type> color(boost::num_vertices(g));
6601 std::cout <<
"Compute Max flow " << std::endl;
6605 #if BOOST_VERSION >= 104700
6607 boykov_kolmogorov_max_flow(g, capacity, residual_capacity, rev,
6608 &color[0], indexmap, vSource, vSink);
6610 double flow2 = kolmogorov_max_flow(g, capacity, residual_capacity, rev,
6611 &color[0], indexmap, vSource, vSink);
6613 std::cout <<
"c The total flow found :" << std::endl;
6614 std::cout <<
"s " << flow2 << std::endl << std::endl;
6616 for (it = imIn.begin(), iend = imIn.end(); it != iend;
6619 o1 = it.getOffset();
6620 if (color[o1] == color[vSource])
6621 imOut.setPixel(o1, 2);
6622 else if (color[o1] == color[vSink])
6623 imOut.setPixel(o1, 3);
6624 else if (color[o1] == 1)
6625 imOut.setPixel(o1, 4);
6631 template <
class ImageIn,
class ImageVal,
typename _nbmarkers,
class SE,
6633 RES_C t_geoCutsStochastic_Watershed(
const ImageIn &imIn,
6634 const ImageVal &imVal,
6635 const _nbmarkers nbmarkers,
6636 const SE &nl, ImageOut &imOut)
6638 MORPHEE_ENTER_FUNCTION(
"t_geoCutsStochastic_Watershed");
6640 std::cout <<
"Enter function Geo-Cuts Stochastic Watershed" << std::endl;
6642 if ((!imOut.isAllocated())) {
6643 MORPHEE_REGISTER_ERROR(
"Not allocated");
6644 return RES_NOT_ALLOCATED;
6647 if ((!imIn.isAllocated())) {
6648 MORPHEE_REGISTER_ERROR(
"Not allocated");
6649 return RES_NOT_ALLOCATED;
6652 if ((!imVal.isAllocated())) {
6653 MORPHEE_REGISTER_ERROR(
"Not allocated");
6654 return RES_NOT_ALLOCATED;
6657 double markers = (double) nbmarkers;
6659 int size = imIn.getXSize() * imIn.getYSize() * imIn.getZSize();
6662 typedef typename morphee::graph::CommonGraph32::_boostGraph BoostGraph;
6664 typename boost::graph_traits<BoostGraph>::edge_iterator EdgeIterator;
6665 typedef typename boost::graph_traits<BoostGraph>::vertex_iterator
6667 typedef typename boost::graph_traits<BoostGraph>::edge_descriptor
6669 typedef typename boost::graph_traits<BoostGraph>::vertex_descriptor
6671 typename morphee::graph::CommonGraph32::EdgeProperty tmp;
6672 typename morphee::graph::CommonGraph32::VertexProperty vdata1;
6673 typename morphee::graph::CommonGraph32::VertexProperty label1, label2;
6674 EdgeIterator ed_it, ed_end, ed_it2, ed_end2;
6675 VertexIterator v_it, v_end;
6676 EdgeDescriptor last_edge, e1, e2;
6680 std::vector<double> val_edges;
6681 val_edges.push_back(0.0);
6682 double last_edge_value = 0.0;
6684 std::vector<EdgeDescriptor> removed_edges;
6686 morphee::graph::CommonGraph32 G(0);
6687 morphee::graph::CommonGraph32 Tree(0);
6689 morphee::morphoBase::t_NeighborhoodGraphFromMosaic_WithPass(imIn, imVal,
6691 Tree = morphee::graph::MinimumSpanningTreeFromGraph(G);
6693 ImageIn ImTempSurfaces = imIn.getSame();
6696 morphee::morphoBase::t_ImLabelFlatZonesWithVolume(imIn, imVal, nl,
6708 morphee::graph::CommonGraph32 Tree_temp = t_CopyGraph(Tree);
6709 morphee::graph::CommonGraph32 Tree_out = t_CopyGraph(Tree);
6710 morphee::graph::CommonGraph32 Tree2 = t_CopyGraph(Tree_temp);
6712 morphee::graph::t_ProjectMarkersOnGraph(ImTempSurfaces, imIn, Tree);
6715 for (boost::tie(v_it, v_end) = boost::vertices(G.getBoostGraph());
6716 v_it != v_end; ++v_it) {
6717 Tree.vertexData(*v_it, &vdata1);
6721 for (boost::tie(ed_it, ed_end) = boost::edges(G.getBoostGraph());
6722 ed_it != ed_end; ++ed_it) {
6723 G.setEdgeWeight(*ed_it, 0);
6726 for (boost::tie(ed_it, ed_end) = boost::edges(Tree.getBoostGraph());
6727 ed_it != ed_end; ++ed_it) {
6728 Tree.edgeWeight(*ed_it, &tmp);
6729 if ((
double) tmp > last_edge_value) {
6730 val_edges.push_back(tmp);
6731 last_edge_value = (double) tmp;
6735 while (val_edges.size() > 1) {
6736 last_edge_value = val_edges.back();
6737 val_edges.pop_back();
6740 for (boost::tie(ed_it, ed_end) = boost::edges(Tree.getBoostGraph());
6741 ed_it != ed_end; ++ed_it) {
6743 Tree.edgeWeight(*ed_it, &tmp);
6745 if (tmp == last_edge_value) {
6746 boost::tie(e1, in1) =
6747 boost::edge(Tree.edgeSource(*ed_it), Tree.edgeTarget(*ed_it),
6748 Tree_temp.getBoostGraph());
6749 removed_edges.push_back(e1);
6750 Tree_temp.removeEdge(
6751 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6752 Tree.edgeSource(*ed_it)),
6753 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6754 Tree.edgeTarget(*ed_it)));
6758 t_LabelConnectedComponent(Tree_temp, Tree2);
6760 while (removed_edges.size() > 0) {
6761 e1 = removed_edges.back();
6762 removed_edges.pop_back();
6765 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6766 Tree.edgeSource(e1)),
6769 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6770 Tree.edgeTarget(e1)),
6776 for (boost::tie(v_it, v_end) = boost::vertices(Tree2.getBoostGraph());
6777 v_it != v_end; ++v_it) {
6778 Tree2.vertexData(*v_it, &vdata1);
6779 if (vdata1 == label1) {
6780 Tree.vertexData(*v_it, &vdata1);
6781 S1 = S1 + (double) vdata1;
6782 }
else if (vdata1 == label2) {
6783 Tree.vertexData(*v_it, &vdata1);
6784 S2 = S2 + (double) vdata1;
6791 double probability =
6798 if (probability > 0.0) {
6799 RES_C res = Tree_out.edgeFromVertices(
6800 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6801 Tree.edgeSource(e1)),
6802 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6803 Tree.edgeTarget(e1)),
6805 Tree_out.setEdgeWeight(e2, 255.0 * probability);
6807 RES_C res = Tree_out.edgeFromVertices(
6808 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6809 Tree.edgeSource(e1)),
6810 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6811 Tree.edgeTarget(e1)),
6813 Tree_out.setEdgeWeight(e2, 0.0);
6818 std::cout <<
"project on graphs" << std::endl;
6819 Tree2 = t_CopyGraph(Tree_out);
6820 Tree_temp = t_CopyGraph(Tree_out);
6822 for (boost::tie(ed_it, ed_end) = boost::edges(Tree_out.getBoostGraph());
6823 ed_it != ed_end; ++ed_it) {
6824 Tree_out.edgeWeight(*ed_it, &tmp);
6825 double value = (double) tmp;
6828 Tree_temp.removeEdge(
6829 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6830 Tree_out.edgeSource(*ed_it)),
6831 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6832 Tree_out.edgeTarget(*ed_it)));
6834 t_LabelConnectedComponent(Tree_temp, Tree2);
6837 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6838 Tree_out.edgeSource(*ed_it)),
6839 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6840 Tree_out.edgeTarget(*ed_it)),
6843 for (boost::tie(ed_it2, ed_end2) = boost::edges(G.getBoostGraph());
6844 ed_it2 != ed_end2; ++ed_it2) {
6846 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6847 G.edgeSource(*ed_it2)),
6850 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
6851 G.edgeTarget(*ed_it2)),
6854 if (label1 != label2) {
6855 G.edgeWeight(*ed_it2, &tmp);
6856 G.setEdgeWeight(*ed_it2, std::max((
double) tmp, value));
6863 std::cout <<
"project on image the pdf" << std::endl;
6865 typename ImageOut::const_iterator it, iend;
6866 morphee::selement::Neighborhood<SE, ImageOut> neighb(imOut, nl);
6867 typename morphee::selement::Neighborhood<SE, ImageOut>::iterator nit,
6871 for (it = imOut.begin(), iend = imOut.end(); it != iend;
6874 o0 = it.getOffset();
6875 imOut.setPixel(o0, 0);
6878 for (it = imOut.begin(), iend = imOut.end(); it != iend;
6881 o0 = it.getOffset();
6882 int val1 = imIn.pixelFromOffset(o0);
6883 double valout1 = imOut.pixelFromOffset(o0);
6885 neighb.setCenter(o0);
6887 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
6888 o1 = nit.getOffset();
6891 int val2 = imIn.pixelFromOffset(o1);
6892 double valout2 = imOut.pixelFromOffset(o1);
6895 RES_C res = G.edgeFromVertices(val1 - 1, val2 - 1, &e1);
6896 if (res == RES_OK) {
6897 G.edgeWeight(e1, &tmp);
6899 imOut.setPixel(o0, std::max(valout1, (
double) tmp));
6900 imOut.setPixel(o1, std::max(valout2, (
double) tmp));
6903 RES_C res = G.edgeFromVertices(val2 - 1, val1 - 1, &e1);
6904 if (res == RES_OK) {
6905 G.edgeWeight(e1, &tmp);
6907 imOut.setPixel(o0, std::max(valout1, (
double) tmp));
6908 imOut.setPixel(o1, std::max(valout2, (
double) tmp));
6920 template <
class ImageIn,
class ImageVal,
class ImageMark,
6921 typename _nbmarkers,
class SE,
class ImageOut>
6922 RES_C t_geoCutsStochastic_Watershed_2(
const ImageIn &imIn,
6923 const ImageVal &imVal,
6924 const ImageMark &imMark,
6925 const _nbmarkers nbmarkers,
6926 const SE &nl, ImageOut &imOut)
6928 MORPHEE_ENTER_FUNCTION(
"t_geoCutsStochastic_Watershed_2");
6930 std::cout <<
"Enter function Geo-Cuts Stochastic Watershed_2"
6933 if ((!imOut.isAllocated())) {
6934 MORPHEE_REGISTER_ERROR(
"Not allocated");
6935 return RES_NOT_ALLOCATED;
6938 if ((!imIn.isAllocated())) {
6939 MORPHEE_REGISTER_ERROR(
"Not allocated");
6940 return RES_NOT_ALLOCATED;
6943 if ((!imVal.isAllocated())) {
6944 MORPHEE_REGISTER_ERROR(
"Not allocated");
6945 return RES_NOT_ALLOCATED;
6948 if ((!imMark.isAllocated())) {
6949 MORPHEE_REGISTER_ERROR(
"Not allocated");
6950 return RES_NOT_ALLOCATED;
6953 double markers = (double) nbmarkers;
6955 int size = imIn.getXSize() * imIn.getYSize() * imIn.getZSize();
6958 typedef typename morphee::graph::CommonGraph32::_boostGraph BoostGraph;
6960 typename boost::graph_traits<BoostGraph>::edge_iterator EdgeIterator;
6961 typedef typename boost::graph_traits<BoostGraph>::vertex_iterator
6963 typedef typename boost::graph_traits<BoostGraph>::edge_descriptor
6965 typedef typename boost::graph_traits<BoostGraph>::vertex_descriptor
6967 typename morphee::graph::CommonGraph32::EdgeProperty tmp;
6968 typename morphee::graph::CommonGraph32::VertexProperty vdata1, vdata2;
6969 typename morphee::graph::CommonGraph32::VertexProperty label1, label2;
6970 EdgeIterator ed_it, ed_end, ed_it2, ed_end2;
6971 VertexIterator v_it, v_end;
6972 EdgeDescriptor last_edge, e1, e2;
6975 std::vector<double> val_edges;
6976 val_edges.push_back(0.0);
6977 double last_edge_value = 0.0;
6979 std::vector<EdgeDescriptor> removed_edges;
6981 morphee::graph::CommonGraph32 G(0);
6982 morphee::graph::CommonGraph32 Tree(0);
6983 morphee::morphoBase::t_NeighborhoodGraphFromMosaic_WithPass(imIn, imVal,
6986 Tree = morphee::graph::MinimumSpanningTreeFromGraph(G);
6988 ImageIn ImTempSurfaces = imIn.getSame();
6990 morphee::morphoBase::t_ImLabelFlatZonesWithArea(imIn, nl, ImTempSurfaces);
6999 morphee::graph::CommonGraph32 Tree_temp = t_CopyGraph(Tree);
7000 morphee::graph::CommonGraph32 Tree_out = t_CopyGraph(Tree);
7001 morphee::graph::CommonGraph32 Tree2 = t_CopyGraph(Tree_temp);
7002 morphee::graph::CommonGraph32 TreeMark = t_CopyGraph(Tree);
7004 morphee::graph::t_ProjectMarkersOnGraph(ImTempSurfaces, imIn, Tree);
7006 morphee::graph::t_ProjectMarkersOnGraph(imMark, imIn, TreeMark);
7009 for (boost::tie(v_it, v_end) = boost::vertices(G.getBoostGraph());
7010 v_it != v_end; ++v_it) {
7011 Tree.vertexData(*v_it, &vdata1);
7015 for (boost::tie(ed_it, ed_end) = boost::edges(G.getBoostGraph());
7016 ed_it != ed_end; ++ed_it) {
7017 G.setEdgeWeight(*ed_it, 0);
7020 for (boost::tie(ed_it, ed_end) = boost::edges(Tree.getBoostGraph());
7021 ed_it != ed_end; ++ed_it) {
7022 Tree.edgeWeight(*ed_it, &tmp);
7023 if ((
double) tmp > last_edge_value) {
7024 val_edges.push_back(tmp);
7025 last_edge_value = (double) tmp;
7029 while (val_edges.size() > 1) {
7030 last_edge_value = val_edges.back();
7031 val_edges.pop_back();
7034 for (boost::tie(ed_it, ed_end) = boost::edges(Tree.getBoostGraph());
7035 ed_it != ed_end; ++ed_it) {
7037 Tree.edgeWeight(*ed_it, &tmp);
7039 if (tmp == last_edge_value) {
7040 boost::tie(e1, in1) =
7041 boost::edge(Tree.edgeSource(*ed_it), Tree.edgeTarget(*ed_it),
7042 Tree_temp.getBoostGraph());
7043 removed_edges.push_back(e1);
7044 Tree_temp.removeEdge(
7045 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
7046 Tree.edgeSource(*ed_it)),
7047 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
7048 Tree.edgeTarget(*ed_it)));
7052 t_LabelConnectedComponent(Tree_temp, Tree2);
7054 while (removed_edges.size() > 0) {
7055 e1 = removed_edges.back();
7056 removed_edges.pop_back();
7059 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
7060 Tree.edgeSource(e1)),
7063 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
7064 Tree.edgeTarget(e1)),
7070 bool region1 =
false;
7071 bool region2 =
false;
7073 for (boost::tie(v_it, v_end) = boost::vertices(Tree2.getBoostGraph());
7074 v_it != v_end; ++v_it) {
7075 Tree2.vertexData(*v_it, &vdata1);
7076 TreeMark.vertexData(*v_it, &vdata2);
7078 if (vdata1 == label1) {
7079 Tree.vertexData(*v_it, &vdata1);
7080 S1 = S1 + (double) vdata1;
7081 }
else if (vdata1 == label2) {
7082 Tree.vertexData(*v_it, &vdata1);
7083 S2 = S2 + (double) vdata1;
7086 if (vdata1 == label1 && vdata2 > 0) {
7088 }
else if (vdata1 == label2 && vdata2 > 0) {
7096 double probability = 0;
7098 if (region1 ==
false && region2 ==
false) {
7099 probability = 1 -
std::pow(1 - S1 / ((
double)
volume), markers) -
7102 }
else if (region1 ==
true && region2 ==
false) {
7104 }
else if (region1 ==
false && region2 ==
true) {
7106 }
else if (region1 ==
true && region2 ==
true) {
7112 if (probability > 0.0) {
7113 RES_C res = Tree_out.edgeFromVertices(
7114 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
7115 Tree.edgeSource(e1)),
7116 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
7117 Tree.edgeTarget(e1)),
7119 Tree_out.setEdgeWeight(e2, 255.0 * probability);
7121 RES_C res = Tree_out.edgeFromVertices(
7122 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
7123 Tree.edgeSource(e1)),
7124 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
7125 Tree.edgeTarget(e1)),
7127 Tree_out.setEdgeWeight(e2, 0.0);
7132 std::cout <<
"project on graphs" << std::endl;
7133 Tree2 = t_CopyGraph(Tree_out);
7134 Tree_temp = t_CopyGraph(Tree_out);
7136 for (boost::tie(ed_it, ed_end) = boost::edges(Tree_out.getBoostGraph());
7137 ed_it != ed_end; ++ed_it) {
7138 Tree_out.edgeWeight(*ed_it, &tmp);
7139 double value = (double) tmp;
7142 Tree_temp.removeEdge(
7143 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
7144 Tree_out.edgeSource(*ed_it)),
7145 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
7146 Tree_out.edgeTarget(*ed_it)));
7148 t_LabelConnectedComponent(Tree_temp, Tree2);
7151 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
7152 Tree_out.edgeSource(*ed_it)),
7153 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
7154 Tree_out.edgeTarget(*ed_it)),
7157 for (boost::tie(ed_it2, ed_end2) = boost::edges(G.getBoostGraph());
7158 ed_it2 != ed_end2; ++ed_it2) {
7160 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
7161 G.edgeSource(*ed_it2)),
7164 typename boost::graph_traits<BoostGraph>::vertex_descriptor(
7165 G.edgeTarget(*ed_it2)),
7168 if (label1 != label2) {
7169 G.edgeWeight(*ed_it2, &tmp);
7170 G.setEdgeWeight(*ed_it2, std::max((
double) tmp, value));
7177 std::cout <<
"project on image the pdf" << std::endl;
7179 typename ImageOut::const_iterator it, iend;
7180 morphee::selement::Neighborhood<SE, ImageOut> neighb(imOut, nl);
7181 typename morphee::selement::Neighborhood<SE, ImageOut>::iterator nit,
7185 for (it = imOut.begin(), iend = imOut.end(); it != iend;
7188 o0 = it.getOffset();
7189 imOut.setPixel(o0, 0);
7192 for (it = imOut.begin(), iend = imOut.end(); it != iend;
7195 o0 = it.getOffset();
7196 int val1 = imIn.pixelFromOffset(o0);
7197 double valout1 = imOut.pixelFromOffset(o0);
7199 neighb.setCenter(o0);
7201 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
7202 o1 = nit.getOffset();
7205 int val2 = imIn.pixelFromOffset(o1);
7206 double valout2 = imOut.pixelFromOffset(o1);
7209 RES_C res = G.edgeFromVertices(val1 - 1, val2 - 1, &e1);
7210 if (res == RES_OK) {
7211 G.edgeWeight(e1, &tmp);
7213 imOut.setPixel(o0, std::max(valout1, (
double) tmp));
7214 imOut.setPixel(o1, std::max(valout2, (
double) tmp));
7217 RES_C res = G.edgeFromVertices(val2 - 1, val1 - 1, &e1);
7218 if (res == RES_OK) {
7219 G.edgeWeight(e1, &tmp);
7221 imOut.setPixel(o0, std::max(valout1, (
double) tmp));
7222 imOut.setPixel(o1, std::max(valout2, (
double) tmp));
7234 template <
class ImageIn,
class ImageMarker,
typename _Power,
class SE,
7236 RES_C t_geoCutsWatershed_MinCut(
const ImageIn &imIn,
7237 const ImageMarker &imMarker,
7238 const _Power Power,
const SE &nl,
7241 MORPHEE_ENTER_FUNCTION(
"t_geoCutsWatershed_MinCut");
7243 std::cout <<
"Enter function Geo-Cuts Watershed" << std::endl;
7245 if ((!imOut.isAllocated())) {
7246 MORPHEE_REGISTER_ERROR(
"Not allocated");
7247 return RES_NOT_ALLOCATED;
7250 if ((!imIn.isAllocated())) {
7251 MORPHEE_REGISTER_ERROR(
"Not allocated");
7252 return RES_NOT_ALLOCATED;
7255 if ((!imMarker.isAllocated())) {
7256 MORPHEE_REGISTER_ERROR(
"Not allocated");
7257 return RES_NOT_ALLOCATED;
7260 double exposant = (double) Power;
7261 std::cout <<
"exposant = " << exposant << std::endl;
7265 typename ImageIn::const_iterator it, iend;
7266 typename ImageOut::const_iterator ito, ioend;
7267 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
7268 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
7273 typedef boost::adjacency_list_traits<boost::vecS, boost::vecS,
7276 typedef boost::adjacency_list<
7277 boost::listS, boost::vecS, boost::directedS,
7278 boost::property<boost::vertex_name_t, std::string>,
7280 boost::edge_capacity_t, double,
7281 boost::property<boost::edge_residual_capacity_t, double,
7282 boost::property<boost::edge_reverse_t,
7283 Traits::edge_descriptor>>>>
7288 boost::property_map<Graph_d, boost::edge_capacity_t>::type capacity =
7289 boost::get(boost::edge_capacity, g);
7291 boost::property_map<Graph_d, boost::edge_reverse_t>::type rev =
7292 get(boost::edge_reverse, g);
7294 boost::property_map<Graph_d, boost::edge_residual_capacity_t>::type
7295 residual_capacity = get(boost::edge_residual_capacity, g);
7298 Graph_d::edge_descriptor e1, e2, e3, e4;
7299 Graph_d::vertex_descriptor vSource, vSink;
7302 std::cout <<
"build graph vertices" << std::endl;
7303 for (it = imIn.begin(), iend = imIn.end(); it != iend;
7306 o0 = it.getOffset();
7307 boost::add_vertex(g);
7311 std::cout <<
"number of vertices: " << numVert << std::endl;
7313 vSource = boost::add_vertex(g);
7314 vSink = boost::add_vertex(g);
7316 std::cout <<
"build graph edges" << std::endl;
7317 for (it = imIn.begin(), iend = imIn.end(); it != iend;
7320 o1 = it.getOffset();
7321 double val = imIn.pixelFromOffset(o1);
7322 int marker = imMarker.pixelFromOffset(o1);
7330 boost::tie(e4, in1) = boost::add_edge(vSource, o1, g);
7331 boost::tie(e3, in1) = boost::add_edge(o1, vSource, g);
7332 capacity[e4] = (std::numeric_limits<double>::max)();
7333 capacity[e3] = (std::numeric_limits<double>::max)();
7336 }
else if (marker == 3) {
7337 boost::tie(e4, in1) = boost::add_edge(o1, vSink, g);
7338 boost::tie(e3, in1) = boost::add_edge(vSink, o1, g);
7339 capacity[e4] = (std::numeric_limits<double>::max)();
7340 capacity[e3] = (std::numeric_limits<double>::max)();
7345 neighb.setCenter(o1);
7347 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
7348 const offset_t o2 = nit.getOffset();
7352 double val2 = imIn.pixelFromOffset(o2);
7353 double valeur = (255.0 / (std::abs(val - val2) + 1));
7355 double cost =
std::pow(valeur, exposant);
7356 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
7357 boost::tie(e3, in1) = boost::add_edge(o2, o1, g);
7358 capacity[e4] = cost;
7359 capacity[e3] = cost;
7366 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap =
7367 boost::get(boost::vertex_index, g);
7368 std::vector<boost::default_color_type> color(boost::num_vertices(g));
7369 std::vector<boost::graph_traits<Graph_d>::vertex_descriptor> p(
7370 boost::num_vertices(g));
7372 std::cout <<
"Compute Max flow " << std::endl;
7373 #if BOOST_VERSION >= 104700
7375 boykov_kolmogorov_max_flow(g, capacity, residual_capacity, rev,
7376 &color[0], indexmap, vSource, vSink);
7378 double flow = kolmogorov_max_flow(g, capacity, residual_capacity, rev,
7379 &color[0], indexmap, vSource, vSink);
7382 std::cout <<
"c The total flow found :" << std::endl;
7383 std::cout <<
"s " << flow << std::endl << std::endl;
7385 for (it = imIn.begin(), iend = imIn.end(); it != iend;
7388 o1 = it.getOffset();
7389 if (color[o1] == color[vSource])
7390 imOut.setPixel(o1, 2);
7391 else if (color[o1] == color[vSink])
7392 imOut.setPixel(o1, 3);
7393 else if (color[o1] == 1)
7394 imOut.setPixel(o1, 4);
7400 template <
class ImageIn,
class ImageMarker,
class SE,
class ImageOut>
7401 RES_C t_geoCutsWatershed_Prog_MinCut(
const ImageIn &imIn,
7402 const ImageMarker &imMarker,
7403 const SE &nl, ImageOut &imOut)
7405 MORPHEE_ENTER_FUNCTION(
"t_geoCutsWatershed_Prog_MinCut");
7407 std::cout <<
"Enter function geoCutsWatershed_Prog_MinCut" << std::endl;
7409 if ((!imOut.isAllocated())) {
7410 MORPHEE_REGISTER_ERROR(
"Not allocated");
7411 return RES_NOT_ALLOCATED;
7414 if ((!imIn.isAllocated())) {
7415 MORPHEE_REGISTER_ERROR(
"Not allocated");
7416 return RES_NOT_ALLOCATED;
7419 if ((!imMarker.isAllocated())) {
7420 MORPHEE_REGISTER_ERROR(
"Not allocated");
7421 return RES_NOT_ALLOCATED;
7425 typename ImageIn::const_iterator it, iend;
7426 typename ImageOut::const_iterator ito, ioend;
7427 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
7428 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
7433 typedef boost::adjacency_list_traits<boost::vecS, boost::vecS,
7436 typedef boost::adjacency_list<
7437 boost::listS, boost::vecS, boost::directedS,
7438 boost::property<boost::vertex_name_t, std::string>,
7440 boost::edge_capacity_t, double,
7441 boost::property<boost::edge_residual_capacity_t, double,
7442 boost::property<boost::edge_reverse_t,
7443 Traits::edge_descriptor>>>>
7448 boost::property_map<Graph_d, boost::edge_capacity_t>::type capacity =
7449 boost::get(boost::edge_capacity, g);
7451 boost::property_map<Graph_d, boost::edge_reverse_t>::type rev =
7452 get(boost::edge_reverse, g);
7454 boost::property_map<Graph_d, boost::edge_residual_capacity_t>::type
7455 residual_capacity = get(boost::edge_residual_capacity, g);
7458 Graph_d::edge_descriptor e1, e2, e3, e4;
7459 Graph_d::vertex_descriptor vSource, vSink;
7462 std::cout <<
"build graph vertices" << std::endl;
7463 for (it = imIn.begin(), iend = imIn.end(); it != iend;
7466 o0 = it.getOffset();
7467 boost::add_vertex(g);
7471 std::cout <<
"number of vertices: " << numVert << std::endl;
7473 vSource = boost::add_vertex(g);
7474 vSink = boost::add_vertex(g);
7476 std::cout <<
"build graph edges" << std::endl;
7477 for (it = imIn.begin(), iend = imIn.end(); it != iend;
7480 o1 = it.getOffset();
7481 double val = imIn.pixelFromOffset(o1);
7482 int marker = imMarker.pixelFromOffset(o1);
7490 boost::tie(e4, in1) = boost::add_edge(vSource, o1, g);
7491 boost::tie(e3, in1) = boost::add_edge(o1, vSource, g);
7492 capacity[e4] = (std::numeric_limits<double>::max)();
7493 capacity[e3] = (std::numeric_limits<double>::max)();
7496 }
else if (marker == 3) {
7497 boost::tie(e4, in1) = boost::add_edge(o1, vSink, g);
7498 boost::tie(e3, in1) = boost::add_edge(vSink, o1, g);
7499 capacity[e4] = (std::numeric_limits<double>::max)();
7500 capacity[e3] = (std::numeric_limits<double>::max)();
7505 neighb.setCenter(o1);
7507 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
7508 const offset_t o2 = nit.getOffset();
7512 double val2 = imIn.pixelFromOffset(o2);
7513 double valeur = (255.0 / (std::abs(val - val2) + 1));
7514 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
7515 boost::tie(e3, in1) = boost::add_edge(o2, o1, g);
7516 capacity[e4] = valeur;
7517 capacity[e3] = valeur;
7524 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap =
7525 boost::get(boost::vertex_index, g);
7526 std::vector<boost::default_color_type> color(boost::num_vertices(g));
7527 std::vector<boost::graph_traits<Graph_d>::vertex_descriptor> p(
7528 boost::num_vertices(g));
7529 std::cout <<
"Compute Max flow " << std::endl;
7530 #if BOOST_VERSION >= 104700
7532 boykov_kolmogorov_max_flow(g, capacity, residual_capacity, rev,
7533 &color[0], indexmap, vSource, vSink);
7535 double flow = kolmogorov_max_flow(g, capacity, residual_capacity, rev,
7536 &color[0], indexmap, vSource, vSink);
7538 std::cout <<
"c The total flow found :" << std::endl;
7539 std::cout <<
"s " << flow << std::endl << std::endl;
7541 for (it = imIn.begin(), iend = imIn.end(); it != iend;
7544 o1 = it.getOffset();
7545 if (color[o1] == color[vSource])
7546 imOut.setPixel(o1, 0);
7547 if (color[o1] == color[vSink])
7548 imOut.setPixel(o1, 2);
7551 for (
int i = 2; i <= 15; i++) {
7552 for (it = imIn.begin(), iend = imIn.end(); it != iend;
7555 o1 = it.getOffset();
7556 neighb.setCenter(o1);
7558 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
7559 const offset_t o2 = nit.getOffset();
7563 boost::tie(e4, in1) = boost::edge(o1, o2, g);
7564 boost::tie(e3, in1) = boost::edge(o2, o1, g);
7565 double valeur = capacity[e4];
7566 double cost =
std::pow(valeur, i) - residual_capacity[e4];
7567 capacity[e4] = cost;
7568 capacity[e3] = cost;
7574 std::cout <<
"Compute Max flow " << std::endl;
7575 #if BOOST_VERSION >= 104700
7577 boykov_kolmogorov_max_flow(g, capacity, residual_capacity, rev,
7578 &color[0], indexmap, vSource, vSink);
7580 double flow = kolmogorov_max_flow(g, capacity, residual_capacity, rev,
7581 &color[0], indexmap, vSource, vSink);
7583 std::cout <<
"c The total flow found :" << std::endl;
7584 std::cout <<
"s " << flow << std::endl << std::endl;
7586 for (it = imIn.begin(), iend = imIn.end(); it != iend;
7589 o1 = it.getOffset();
7590 int val = imOut.pixelFromOffset(o1);
7591 if (color[o1] == color[vSource])
7592 imOut.setPixel(o1, 0);
7593 if (color[o1] == color[vSink] && val == 0)
7594 imOut.setPixel(o1, 2 + i - 1);
7601 template <
class ImageIn,
class ImageMarker,
typename _Power,
class SE,
7604 t_geoCutsWatershed_SPF(
const ImageIn &imIn,
const ImageMarker &imMarker,
7605 const _Power Power,
const SE &nl, ImageOut &imOut)
7607 MORPHEE_ENTER_FUNCTION(
"t_geoCutsWatershed_SPF");
7609 std::cout <<
"Enter function Geo-Cuts Watershed SPF" << std::endl;
7611 if ((!imOut.isAllocated())) {
7612 MORPHEE_REGISTER_ERROR(
"Not allocated");
7613 return RES_NOT_ALLOCATED;
7616 if ((!imIn.isAllocated())) {
7617 MORPHEE_REGISTER_ERROR(
"Not allocated");
7618 return RES_NOT_ALLOCATED;
7621 if ((!imMarker.isAllocated())) {
7622 MORPHEE_REGISTER_ERROR(
"Not allocated");
7623 return RES_NOT_ALLOCATED;
7626 double exposant = (double) Power;
7627 std::cout <<
"exposant = " << exposant << std::endl;
7630 typename ImageIn::const_iterator it, iend;
7631 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
7632 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
7636 std::queue<int> temp_q;
7641 typedef boost::adjacency_list<
7642 boost::vecS, boost::vecS, boost::undirectedS,
7643 boost::property<boost::vertex_distance_t, double>,
7644 boost::property<boost::edge_capacity_t, double>>
7649 boost::property_map<Graph_d, boost::edge_capacity_t>::type weightmap =
7650 boost::get(boost::edge_capacity, g);
7653 Graph_d::edge_descriptor e1, e2, e3, e4;
7654 Graph_d::vertex_descriptor vRoot;
7660 std::cout <<
"build graph vertices" << std::endl;
7667 for (it = imIn.begin(), iend = imIn.end(); it != iend;
7670 o0 = it.getOffset();
7671 imOut.setPixel(o0, 0);
7672 boost::add_vertex(g);
7695 vRoot = boost::add_vertex(g);
7697 std::cout <<
"number of vertices: " << numVert << std::endl;
7699 std::cout <<
"build graph edges" << std::endl;
7701 for (it = imIn.begin(), iend = imIn.end(); it != iend;
7704 o1 = it.getOffset();
7705 double val = imIn.pixelFromOffset(o1);
7706 int marker = imMarker.pixelFromOffset(o1);
7709 boost::tie(e4, in1) = boost::add_edge(vRoot, o1, g);
7710 weightmap[e4] = 1.0;
7713 neighb.setCenter(o1);
7715 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
7716 const offset_t o2 = nit.getOffset();
7720 double val2 = imIn.pixelFromOffset(o2);
7722 double cost =
std::pow(std::max(val, val2) + 1, exposant);
7726 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
7727 weightmap[e4] = cost;
7732 std::cout <<
"Compute Shortest Path Forest" << std::endl;
7734 std::vector<boost::graph_traits<Graph_d>::vertex_descriptor> p(
7735 boost::num_vertices(g));
7736 boost::property_map<Graph_d, boost::vertex_distance_t>::type distancemap =
7737 boost::get(boost::vertex_distance, g);
7738 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap2 =
7739 boost::get(boost::vertex_index, g);
7749 dijkstra_shortest_paths(g, vRoot, &p[0], distancemap, weightmap,
7750 indexmap2, std::less<double>(),
7751 boost::closed_plus<double>(),
7752 (std::numeric_limits<double>::max)(), 0,
7753 boost::default_dijkstra_visitor());
7755 std::cout <<
"Backward Nodes Labelling" << std::endl;
7757 int current_offset = 0;
7763 for (it = imIn.begin(), iend = imIn.end(); it != iend;
7766 o1 = it.getOffset();
7782 marker = imMarker.pixelFromOffset(o1);
7786 imOut.setPixel(o1, marker);
7790 pixout = imOut.pixelFromOffset(o1);
7796 current_offset = it.getOffset();
7801 temp_q.push(current_offset);
7804 marker = imMarker.pixelFromOffset(p[current_offset]);
7805 pixout = imOut.pixelFromOffset(p[current_offset]);
7808 while ((marker == 0 && pixout == 0)) {
7810 current_offset = p[current_offset];
7812 if (p[current_offset] == current_offset) {
7817 temp_q.push(current_offset);
7820 marker = imMarker.pixelFromOffset(p[current_offset]);
7821 pixout = imOut.pixelFromOffset(p[current_offset]);
7826 if (marker > 0 && pixout == 0) {
7828 imOut.setPixel(p[current_offset],
label);
7829 }
else if (marker == 0 && pixout > 0) {
7831 }
else if (marker > 0 && pixout > 0) {
7835 while (temp_q.size() > 0) {
7836 current_offset = temp_q.front();
7838 imOut.setPixel(current_offset,
label);
7846 template <
class ImageIn,
class ImageMarker,
class SE,
class ImageOut>
7847 RES_C t_geoCutsMax_Fiability_Forest(
const ImageIn &imIn,
7848 const ImageMarker &imMarker,
7849 const SE &nl, ImageOut &imOut)
7851 MORPHEE_ENTER_FUNCTION(
"t_geoCutsMax_Fiability_Forest");
7853 std::cout <<
"Enter function t_geoCutsMax_Fiability_Forest" << std::endl;
7855 if ((!imOut.isAllocated())) {
7856 MORPHEE_REGISTER_ERROR(
"Not allocated");
7857 return RES_NOT_ALLOCATED;
7860 if ((!imIn.isAllocated())) {
7861 MORPHEE_REGISTER_ERROR(
"Not allocated");
7862 return RES_NOT_ALLOCATED;
7865 if ((!imMarker.isAllocated())) {
7866 MORPHEE_REGISTER_ERROR(
"Not allocated");
7867 return RES_NOT_ALLOCATED;
7871 typename ImageIn::const_iterator it, iend;
7872 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
7873 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
7877 std::queue<int> temp_q;
7882 typedef boost::adjacency_list<
7883 boost::vecS, boost::vecS, boost::undirectedS,
7884 boost::property<boost::vertex_distance_t, double>,
7885 boost::property<boost::edge_capacity_t, double>>
7890 boost::property_map<Graph_d, boost::edge_capacity_t>::type weightmap =
7891 boost::get(boost::edge_capacity, g);
7893 boost::property_map<Graph_d, boost::edge_capacity_t>::type weightmap2 =
7894 boost::get(boost::edge_capacity, g);
7897 Graph_d::edge_descriptor e1, e2, e3, e4;
7898 Graph_d::vertex_descriptor vRoot;
7904 std::cout <<
"build graph vertices" << std::endl;
7906 for (it = imIn.begin(), iend = imIn.end(); it != iend;
7909 o0 = it.getOffset();
7910 imOut.setPixel(o0, 0);
7911 boost::add_vertex(g);
7915 vRoot = boost::add_vertex(g);
7917 std::cout <<
"number of vertices: " << numVert << std::endl;
7919 std::cout <<
"build graph edges" << std::endl;
7921 for (it = imIn.begin(), iend = imIn.end(); it != iend;
7924 o1 = it.getOffset();
7925 double val = imIn.pixelFromOffset(o1);
7926 int marker = imMarker.pixelFromOffset(o1);
7929 boost::tie(e4, in1) = boost::add_edge(vRoot, o1, g);
7930 weightmap[e4] = 1.0;
7933 neighb.setCenter(o1);
7935 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
7936 const offset_t o2 = nit.getOffset();
7940 double val2 = imIn.pixelFromOffset(o2);
7941 double cost = 1.0 / (1.0 + 0.1 * std::abs(val - val2));
7942 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
7943 weightmap2[e4] = cost;
7979 std::cout <<
"Compute Shortest Path Forest" << std::endl;
7981 std::vector<boost::graph_traits<Graph_d>::vertex_descriptor> p(
7982 boost::num_vertices(g));
7983 boost::property_map<Graph_d, boost::vertex_distance_t>::type distancemap =
7984 boost::get(boost::vertex_distance, g);
7985 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap2 =
7986 boost::get(boost::vertex_index, g);
7988 dijkstra_shortest_paths(g, vRoot, &p[0], distancemap, weightmap2,
7989 indexmap2, std::greater<double>(),
7990 boost::detail::multiplication<double>(), 0.0, 1.0,
7991 boost::default_dijkstra_visitor());
7993 std::cout <<
"Backward Nodes Labelling" << std::endl;
7995 int current_offset = 0;
8001 for (it = imIn.begin(), iend = imIn.end(); it != iend;
8004 o1 = it.getOffset();
8007 marker = imMarker.pixelFromOffset(o1);
8013 imOut.setPixel(o1, marker);
8017 pixout = imOut.pixelFromOffset(o1);
8023 current_offset = it.getOffset();
8028 temp_q.push(current_offset);
8031 marker = imMarker.pixelFromOffset(p[current_offset]);
8032 pixout = imOut.pixelFromOffset(p[current_offset]);
8035 while ((marker == 0 && pixout == 0)) {
8037 current_offset = p[current_offset];
8039 if (p[current_offset] == current_offset) {
8044 temp_q.push(current_offset);
8047 marker = imMarker.pixelFromOffset(p[current_offset]);
8048 pixout = imOut.pixelFromOffset(p[current_offset]);
8053 if (marker > 0 && pixout == 0) {
8055 imOut.setPixel(p[current_offset],
label);
8056 }
else if (marker == 0 && pixout > 0) {
8058 }
else if (marker > 0 && pixout > 0) {
8062 while (temp_q.size() > 0) {
8063 current_offset = temp_q.front();
8065 imOut.setPixel(current_offset,
label);
8073 template <
class ImageIn,
class ImageMarker,
class SE,
class ImageOut>
8074 RES_C t_geoCutsBiCriteria_Shortest_Forest(
const ImageIn &imIn,
8075 const ImageMarker &imMarker,
8076 const SE &nl, ImageOut &imOut)
8078 MORPHEE_ENTER_FUNCTION(
"t_geoCutsBiCriteria_Shortest_Forest");
8080 std::cout <<
"Enter function t_geoCutsBiCriteria_Shortest_Forest"
8083 if ((!imOut.isAllocated())) {
8084 MORPHEE_REGISTER_ERROR(
"Not allocated");
8085 return RES_NOT_ALLOCATED;
8088 if ((!imIn.isAllocated())) {
8089 MORPHEE_REGISTER_ERROR(
"Not allocated");
8090 return RES_NOT_ALLOCATED;
8093 if ((!imMarker.isAllocated())) {
8094 MORPHEE_REGISTER_ERROR(
"Not allocated");
8095 return RES_NOT_ALLOCATED;
8099 typename ImageIn::const_iterator it, iend;
8100 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
8101 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
8105 std::queue<int> temp_q;
8110 typedef std::pair<double, double> double2;
8112 typedef boost::adjacency_list<
8113 boost::vecS, boost::vecS, boost::undirectedS,
8114 boost::property<boost::vertex_distance_t, double2>,
8115 boost::property<boost::edge_capacity_t, double2>>
8120 boost::property_map<Graph_d, boost::edge_capacity_t>::type weightmap =
8121 boost::get(boost::edge_capacity, g);
8124 Graph_d::edge_descriptor e1, e2, e3, e4;
8125 Graph_d::vertex_descriptor vRoot;
8131 std::cout <<
"build graph vertices" << std::endl;
8133 for (it = imIn.begin(), iend = imIn.end(); it != iend;
8136 o0 = it.getOffset();
8137 imOut.setPixel(o0, 0);
8138 boost::add_vertex(g);
8142 vRoot = boost::add_vertex(g);
8144 std::cout <<
"number of vertices: " << numVert << std::endl;
8146 std::cout <<
"build graph edges" << std::endl;
8148 for (it = imIn.begin(), iend = imIn.end(); it != iend;
8151 o1 = it.getOffset();
8152 double val = imIn.pixelFromOffset(o1);
8153 int marker = imMarker.pixelFromOffset(o1);
8156 boost::tie(e4, in1) = boost::add_edge(vRoot, o1, g);
8157 weightmap[e4] = std::make_pair(1.0, 1.0);
8160 neighb.setCenter(o1);
8162 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
8163 const offset_t o2 = nit.getOffset();
8167 double val2 = imIn.pixelFromOffset(o2);
8169 double cost = std::max(val, val2) + 1;
8170 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
8171 weightmap[e4] = std::make_pair(cost, 1.0);
8176 std::cout <<
"Compute Shortest Path Forest" << std::endl;
8178 std::vector<boost::graph_traits<Graph_d>::vertex_descriptor> p(
8179 boost::num_vertices(g));
8180 boost::property_map<Graph_d, boost::vertex_distance_t>::type distancemap =
8181 boost::get(boost::vertex_distance, g);
8182 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap2 =
8183 boost::get(boost::vertex_index, g);
8185 dijkstra_shortest_paths(
8186 g, vRoot, &p[0], distancemap, weightmap, indexmap2,
8187 boost::detail::lexico_less2<double2>(),
8188 boost::detail::lexico_addition2<double2>(),
8189 std::make_pair(std::numeric_limits<double>::max(),
8190 std::numeric_limits<double>::max()),
8191 std::make_pair(0, 0), boost::default_dijkstra_visitor());
8193 std::cout <<
"Backward Nodes Labelling" << std::endl;
8195 int current_offset = 0;
8201 for (it = imIn.begin(), iend = imIn.end(); it != iend;
8204 o1 = it.getOffset();
8207 marker = imMarker.pixelFromOffset(o1);
8211 imOut.setPixel(o1, marker);
8215 pixout = imOut.pixelFromOffset(o1);
8221 current_offset = it.getOffset();
8226 temp_q.push(current_offset);
8229 marker = imMarker.pixelFromOffset(p[current_offset]);
8230 pixout = imOut.pixelFromOffset(p[current_offset]);
8233 while ((marker == 0 && pixout == 0)) {
8235 current_offset = p[current_offset];
8237 if (p[current_offset] == current_offset) {
8242 temp_q.push(current_offset);
8245 marker = imMarker.pixelFromOffset(p[current_offset]);
8246 pixout = imOut.pixelFromOffset(p[current_offset]);
8251 if (marker > 0 && pixout == 0) {
8253 imOut.setPixel(p[current_offset],
label);
8254 }
else if (marker == 0 && pixout > 0) {
8256 }
else if (marker > 0 && pixout > 0) {
8260 while (temp_q.size() > 0) {
8261 current_offset = temp_q.front();
8263 imOut.setPixel(current_offset,
label);
8271 template <
class ImageIn,
class ImageMarker,
class SE,
class ImageOut>
8272 RES_C t_geoCutsLexicographical_Shortest_Forest(
const ImageIn &imIn,
8273 const ImageMarker &imMarker,
8277 MORPHEE_ENTER_FUNCTION(
"t_geoCutsLexicographical_Shortest_Forest");
8279 std::cout <<
"Enter function t_geoCutsLexicographical_Shortest_Forest"
8282 if ((!imOut.isAllocated())) {
8283 MORPHEE_REGISTER_ERROR(
"Not allocated");
8284 return RES_NOT_ALLOCATED;
8287 if ((!imIn.isAllocated())) {
8288 MORPHEE_REGISTER_ERROR(
"Not allocated");
8289 return RES_NOT_ALLOCATED;
8292 if ((!imMarker.isAllocated())) {
8293 MORPHEE_REGISTER_ERROR(
"Not allocated");
8294 return RES_NOT_ALLOCATED;
8298 typename ImageIn::const_iterator it, iend;
8299 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
8300 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
8304 std::queue<int> temp_q;
8309 typedef std::vector<double> vdouble;
8311 typedef boost::adjacency_list<
8312 boost::vecS, boost::vecS, boost::undirectedS,
8313 boost::property<boost::vertex_distance_t, vdouble>,
8314 boost::property<boost::edge_capacity_t, vdouble>>
8319 boost::property_map<Graph_d, boost::edge_capacity_t>::type weightmap =
8320 boost::get(boost::edge_capacity, g);
8323 Graph_d::edge_descriptor e1, e2, e3, e4;
8324 Graph_d::vertex_descriptor vRoot;
8327 std::vector<double> roots_cost;
8328 roots_cost.push_back(1.0);
8330 std::vector<double> edges_cost;
8331 edges_cost.push_back(1.0);
8335 std::cout <<
"build graph vertices" << std::endl;
8337 for (it = imIn.begin(), iend = imIn.end(); it != iend;
8340 o0 = it.getOffset();
8341 imOut.setPixel(o0, 0);
8342 boost::add_vertex(g);
8346 vRoot = boost::add_vertex(g);
8348 std::cout <<
"number of vertices: " << numVert << std::endl;
8350 std::cout <<
"build graph edges" << std::endl;
8352 for (it = imIn.begin(), iend = imIn.end(); it != iend;
8355 o1 = it.getOffset();
8356 double val = imIn.pixelFromOffset(o1);
8357 int marker = imMarker.pixelFromOffset(o1);
8360 boost::tie(e4, in1) = boost::add_edge(vRoot, o1, g);
8361 weightmap[e4] = roots_cost;
8364 neighb.setCenter(o1);
8366 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
8367 const offset_t o2 = nit.getOffset();
8371 double val2 = imIn.pixelFromOffset(o2);
8373 double cost = std::max(val, val2) + 1;
8374 edges_cost[0] = cost;
8375 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
8376 weightmap[e4] = edges_cost;
8381 std::cout <<
"Compute Shortest Path Forest" << std::endl;
8383 std::vector<boost::graph_traits<Graph_d>::vertex_descriptor> p(
8384 boost::num_vertices(g));
8385 boost::property_map<Graph_d, boost::vertex_distance_t>::type distancemap =
8386 boost::get(boost::vertex_distance, g);
8387 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap2 =
8388 boost::get(boost::vertex_index, g);
8390 std::vector<double> infinite;
8391 infinite.push_back(std::numeric_limits<double>::max());
8393 std::vector<double> zero;
8396 dijkstra_shortest_paths(
8397 g, vRoot, &p[0], distancemap, weightmap, indexmap2,
8398 boost::detail::lexico_compare<vdouble>(),
8399 boost::detail::lexico_addition<vdouble>(), infinite, zero,
8400 boost::default_dijkstra_visitor());
8402 std::cout <<
"Backward Nodes Labelling" << std::endl;
8404 int current_offset = 0;
8410 for (it = imIn.begin(), iend = imIn.end(); it != iend;
8413 o1 = it.getOffset();
8418 marker = imMarker.pixelFromOffset(o1);
8422 imOut.setPixel(o1, marker);
8426 pixout = imOut.pixelFromOffset(o1);
8432 current_offset = it.getOffset();
8437 temp_q.push(current_offset);
8440 marker = imMarker.pixelFromOffset(p[current_offset]);
8441 pixout = imOut.pixelFromOffset(p[current_offset]);
8444 while ((marker == 0 && pixout == 0)) {
8446 current_offset = p[current_offset];
8448 if (p[current_offset] == current_offset) {
8453 temp_q.push(current_offset);
8456 marker = imMarker.pixelFromOffset(p[current_offset]);
8457 pixout = imOut.pixelFromOffset(p[current_offset]);
8462 if (marker > 0 && pixout == 0) {
8464 imOut.setPixel(p[current_offset],
label);
8465 }
else if (marker == 0 && pixout > 0) {
8467 }
else if (marker > 0 && pixout > 0) {
8471 while (temp_q.size() > 0) {
8472 current_offset = temp_q.front();
8474 imOut.setPixel(current_offset,
label);
8482 template <
class ImageIn,
class ImageMarker,
class SE,
class ImageOut>
8483 RES_C t_geoCutsVectorial_Lexicographical_Shortest_Forest(
8484 const ImageIn &imIn,
const ImageMarker &imMarker,
const SE &nl,
8487 MORPHEE_ENTER_FUNCTION(
8488 "t_geoCutsVectorial_Lexicographical_Shortest_Forest");
8490 std::cout <<
"Enter function "
8491 "t_geoCutsVectorial_Lexicographical_Shortest_Forest"
8494 if ((!imOut.isAllocated())) {
8495 MORPHEE_REGISTER_ERROR(
"Not allocated");
8496 return RES_NOT_ALLOCATED;
8499 if ((!imIn.isAllocated())) {
8500 MORPHEE_REGISTER_ERROR(
"Not allocated");
8501 return RES_NOT_ALLOCATED;
8504 if ((!imMarker.isAllocated())) {
8505 MORPHEE_REGISTER_ERROR(
"Not allocated");
8506 return RES_NOT_ALLOCATED;
8510 typename ImageIn::const_iterator it, iend;
8511 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
8512 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
8516 std::queue<int> temp_q;
8522 typedef boost::adjacency_list<
8523 boost::vecS, boost::vecS, boost::undirectedS,
8524 boost::property<boost::vertex_distance_t,
8525 std::vector<std::vector<double>>>,
8526 boost::property<boost::edge_capacity_t,
8527 std::vector<std::vector<double>>>>
8532 boost::property_map<Graph_d, boost::edge_capacity_t>::type weightmap =
8533 boost::get(boost::edge_capacity, g);
8536 Graph_d::edge_descriptor e1, e2, e3, e4;
8537 Graph_d::vertex_descriptor vRoot;
8543 pixel_3<F_DOUBLE> pxImageIn;
8544 pixel_3<F_DOUBLE> pxImageNeigh;
8546 std::vector<std::vector<double>> root;
8548 std::vector<double> roots_cost;
8549 roots_cost.push_back(0.1);
8550 roots_cost.push_back(0.1);
8551 roots_cost.push_back(0.1);
8553 root.push_back(roots_cost);
8555 std::vector<std::vector<double>>
test;
8556 test.push_back(roots_cost);
8557 test.push_back(roots_cost);
8558 int sizea =
test.size();
8560 std::cout <<
"taille des test " << sizea << std::endl;
8562 for (
int i = 0; i < 2; i++) {
8563 std::vector<double> temp =
test[i];
8564 std::cout << temp[0] <<
" " << temp[1] <<
" " << temp[2] << std::endl;
8567 std::cout <<
"build graph vertices" << std::endl;
8569 for (it = imIn.begin(), iend = imIn.end(); it != iend;
8572 o0 = it.getOffset();
8573 imOut.setPixel(o0, 0);
8574 boost::add_vertex(g);
8578 vRoot = boost::add_vertex(g);
8580 std::cout <<
"number of vertices: " << numVert << std::endl;
8582 std::cout <<
"build graph edges" << std::endl;
8584 for (it = imIn.begin(), iend = imIn.end(); it != iend;
8587 o1 = it.getOffset();
8588 pxImageIn = imIn.pixelFromOffset(o1);
8590 double valeur1 = pxImageIn.channel1;
8591 double valeur2 = pxImageIn.channel2;
8592 double valeur3 = pxImageIn.channel3;
8594 int marker = imMarker.pixelFromOffset(o1);
8597 boost::tie(e4, in1) = boost::add_edge(vRoot, o1, g);
8598 weightmap[e4] = root;
8601 neighb.setCenter(o1);
8603 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
8604 const offset_t o2 = nit.getOffset();
8608 pxImageNeigh = imIn.pixelFromOffset(o2);
8609 std::vector<std::vector<double>> cost;
8610 std::vector<double> ordered_cost_vector;
8612 double valeurn1 = std::abs(pxImageNeigh.channel1 - valeur1) + 1;
8613 double valeurn2 = std::abs(pxImageNeigh.channel2 - valeur2) + 1;
8614 double valeurn3 = std::abs(pxImageNeigh.channel3 - valeur3) + 1;
8618 ordered_cost_vector.push_back(valeurn1);
8619 ordered_cost_vector.push_back(valeurn2);
8620 ordered_cost_vector.push_back(valeurn3);
8641 cost.push_back(ordered_cost_vector);
8642 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
8643 weightmap[e4] = cost;
8648 std::cout <<
"Compute Minimum Spanning Forest" << std::endl;
8650 std::vector<boost::graph_traits<Graph_d>::vertex_descriptor> p(
8651 boost::num_vertices(g));
8652 boost::property_map<Graph_d, boost::vertex_distance_t>::type distancemap =
8653 boost::get(boost::vertex_distance, g);
8654 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap2 =
8655 boost::get(boost::vertex_index, g);
8657 std::vector<std::vector<double>> inff;
8658 std::vector<double> infinite;
8659 infinite.push_back(std::numeric_limits<double>::max());
8660 infinite.push_back(std::numeric_limits<double>::max());
8661 infinite.push_back(std::numeric_limits<double>::max());
8662 inff.push_back(infinite);
8664 std::vector<std::vector<double>> Zeroo;
8665 std::vector<double> zero;
8666 zero.push_back(0.0);
8667 zero.push_back(0.0);
8668 zero.push_back(0.0);
8669 Zeroo.push_back(zero);
8671 dijkstra_shortest_paths(g, vRoot, &p[0], distancemap, weightmap,
8673 boost::detail::lexico_compare_vect_of_vect<
8674 std::vector<std::vector<double>>>(),
8675 boost::detail::lexico_addition_vect_of_vect<
8676 std::vector<std::vector<double>>>(),
8677 inff, Zeroo, boost::default_dijkstra_visitor());
8678 std::cout <<
"Backward Nodes Labelling" << std::endl;
8680 int current_offset = 0;
8686 for (it = imIn.begin(), iend = imIn.end(); it != iend;
8689 o1 = it.getOffset();
8692 marker = imMarker.pixelFromOffset(o1);
8696 imOut.setPixel(o1, marker);
8700 pixout = imOut.pixelFromOffset(o1);
8706 current_offset = it.getOffset();
8711 temp_q.push(current_offset);
8714 marker = imMarker.pixelFromOffset(p[current_offset]);
8715 pixout = imOut.pixelFromOffset(p[current_offset]);
8718 while ((marker == 0 && pixout == 0)) {
8720 current_offset = p[current_offset];
8722 if (p[current_offset] == current_offset) {
8727 temp_q.push(current_offset);
8730 marker = imMarker.pixelFromOffset(p[current_offset]);
8731 pixout = imOut.pixelFromOffset(p[current_offset]);
8736 if (marker > 0 && pixout == 0) {
8738 imOut.setPixel(p[current_offset],
label);
8739 }
else if (marker == 0 && pixout > 0) {
8741 }
else if (marker > 0 && pixout > 0) {
8745 while (temp_q.size() > 0) {
8746 current_offset = temp_q.front();
8748 imOut.setPixel(current_offset,
label);
8756 template <
class ImageIn,
class ImageMarker,
class SE,
class ImageOut>
8757 RES_C t_geoCutsVectorial_Shortest_Forest(
const ImageIn &imIn,
8758 const ImageMarker &imMarker,
8759 const SE &nl, ImageOut &imOut)
8761 MORPHEE_ENTER_FUNCTION(
"t_geoCutsVectorial_Shortest_Forest");
8763 std::cout <<
"Enter function t_geoCutsVectorial_Shortest_Forest"
8766 if ((!imOut.isAllocated())) {
8767 MORPHEE_REGISTER_ERROR(
"Not allocated");
8768 return RES_NOT_ALLOCATED;
8771 if ((!imIn.isAllocated())) {
8772 MORPHEE_REGISTER_ERROR(
"Not allocated");
8773 return RES_NOT_ALLOCATED;
8776 if ((!imMarker.isAllocated())) {
8777 MORPHEE_REGISTER_ERROR(
"Not allocated");
8778 return RES_NOT_ALLOCATED;
8782 typename ImageIn::const_iterator it, iend;
8783 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
8784 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
8788 std::queue<int> temp_q;
8793 typedef std::vector<double> vdouble;
8795 typedef boost::adjacency_list<
8796 boost::vecS, boost::vecS, boost::undirectedS,
8797 boost::property<boost::vertex_distance_t, vdouble>,
8798 boost::property<boost::edge_capacity_t, vdouble>>
8803 boost::property_map<Graph_d, boost::edge_capacity_t>::type weightmap =
8804 boost::get(boost::edge_capacity, g);
8807 Graph_d::edge_descriptor e1, e2, e3, e4;
8808 Graph_d::vertex_descriptor vRoot;
8814 pixel_3<F_DOUBLE> pxImageIn;
8815 pixel_3<F_DOUBLE> pxImageNeigh;
8817 std::vector<double> roots_cost;
8818 roots_cost.push_back(0.1);
8819 roots_cost.push_back(0.1);
8820 roots_cost.push_back(0.1);
8822 std::cout <<
"build graph vertices" << std::endl;
8824 for (it = imIn.begin(), iend = imIn.end(); it != iend;
8827 o0 = it.getOffset();
8828 imOut.setPixel(o0, 0);
8829 boost::add_vertex(g);
8833 vRoot = boost::add_vertex(g);
8835 std::cout <<
"number of vertices: " << numVert << std::endl;
8837 std::cout <<
"build graph edges" << std::endl;
8839 for (it = imIn.begin(), iend = imIn.end(); it != iend;
8842 o1 = it.getOffset();
8843 pxImageIn = imIn.pixelFromOffset(o1);
8845 double valeur1 = pxImageIn.channel1;
8846 double valeur2 = pxImageIn.channel2;
8847 double valeur3 = pxImageIn.channel3;
8849 int marker = imMarker.pixelFromOffset(o1);
8852 boost::tie(e4, in1) = boost::add_edge(vRoot, o1, g);
8853 weightmap[e4] = roots_cost;
8856 neighb.setCenter(o1);
8858 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
8859 const offset_t o2 = nit.getOffset();
8863 pxImageNeigh = imIn.pixelFromOffset(o2);
8864 std::vector<double> ordered_cost_vector;
8865 double valeurn1 = std::abs(pxImageNeigh.channel1 - valeur1) + 1;
8866 double valeurn2 = std::abs(pxImageNeigh.channel2 - valeur2) + 1;
8867 double valeurn3 = std::abs(pxImageNeigh.channel3 - valeur3) + 1;
8871 ordered_cost_vector.push_back(valeurn1);
8872 ordered_cost_vector.push_back(valeurn2);
8873 ordered_cost_vector.push_back(valeurn3);
8894 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
8895 weightmap[e4] = ordered_cost_vector;
8900 std::cout <<
"Compute Minimum Spanning Forest" << std::endl;
8902 std::vector<boost::graph_traits<Graph_d>::vertex_descriptor> p(
8903 boost::num_vertices(g));
8904 boost::property_map<Graph_d, boost::vertex_distance_t>::type distancemap =
8905 boost::get(boost::vertex_distance, g);
8906 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap2 =
8907 boost::get(boost::vertex_index, g);
8909 std::vector<double> infinite;
8910 infinite.push_back(std::numeric_limits<double>::max());
8911 infinite.push_back(std::numeric_limits<double>::max());
8912 infinite.push_back(std::numeric_limits<double>::max());
8914 std::vector<double> zero;
8915 zero.push_back(0.0);
8916 zero.push_back(0.0);
8917 zero.push_back(0.0);
8919 dijkstra_shortest_paths(
8920 g, vRoot, &p[0], distancemap, weightmap, indexmap2,
8921 boost::detail::lexico_compare3<vdouble>(),
8922 boost::detail::lexico_addition3<vdouble>(), infinite, zero,
8923 boost::default_dijkstra_visitor());
8924 std::cout <<
"Backward Nodes Labelling" << std::endl;
8926 int current_offset = 0;
8932 for (it = imIn.begin(), iend = imIn.end(); it != iend;
8935 o1 = it.getOffset();
8938 marker = imMarker.pixelFromOffset(o1);
8942 imOut.setPixel(o1, marker);
8946 pixout = imOut.pixelFromOffset(o1);
8952 current_offset = it.getOffset();
8957 temp_q.push(current_offset);
8960 marker = imMarker.pixelFromOffset(p[current_offset]);
8961 pixout = imOut.pixelFromOffset(p[current_offset]);
8964 while ((marker == 0 && pixout == 0)) {
8966 current_offset = p[current_offset];
8968 if (p[current_offset] == current_offset) {
8973 temp_q.push(current_offset);
8976 marker = imMarker.pixelFromOffset(p[current_offset]);
8977 pixout = imOut.pixelFromOffset(p[current_offset]);
8982 if (marker > 0 && pixout == 0) {
8984 imOut.setPixel(p[current_offset],
label);
8985 }
else if (marker == 0 && pixout > 0) {
8987 }
else if (marker > 0 && pixout > 0) {
8991 while (temp_q.size() > 0) {
8992 current_offset = temp_q.front();
8994 imOut.setPixel(current_offset,
label);
9002 template <
class ImageIn,
class ImageMarker,
class SE,
class ImageOut>
9003 RES_C t_geoCutsWatershed_SpanningForest(
const ImageIn &imIn,
9004 const ImageMarker &imMarker,
9005 const SE &nl, ImageOut &imOut)
9007 MORPHEE_ENTER_FUNCTION(
"t_geoCutsWatershed_SpanningForest");
9009 std::cout <<
"Enter function t_geoCutsWatershed_SpanningForest"
9012 if ((!imOut.isAllocated())) {
9013 MORPHEE_REGISTER_ERROR(
"Not allocated");
9014 return RES_NOT_ALLOCATED;
9017 if ((!imIn.isAllocated())) {
9018 MORPHEE_REGISTER_ERROR(
"Not allocated");
9019 return RES_NOT_ALLOCATED;
9022 if ((!imMarker.isAllocated())) {
9023 MORPHEE_REGISTER_ERROR(
"Not allocated");
9024 return RES_NOT_ALLOCATED;
9028 typename ImageIn::const_iterator it, iend;
9029 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
9030 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
9034 std::queue<int> temp_q;
9039 typedef boost::adjacency_list<
9040 boost::vecS, boost::vecS, boost::undirectedS,
9041 boost::property<boost::vertex_distance_t, double>,
9042 boost::property<boost::edge_capacity_t, double>>
9047 boost::property_map<Graph_d, boost::edge_capacity_t>::type weightmap =
9048 boost::get(boost::edge_capacity, g);
9051 Graph_d::edge_descriptor e1, e2, e3, e4;
9052 Graph_d::vertex_descriptor vRoot;
9058 std::cout <<
"build graph vertices" << std::endl;
9060 for (it = imIn.begin(), iend = imIn.end(); it != iend;
9063 o0 = it.getOffset();
9064 imOut.setPixel(o0, 0);
9065 boost::add_vertex(g);
9069 vRoot = boost::add_vertex(g);
9071 std::cout <<
"number of vertices: " << numVert << std::endl;
9073 std::cout <<
"build graph edges" << std::endl;
9075 for (it = imIn.begin(), iend = imIn.end(); it != iend;
9078 o1 = it.getOffset();
9079 double val = imIn.pixelFromOffset(o1);
9080 int marker = imMarker.pixelFromOffset(o1);
9083 boost::tie(e4, in1) = boost::add_edge(vRoot, o1, g);
9084 weightmap[e4] = 0.1;
9087 neighb.setCenter(o1);
9089 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
9090 const offset_t o2 = nit.getOffset();
9094 double val2 = imIn.pixelFromOffset(o2);
9095 double cost = std::abs(val - val2) + 1;
9097 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
9098 weightmap[e4] = cost;
9103 std::cout <<
"Compute Minimum Spanning Forest" << std::endl;
9105 std::vector<boost::graph_traits<Graph_d>::vertex_descriptor> p(
9106 boost::num_vertices(g));
9107 boost::property_map<Graph_d, boost::vertex_distance_t>::type distancemap =
9108 boost::get(boost::vertex_distance, g);
9109 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap2 =
9110 boost::get(boost::vertex_index, g);
9112 dijkstra_shortest_paths(g, vRoot, &p[0], distancemap, weightmap,
9113 indexmap2, std::less<double>(),
9114 boost::detail::maximum<double>(),
9115 (std::numeric_limits<double>::max)(), 0,
9116 boost::default_dijkstra_visitor());
9121 std::cout <<
"Backward Nodes Labelling" << std::endl;
9123 int current_offset = 0;
9129 for (it = imIn.begin(), iend = imIn.end(); it != iend;
9132 o1 = it.getOffset();
9135 marker = imMarker.pixelFromOffset(o1);
9139 imOut.setPixel(o1, marker);
9143 pixout = imOut.pixelFromOffset(o1);
9149 current_offset = it.getOffset();
9154 temp_q.push(current_offset);
9157 marker = imMarker.pixelFromOffset(p[current_offset]);
9158 pixout = imOut.pixelFromOffset(p[current_offset]);
9161 while ((marker == 0 && pixout == 0)) {
9163 current_offset = p[current_offset];
9165 if (p[current_offset] == current_offset) {
9170 temp_q.push(current_offset);
9173 marker = imMarker.pixelFromOffset(p[current_offset]);
9174 pixout = imOut.pixelFromOffset(p[current_offset]);
9179 if (marker > 0 && pixout == 0) {
9181 imOut.setPixel(p[current_offset],
label);
9182 }
else if (marker == 0 && pixout > 0) {
9184 }
else if (marker > 0 && pixout > 0) {
9188 while (temp_q.size() > 0) {
9189 current_offset = temp_q.front();
9191 imOut.setPixel(current_offset,
label);
9199 template <
class ImageIn,
class ImageMarker,
class SE,
class ImageOut>
9200 RES_C t_geoCutsWatershed_SpanningForest_v2(
const ImageIn &imIn,
9201 const ImageMarker &imMarker,
9202 const SE &nl, ImageOut &imOut)
9204 MORPHEE_ENTER_FUNCTION(
"t_geoCutsWatershed_SpanningForest_v2");
9206 std::cout <<
"Enter function t_geoCutsWatershed_SpanningForest_v2"
9209 if ((!imOut.isAllocated())) {
9210 MORPHEE_REGISTER_ERROR(
"Not allocated");
9211 return RES_NOT_ALLOCATED;
9214 if ((!imIn.isAllocated())) {
9215 MORPHEE_REGISTER_ERROR(
"Not allocated");
9216 return RES_NOT_ALLOCATED;
9219 if ((!imMarker.isAllocated())) {
9220 MORPHEE_REGISTER_ERROR(
"Not allocated");
9221 return RES_NOT_ALLOCATED;
9226 SE nl2 = morphee::selement::neighborsCross2D;
9227 morphee::selement::Neighborhood<SE, ImageIn> neighb2(imIn, nl2);
9228 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit2,
9231 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
9232 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
9234 typename ImageIn::const_iterator it, iend;
9239 std::queue<int> temp_q;
9244 typedef boost::adjacency_list<
9245 boost::vecS, boost::vecS, boost::undirectedS,
9246 boost::property<boost::vertex_distance_t, double>,
9247 boost::property<boost::edge_capacity_t, double>>
9252 boost::property_map<Graph_d, boost::edge_capacity_t>::type weightmap =
9253 boost::get(boost::edge_capacity, g);
9256 Graph_d::edge_descriptor e1, e2, e3, e4;
9257 Graph_d::vertex_descriptor vRoot;
9262 typedef typename s_from_type_to_type<ImageIn, F_SIMPLE>::image_type
9264 dist_image_type work = imIn.template t_getSame<float>();
9265 RES_C res = t_ImSetConstant(work, 0.0);
9269 std::cout <<
"build graph vertices" << std::endl;
9271 for (it = imIn.begin(), iend = imIn.end(); it != iend;
9274 o0 = it.getOffset();
9275 work.setPixel(o0, 0.0);
9276 imOut.setPixel(o0, 0);
9277 boost::add_vertex(g);
9281 vRoot = boost::add_vertex(g);
9283 std::cout <<
"number of vertices: " << numVert << std::endl;
9289 for (it = imIn.begin(), iend = imIn.end(); it != iend;
9292 o0 = it.getOffset();
9293 double val = imIn.pixelFromOffset(o0);
9295 neighb2.setCenter(o0);
9296 double tempval = val;
9297 double tempval2 = val;
9300 for (nit2 = neighb2.begin(), nend2 = neighb2.end(); nit2 != nend2;
9302 o1 = nit2.getOffset();
9303 val2 = imIn.pixelFromOffset(o1);
9305 if (val2 < tempval) {
9317 work.setPixel(o0, (
float) val - tempval);
9323 std::cout <<
"build graph edges" << std::endl;
9325 for (it = imIn.begin(), iend = imIn.end(); it != iend;
9328 o1 = it.getOffset();
9330 double val = imIn.pixelFromOffset(o1);
9331 double ls1 = work.pixelFromOffset(o1);
9333 int marker = imMarker.pixelFromOffset(o1);
9336 boost::tie(e4, in1) = boost::add_edge(vRoot, o1, g);
9340 neighb.setCenter(o1);
9342 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
9343 const offset_t o2 = nit.getOffset();
9348 double val2 = imIn.pixelFromOffset(o2);
9349 double ls2 = work.pixelFromOffset(o2);
9354 }
else if (val2 > val) {
9356 }
else if (val2 == val) {
9357 cost = val + std::max(ls2, ls1);
9363 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
9364 weightmap[e4] = cost + 1;
9369 std::cout <<
"Compute Minimum Spanning Forest" << std::endl;
9371 std::vector<boost::graph_traits<Graph_d>::vertex_descriptor> p(
9372 boost::num_vertices(g));
9373 boost::property_map<Graph_d, boost::vertex_distance_t>::type distancemap =
9374 boost::get(boost::vertex_distance, g);
9375 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap2 =
9376 boost::get(boost::vertex_index, g);
9378 dijkstra_shortest_paths(g, vRoot, &p[0], distancemap, weightmap,
9379 indexmap2, std::less<double>(),
9380 boost::detail::maximum<double>(),
9381 (std::numeric_limits<double>::max)(), 0,
9382 boost::default_dijkstra_visitor());
9392 std::cout <<
"Backward Nodes Labelling" << std::endl;
9394 int current_offset = 0;
9400 for (it = imIn.begin(), iend = imIn.end(); it != iend;
9403 o1 = it.getOffset();
9409 marker = imMarker.pixelFromOffset(o1);
9413 imOut.setPixel(o1, marker);
9417 pixout = imOut.pixelFromOffset(o1);
9423 current_offset = it.getOffset();
9428 temp_q.push(current_offset);
9431 marker = imMarker.pixelFromOffset(p[current_offset]);
9432 pixout = imOut.pixelFromOffset(p[current_offset]);
9435 while ((marker == 0 && pixout == 0)) {
9437 current_offset = p[current_offset];
9439 if (p[current_offset] == current_offset) {
9444 temp_q.push(current_offset);
9447 marker = imMarker.pixelFromOffset(p[current_offset]);
9448 pixout = imOut.pixelFromOffset(p[current_offset]);
9453 if (marker > 0 && pixout == 0) {
9455 imOut.setPixel(p[current_offset],
label);
9456 }
else if (marker == 0 && pixout > 0) {
9458 }
else if (marker > 0 && pixout > 0) {
9462 while (temp_q.size() > 0) {
9463 current_offset = temp_q.front();
9465 imOut.setPixel(current_offset,
label);
9473 template <
class ImageIn,
class ImageMarker,
class SE,
class ImageOut>
9474 RES_C t_geoCutsReg_SpanningForest(
const ImageIn &imIn,
9475 const ImageMarker &imMarker,
9476 const SE &nl, ImageOut &imOut)
9478 MORPHEE_ENTER_FUNCTION(
"t_geoCutsReg_SpanningForest");
9480 std::cout <<
"Enter function geoCutsSpanningForest" << std::endl;
9482 if ((!imOut.isAllocated())) {
9483 MORPHEE_REGISTER_ERROR(
"Not allocated");
9484 return RES_NOT_ALLOCATED;
9487 if ((!imIn.isAllocated())) {
9488 MORPHEE_REGISTER_ERROR(
"Not allocated");
9489 return RES_NOT_ALLOCATED;
9492 if ((!imMarker.isAllocated())) {
9493 MORPHEE_REGISTER_ERROR(
"Not allocated");
9494 return RES_NOT_ALLOCATED;
9498 typename ImageIn::const_iterator it, iend;
9499 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
9500 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
9504 std::queue<int> temp_q;
9509 typedef boost::adjacency_list<
9510 boost::vecS, boost::vecS, boost::undirectedS,
9511 boost::property<boost::vertex_distance_t, double>,
9512 boost::property<boost::edge_capacity_t, double>>
9517 boost::property_map<Graph_d, boost::edge_capacity_t>::type weightmap =
9518 boost::get(boost::edge_capacity, g);
9521 Graph_d::edge_descriptor e1, e2, e3, e4;
9522 Graph_d::vertex_descriptor vRoot;
9528 typedef boost::adjacency_list_traits<boost::vecS, boost::vecS,
9531 typedef boost::adjacency_list<
9532 boost::listS, boost::vecS, boost::directedS,
9533 boost::property<boost::vertex_name_t, std::string>,
9535 boost::edge_capacity_t, double,
9536 boost::property<boost::edge_residual_capacity_t, double,
9537 boost::property<boost::edge_reverse_t,
9538 Traits::edge_descriptor>>>>
9543 boost::property_map<Graph_flow, boost::edge_capacity_t>::type capacity =
9544 boost::get(boost::edge_capacity, g_flow);
9546 boost::property_map<Graph_flow, boost::edge_reverse_t>::type rev =
9547 get(boost::edge_reverse, g_flow);
9549 boost::property_map<Graph_flow, boost::edge_residual_capacity_t>::type
9550 residual_capacity = get(boost::edge_residual_capacity, g_flow);
9552 Graph_flow::edge_descriptor e11, e22;
9553 Graph_flow::vertex_descriptor vSource, vSink;
9558 std::cout <<
"build graph vertices" << std::endl;
9560 for (it = imIn.begin(), iend = imIn.end(); it != iend;
9563 o0 = it.getOffset();
9564 imOut.setPixel(o0, 0);
9565 boost::add_vertex(g);
9568 boost::add_vertex(g_flow);
9574 vRoot = boost::add_vertex(g);
9577 vSource = boost::add_vertex(g_flow);
9578 vSink = boost::add_vertex(g_flow);
9581 std::cout <<
"number of vertices: " << numVert << std::endl;
9583 std::cout <<
"build graph edges" << std::endl;
9585 for (it = imIn.begin(), iend = imIn.end(); it != iend;
9588 o1 = it.getOffset();
9589 double val = imIn.pixelFromOffset(o1);
9590 int marker = imMarker.pixelFromOffset(o1);
9593 boost::tie(e4, in1) = boost::add_edge(vRoot, o1, g);
9594 weightmap[e4] = 0.5;
9597 boost::tie(e11, in1) = boost::add_edge(vSource, o1, g_flow);
9598 boost::tie(e22, in2) = boost::add_edge(o1, vSource, g_flow);
9599 capacity[e11] = 1000000000;
9600 capacity[e22] = 1000000000;
9605 boost::tie(e11, in1) = boost::add_edge(vSink, o1, g_flow);
9606 boost::tie(e22, in2) = boost::add_edge(o1, vSink, g_flow);
9607 capacity[e11] = 1000000000;
9608 capacity[e22] = 1000000000;
9614 neighb.setCenter(o1);
9616 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
9617 const offset_t o2 = nit.getOffset();
9621 double val2 = imIn.pixelFromOffset(o2);
9622 double cost = std::abs((val - val2)) + 1;
9623 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
9624 weightmap[e4] = cost;
9626 boost::tie(e11, in1) = boost::add_edge(o1, o2, g_flow);
9627 boost::tie(e22, in2) = boost::add_edge(o2, o1, g_flow);
9628 capacity[e11] = 256 / (cost);
9629 capacity[e22] = 256 / (cost);
9636 std::cout <<
"Compute Minimum Spanning Forest" << std::endl;
9638 std::vector<boost::graph_traits<Graph_d>::vertex_descriptor> p(
9639 boost::num_vertices(g));
9640 boost::property_map<Graph_d, boost::vertex_distance_t>::type distancemap =
9641 boost::get(boost::vertex_distance, g);
9642 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap2 =
9643 boost::get(boost::vertex_index, g);
9645 prim_minimum_spanning_tree(g, vRoot, &p[0], distancemap, weightmap,
9646 indexmap2, boost::default_dijkstra_visitor());
9648 std::cout <<
"Backward Nodes Labelling" << std::endl;
9650 int current_offset = 0;
9651 int first_offset = 0;
9657 for (
int i = 0; i < numVert; i++) {
9659 marker = imMarker.pixelFromOffset(i);
9663 imOut.setPixel(i, marker);
9667 pixout = imOut.pixelFromOffset(i);
9674 temp_q.push(current_offset);
9677 marker = imMarker.pixelFromOffset(p[current_offset]);
9678 pixout = imOut.pixelFromOffset(p[current_offset]);
9681 while ((marker == 0 && pixout == 0)) {
9683 current_offset = p[current_offset];
9686 temp_q.push(current_offset);
9689 marker = imMarker.pixelFromOffset(p[current_offset]);
9690 pixout = imOut.pixelFromOffset(p[current_offset]);
9693 temp_q.push(p[current_offset]);
9696 if (marker > 0 && pixout == 0) {
9698 imOut.setPixel(p[current_offset],
label);
9699 }
else if (marker == 0 && pixout > 0) {
9701 }
else if (marker > 0 && pixout > 0) {
9706 while (temp_q.size() > 0) {
9707 first_offset = temp_q.front();
9709 imOut.setPixel(first_offset,
label);
9711 if (p[first_offset] != vRoot) {
9712 current_offset = p[first_offset];
9713 imOut.setPixel(current_offset,
label);
9715 boost::tie(e11, in1) =
9716 boost::edge(first_offset, current_offset, g_flow);
9717 boost::tie(e22, in2) =
9718 boost::edge(current_offset, first_offset, g_flow);
9721 capacity[e22] = capacity[e22];
9722 capacity[e11] = capacity[e11];
9732 for (it = imIn.begin(), iend = imIn.end(); it != iend;
9735 o1 = it.getOffset();
9736 pix1 = imOut.pixelFromOffset(o1);
9737 neighb.setCenter(o1);
9739 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
9740 const offset_t o2 = nit.getOffset();
9741 pix2 = imOut.pixelFromOffset(o2);
9747 boost::tie(e11, in1) = boost::edge(o1, o2, g_flow);
9748 boost::tie(e22, in1) = boost::edge(o2, o1, g_flow);
9750 capacity[e11] = 3.2;
9751 capacity[e22] = 3.2;
9757 boost::property_map<Graph_flow, boost::vertex_index_t>::const_type
9758 indexmap = boost::get(boost::vertex_index, g_flow);
9759 std::vector<boost::default_color_type> color(boost::num_vertices(g_flow));
9761 std::cout <<
"Compute Max flow" << std::endl;
9762 #if BOOST_VERSION >= 104700
9764 boykov_kolmogorov_max_flow(g_flow, capacity, residual_capacity, rev,
9765 &color[0], indexmap, vSource, vSink);
9768 kolmogorov_max_flow(g_flow, capacity, residual_capacity, rev,
9769 &color[0], indexmap, vSource, vSink);
9771 std::cout <<
"c The total flow:" << std::endl;
9772 std::cout <<
"s " << flow << std::endl << std::endl;
9774 for (it = imIn.begin(), iend = imIn.end(); it != iend;
9777 o1 = it.getOffset();
9779 imOut.setPixel(o1, 2);
9781 imOut.setPixel(o1, 4);
9783 imOut.setPixel(o1, 3);
9789 template <
class ImageIn,
class ImageMarker,
class SE,
class ImageOut>
9790 RES_C t_geoCutsMinSurfaces_With_Line(
const ImageIn &imIn,
9791 const ImageMarker &imMarker,
9792 const SE &nl, ImageOut &imOut)
9794 MORPHEE_ENTER_FUNCTION(
"t_geoCutsMinSurfaces_With_Line");
9796 std::cout <<
"Enter function Geo-Cuts" << std::endl;
9798 if ((!imOut.isAllocated())) {
9799 MORPHEE_REGISTER_ERROR(
"Not allocated");
9800 return RES_NOT_ALLOCATED;
9803 if ((!imIn.isAllocated())) {
9804 MORPHEE_REGISTER_ERROR(
"Not allocated");
9805 return RES_NOT_ALLOCATED;
9808 if ((!imMarker.isAllocated())) {
9809 MORPHEE_REGISTER_ERROR(
"Not allocated");
9810 return RES_NOT_ALLOCATED;
9814 typename ImageIn::const_iterator it, iend;
9815 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
9816 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
9821 typedef boost::adjacency_list_traits<boost::vecS, boost::vecS,
9824 typedef boost::adjacency_list<
9825 boost::listS, boost::vecS, boost::directedS,
9826 boost::property<boost::vertex_name_t, std::string>,
9828 boost::edge_capacity_t, double,
9829 boost::property<boost::edge_residual_capacity_t, double,
9830 boost::property<boost::edge_reverse_t,
9831 Traits::edge_descriptor>>>>
9838 boost::property_map<Graph_d, boost::edge_capacity_t>::type capacity =
9839 boost::get(boost::edge_capacity, g);
9841 boost::property_map<Graph_d, boost::edge_reverse_t>::type rev =
9842 get(boost::edge_reverse, g);
9844 boost::property_map<Graph_d, boost::edge_residual_capacity_t>::type
9845 residual_capacity = get(boost::edge_residual_capacity, g);
9848 Graph_d::edge_descriptor e1, e2, e3, e4;
9849 Graph_d::vertex_descriptor vSource, vSink;
9852 std::cout <<
"build graph vertices" << std::endl;
9853 for (it = imIn.begin(), iend = imIn.end(); it != iend;
9856 o0 = it.getOffset();
9857 boost::add_vertex(g);
9861 std::cout <<
"number of vertices: " << numVert << std::endl;
9863 vSource = boost::add_vertex(g);
9864 vSink = boost::add_vertex(g);
9866 std::cout <<
"build graph edges" << std::endl;
9867 for (it = imIn.begin(), iend = imIn.end(); it != iend;
9870 o1 = it.getOffset();
9871 double val = imIn.pixelFromOffset(o1);
9872 int marker = imMarker.pixelFromOffset(o1);
9880 boost::tie(e4, in1) = boost::add_edge(vSource, o1, g);
9881 boost::tie(e3, in1) = boost::add_edge(o1, vSource, g);
9882 capacity[e4] = (std::numeric_limits<double>::max)();
9883 capacity[e3] = (std::numeric_limits<double>::max)();
9886 }
else if (marker == 3) {
9887 boost::tie(e4, in1) = boost::add_edge(o1, vSink, g);
9888 boost::tie(e3, in1) = boost::add_edge(vSink, o1, g);
9889 capacity[e4] = (std::numeric_limits<double>::max)();
9890 capacity[e3] = (std::numeric_limits<double>::max)();
9895 neighb.setCenter(o1);
9897 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
9898 const offset_t o2 = nit.getOffset();
9902 double val2 = imIn.pixelFromOffset(o2);
9903 double cost = 10000 / (1 + 1.5 * (val - val2) * (val - val2));
9905 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
9906 boost::tie(e3, in1) = boost::add_edge(o2, o1, g);
9907 capacity[e4] = cost + 1;
9908 capacity[e3] = cost + 1;
9915 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap =
9916 boost::get(boost::vertex_index, g);
9917 std::vector<boost::default_color_type> color(boost::num_vertices(g));
9919 std::cout <<
"Compute Max flow" << std::endl;
9920 #if BOOST_VERSION >= 104700
9922 boykov_kolmogorov_max_flow(g, capacity, residual_capacity, rev,
9923 &color[0], indexmap, vSource, vSink);
9925 double flow = kolmogorov_max_flow(g, capacity, residual_capacity, rev,
9926 &color[0], indexmap, vSource, vSink);
9928 std::cout <<
"c The total flow:" << std::endl;
9929 std::cout <<
"s " << flow << std::endl << std::endl;
9931 for (it = imIn.begin(), iend = imIn.end(); it != iend;
9934 o1 = it.getOffset();
9936 imOut.setPixel(o1, 2);
9938 imOut.setPixel(o1, 4);
9940 imOut.setPixel(o1, 3);
9943 for (it = imIn.begin(), iend = imIn.end(); it != iend;
9946 o1 = it.getOffset();
9947 double val = imOut.pixelFromOffset(o1);
9948 neighb.setCenter(o1);
9950 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
9951 const offset_t o2 = nit.getOffset();
9952 double val2 = imOut.pixelFromOffset(o2);
9954 if (val2 != val && val != 0 && val2 != 0) {
9955 imOut.setPixel(o2, 0);
9956 imOut.setPixel(o1, 0);
9964 template <
class ImageIn,
class ImageMarker,
class SE,
class ImageOut>
9965 RES_C t_geoCutsMultiway_MinSurfaces(
const ImageIn &imIn,
9966 const ImageMarker &imMarker,
9967 const SE &nl, ImageOut &imOut)
9969 MORPHEE_ENTER_FUNCTION(
"t_geoCutsMultiway_MinSurfaces");
9971 std::cout <<
"Enter function Multi way Geo-Cuts" << std::endl;
9973 if ((!imOut.isAllocated())) {
9974 MORPHEE_REGISTER_ERROR(
"Not allocated");
9975 return RES_NOT_ALLOCATED;
9978 if ((!imIn.isAllocated())) {
9979 MORPHEE_REGISTER_ERROR(
"Not allocated");
9980 return RES_NOT_ALLOCATED;
9983 if ((!imMarker.isAllocated())) {
9984 MORPHEE_REGISTER_ERROR(
"Not allocated");
9985 return RES_NOT_ALLOCATED;
9989 typename ImageIn::const_iterator it, iend;
9990 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
9991 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
9996 typedef boost::adjacency_list_traits<boost::vecS, boost::vecS,
9999 typedef boost::adjacency_list<
10000 boost::listS, boost::vecS, boost::directedS,
10001 boost::property<boost::vertex_name_t, std::string>,
10003 boost::edge_capacity_t, double,
10004 boost::property<boost::edge_residual_capacity_t, double,
10005 boost::property<boost::edge_reverse_t,
10006 Traits::edge_descriptor>>>>
10011 double sigma = 1.0;
10013 boost::property_map<Graph_d, boost::edge_capacity_t>::type capacity =
10014 boost::get(boost::edge_capacity, g);
10016 boost::property_map<Graph_d, boost::edge_reverse_t>::type rev =
10017 get(boost::edge_reverse, g);
10019 boost::property_map<Graph_d, boost::edge_residual_capacity_t>::type
10020 residual_capacity = get(boost::edge_residual_capacity, g);
10023 Graph_d::edge_descriptor e1, e2, e3, e4;
10024 Graph_d::vertex_descriptor vSource, vSink;
10028 std::cout <<
"build graph vertices" << std::endl;
10029 for (it = imIn.begin(), iend = imIn.end(); it != iend;
10032 o0 = it.getOffset();
10033 int val2 = imMarker.pixelFromOffset(o0);
10035 imOut.setPixel(o0, 1);
10037 if (val2 > numLabels) {
10041 boost::add_vertex(g);
10045 std::cout <<
"number of Labels: " << numLabels << std::endl;
10047 std::cout <<
"number of vertices: " << numVert << std::endl;
10049 vSource = boost::add_vertex(g);
10050 vSink = boost::add_vertex(g);
10052 std::cout <<
"build graph edges" << std::endl;
10053 for (it = imIn.begin(), iend = imIn.end(); it != iend;
10056 o1 = it.getOffset();
10057 double val = imIn.pixelFromOffset(o1);
10059 neighb.setCenter(o1);
10061 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
10062 const offset_t o2 = nit.getOffset();
10066 double val2 = imIn.pixelFromOffset(o2);
10067 double cost = 10000 / (1 + 1.5 * (val - val2) * (val - val2));
10071 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
10072 boost::tie(e3, in1) = boost::add_edge(o2, o1, g);
10073 capacity[e4] = cost + 1;
10074 capacity[e3] = cost + 1;
10081 for (
int nbk = 2; nbk <= numLabels; nbk++) {
10082 for (it = imIn.begin(), iend = imIn.end(); it != iend;
10085 o0 = it.getOffset();
10086 int val = imMarker.pixelFromOffset(o0);
10089 boost::tie(e4, in1) = boost::edge(vSource, o0, g);
10091 boost::tie(e4, in1) = boost::add_edge(vSource, o0, g);
10092 boost::tie(e3, in1) = boost::add_edge(o0, vSource, g);
10093 capacity[e4] = (std::numeric_limits<double>::max)();
10094 capacity[e3] = (std::numeric_limits<double>::max)();
10098 }
else if (val > 1 && val != nbk) {
10099 boost::tie(e4, in1) = boost::edge(o0, vSink, g);
10101 boost::tie(e4, in1) = boost::add_edge(o0, vSink, g);
10102 boost::tie(e3, in1) = boost::add_edge(vSink, o0, g);
10103 capacity[e4] = (std::numeric_limits<double>::max)();
10104 capacity[e3] = (std::numeric_limits<double>::max)();
10111 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap =
10112 boost::get(boost::vertex_index, g);
10113 std::vector<boost::default_color_type> color(boost::num_vertices(g));
10115 std::cout <<
"Compute Max flow" << std::endl;
10116 #if BOOST_VERSION >= 104700
10118 boykov_kolmogorov_max_flow(g, capacity, residual_capacity, rev,
10119 &color[0], indexmap, vSource, vSink);
10121 double flow = kolmogorov_max_flow(g, capacity, residual_capacity, rev,
10122 &color[0], indexmap, vSource, vSink);
10124 std::cout <<
"c The total flow:" << std::endl;
10125 std::cout <<
"s " << flow << std::endl << std::endl;
10127 for (it = imIn.begin(), iend = imIn.end(); it != iend;
10130 o1 = it.getOffset();
10131 int val = imIn.pixelFromOffset(o1);
10132 int val2 = imOut.pixelFromOffset(o1);
10133 int val3 = imMarker.pixelFromOffset(o1);
10136 if (color[o1] == color[vSource])
10137 imOut.setPixel(o1, nbk);
10141 boost::tie(e4, in1) = boost::edge(vSource, o1, g);
10143 boost::remove_edge(vSource, o1, g);
10144 boost::remove_edge(o1, vSource, g);
10146 }
else if (val3 > 1 && val3 != nbk) {
10147 boost::tie(e4, in1) = boost::edge(o1, vSink, g);
10149 boost::remove_edge(o1, vSink, g);
10150 boost::remove_edge(vSink, o1, g);
10159 template <
class ImageIn,
class ImageMarker,
typename _Power,
class SE,
10161 RES_C t_geoCutsMultiway_Watershed(
const ImageIn &imIn,
10162 const ImageMarker &imMarker,
10163 const _Power Power,
const SE &nl,
10166 MORPHEE_ENTER_FUNCTION(
"t_geoCutsMultiway_Watershed");
10168 std::cout <<
"Enter function Multi way watershed" << std::endl;
10170 if ((!imOut.isAllocated())) {
10171 MORPHEE_REGISTER_ERROR(
"Not allocated");
10172 return RES_NOT_ALLOCATED;
10175 if ((!imIn.isAllocated())) {
10176 MORPHEE_REGISTER_ERROR(
"Not allocated");
10177 return RES_NOT_ALLOCATED;
10180 if ((!imMarker.isAllocated())) {
10181 MORPHEE_REGISTER_ERROR(
"Not allocated");
10182 return RES_NOT_ALLOCATED;
10185 double exposant = (double) Power;
10187 typename ImageIn::const_iterator it, iend;
10188 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
10189 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
10194 typedef boost::adjacency_list_traits<boost::vecS, boost::vecS,
10197 typedef boost::adjacency_list<
10198 boost::listS, boost::vecS, boost::directedS,
10199 boost::property<boost::vertex_name_t, std::string>,
10201 boost::edge_capacity_t, double,
10202 boost::property<boost::edge_residual_capacity_t, double,
10203 boost::property<boost::edge_reverse_t,
10204 Traits::edge_descriptor>>>>
10209 double sigma = 1.0;
10211 boost::property_map<Graph_d, boost::edge_capacity_t>::type capacity =
10212 boost::get(boost::edge_capacity, g);
10214 boost::property_map<Graph_d, boost::edge_reverse_t>::type rev =
10215 get(boost::edge_reverse, g);
10217 boost::property_map<Graph_d, boost::edge_residual_capacity_t>::type
10218 residual_capacity = get(boost::edge_residual_capacity, g);
10221 Graph_d::edge_descriptor e1, e2, e3, e4;
10222 Graph_d::vertex_descriptor vSource, vSink;
10226 std::cout <<
"build graph vertices" << std::endl;
10227 for (it = imIn.begin(), iend = imIn.end(); it != iend;
10230 o0 = it.getOffset();
10231 int val2 = imMarker.pixelFromOffset(o0);
10233 imOut.setPixel(o0, 1);
10235 if (val2 > numLabels) {
10239 boost::add_vertex(g);
10243 std::cout <<
"number of Labels: " << numLabels << std::endl;
10245 std::cout <<
"number of vertices: " << numVert << std::endl;
10247 vSource = boost::add_vertex(g);
10248 vSink = boost::add_vertex(g);
10250 std::cout <<
"build graph edges" << std::endl;
10251 for (it = imIn.begin(), iend = imIn.end(); it != iend;
10254 o1 = it.getOffset();
10255 double val = imIn.pixelFromOffset(o1);
10257 neighb.setCenter(o1);
10259 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
10260 const offset_t o2 = nit.getOffset();
10264 double val2 = imIn.pixelFromOffset(o2);
10265 double valeur = (255.0 / (std::abs(val - val2) + 1));
10266 double cost =
std::pow(valeur, exposant);
10269 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
10270 boost::tie(e3, in1) = boost::add_edge(o2, o1, g);
10271 capacity[e4] = cost;
10272 capacity[e3] = cost;
10279 for (
int nbk = 2; nbk <= numLabels; nbk++) {
10280 for (it = imIn.begin(), iend = imIn.end(); it != iend;
10283 o0 = it.getOffset();
10284 int val = imMarker.pixelFromOffset(o0);
10286 double cost = (std::numeric_limits<double>::max)();
10289 boost::tie(e4, in1) = boost::edge(vSource, o0, g);
10291 boost::tie(e4, in1) = boost::add_edge(vSource, o0, g);
10292 boost::tie(e3, in1) = boost::add_edge(o0, vSource, g);
10293 capacity[e4] = cost;
10294 capacity[e3] = cost;
10298 }
else if (val > 1 && val != nbk) {
10299 boost::tie(e4, in1) = boost::edge(o0, vSink, g);
10301 boost::tie(e4, in1) = boost::add_edge(o0, vSink, g);
10302 boost::tie(e3, in1) = boost::add_edge(vSink, o0, g);
10303 capacity[e4] = cost;
10304 capacity[e3] = cost;
10311 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap =
10312 boost::get(boost::vertex_index, g);
10313 std::vector<boost::default_color_type> color(boost::num_vertices(g));
10315 std::cout <<
"Compute Max flow" << std::endl;
10316 #if BOOST_VERSION >= 104700
10318 boykov_kolmogorov_max_flow(g, capacity, residual_capacity, rev,
10319 &color[0], indexmap, vSource, vSink);
10321 double flow = kolmogorov_max_flow(g, capacity, residual_capacity, rev,
10322 &color[0], indexmap, vSource, vSink);
10324 std::cout <<
"c The total flow:" << std::endl;
10325 std::cout <<
"s " << flow << std::endl << std::endl;
10327 for (it = imIn.begin(), iend = imIn.end(); it != iend;
10330 o1 = it.getOffset();
10331 int val = imIn.pixelFromOffset(o1);
10332 int val2 = imOut.pixelFromOffset(o1);
10333 int val3 = imMarker.pixelFromOffset(o1);
10336 if (color[o1] == color[vSource])
10337 imOut.setPixel(o1, nbk);
10341 boost::tie(e4, in1) = boost::edge(vSource, o1, g);
10343 boost::remove_edge(vSource, o1, g);
10344 boost::remove_edge(o1, vSource, g);
10346 }
else if (val3 > 1 && val3 != nbk) {
10347 boost::tie(e4, in1) = boost::edge(o1, vSink, g);
10349 boost::remove_edge(o1, vSink, g);
10350 boost::remove_edge(vSink, o1, g);
10359 template <
class ImageIn,
class ImageMarker,
typename _Beta,
typename _Sigma,
10360 class SE,
class ImageOut>
10361 RES_C t_MAP_MRF_Ising(
const ImageIn &imIn,
const ImageMarker &imMarker,
10362 const _Beta Beta,
const _Sigma Sigma,
const SE &nl,
10365 MORPHEE_ENTER_FUNCTION(
"t_MAP_MRF_Ising");
10367 std::cout <<
"Enter function t_MAP_MRF_Ising" << std::endl;
10369 if ((!imOut.isAllocated())) {
10370 MORPHEE_REGISTER_ERROR(
"Not allocated");
10371 return RES_NOT_ALLOCATED;
10374 if ((!imIn.isAllocated())) {
10375 MORPHEE_REGISTER_ERROR(
"Not allocated");
10376 return RES_NOT_ALLOCATED;
10379 if ((!imMarker.isAllocated())) {
10380 MORPHEE_REGISTER_ERROR(
"Not allocated");
10381 return RES_NOT_ALLOCATED;
10385 typename ImageIn::const_iterator it, iend;
10386 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
10387 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
10392 typedef boost::adjacency_list_traits<boost::vecS, boost::vecS,
10395 typedef boost::adjacency_list<
10396 boost::listS, boost::vecS, boost::directedS,
10397 boost::property<boost::vertex_name_t, std::string>,
10399 boost::edge_capacity_t, double,
10400 boost::property<boost::edge_residual_capacity_t, double,
10401 boost::property<boost::edge_reverse_t,
10402 Traits::edge_descriptor>>>>
10407 boost::property_map<Graph_d, boost::edge_capacity_t>::type capacity =
10408 boost::get(boost::edge_capacity, g);
10410 boost::property_map<Graph_d, boost::edge_reverse_t>::type rev =
10411 get(boost::edge_reverse, g);
10413 boost::property_map<Graph_d, boost::edge_residual_capacity_t>::type
10414 residual_capacity = get(boost::edge_residual_capacity, g);
10417 Graph_d::edge_descriptor e1, e2, e3, e4;
10418 Graph_d::vertex_descriptor vSource, vSink;
10421 double moy_foreground = 0;
10422 double moy_background = 0;
10423 double nb_foreground = 0;
10424 double nb_background = 0;
10426 double mean_image = 0;
10427 double nb_pixels = 0;
10429 std::cout <<
"build graph vertices" << std::endl;
10430 for (it = imIn.begin(), iend = imIn.end(); it != iend;
10433 o0 = it.getOffset();
10434 int valmarker = imMarker.pixelFromOffset(o0);
10435 int val = imIn.pixelFromOffset(o0);
10436 boost::add_vertex(g);
10439 if (valmarker == 2) {
10440 moy_foreground = moy_foreground + val;
10442 }
else if (valmarker == 3) {
10443 moy_background = moy_background + val;
10447 mean_image = mean_image + val;
10451 mean_image = mean_image / nb_pixels;
10452 moy_foreground = moy_foreground / nb_foreground;
10453 moy_background = moy_background / nb_background;
10454 moy_foreground = moy_foreground / 255.0;
10455 moy_background = moy_background / 255.0;
10457 std::cout <<
"number of vertices: " << numVert << std::endl;
10458 std::cout <<
"Foreground Mean: " << moy_foreground << std::endl;
10459 std::cout <<
"Background Mean: " << moy_background << std::endl;
10461 vSource = boost::add_vertex(g);
10462 vSink = boost::add_vertex(g);
10464 std::cout <<
"build graph edges" << std::endl;
10465 for (it = imIn.begin(), iend = imIn.end(); it != iend;
10468 o1 = it.getOffset();
10470 double val1 = imIn.pixelFromOffset(o1);
10471 int valmarker = imMarker.pixelFromOffset(o1);
10473 neighb.setCenter(o1);
10475 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
10476 const offset_t o2 = nit.getOffset();
10481 double cost = (double) Beta;
10482 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
10483 boost::tie(e3, in1) = boost::add_edge(o2, o1, g);
10484 capacity[e4] = cost;
10485 capacity[e3] = cost;
10491 if (valmarker == 2) {
10492 boost::tie(e4, in1) = boost::add_edge(o1, vSink, g);
10493 boost::tie(e3, in1) = boost::add_edge(vSink, o1, g);
10494 capacity[e4] = (std::numeric_limits<double>::max)();
10495 capacity[e3] = (std::numeric_limits<double>::max)();
10498 }
else if (valmarker == 3) {
10499 boost::tie(e4, in1) = boost::add_edge(vSource, o1, g);
10500 boost::tie(e3, in1) = boost::add_edge(o1, vSource, g);
10501 capacity[e4] = (std::numeric_limits<double>::max)();
10502 capacity[e3] = (std::numeric_limits<double>::max)();
10506 val1 = val1 / 255.0;
10508 double sigma = (double) Sigma;
10510 double sigmab = 0.2;
10512 double slink = (val1 - moy_foreground) * (val1 - moy_foreground) /
10513 (2 * sigma * sigma);
10514 double tlink = (val1 - moy_background) * (val1 - moy_background) /
10515 (2 * sigmab * sigmab);
10517 boost::tie(e4, in1) = boost::add_edge(vSource, o1, g);
10518 boost::tie(e3, in1) = boost::add_edge(o1, vSource, g);
10519 capacity[e4] = slink;
10520 capacity[e3] = slink;
10524 boost::tie(e4, in1) = boost::add_edge(o1, vSink, g);
10525 boost::tie(e3, in1) = boost::add_edge(vSink, o1, g);
10526 capacity[e4] = tlink;
10527 capacity[e3] = tlink;
10533 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap =
10534 boost::get(boost::vertex_index, g);
10535 std::vector<boost::default_color_type> color(boost::num_vertices(g));
10537 std::cout <<
"Compute Max flow" << std::endl;
10538 #if BOOST_VERSION >= 104700
10540 boykov_kolmogorov_max_flow(g, capacity, residual_capacity, rev,
10541 &color[0], indexmap, vSource, vSink);
10543 double flow = kolmogorov_max_flow(g, capacity, residual_capacity, rev,
10544 &color[0], indexmap, vSource, vSink);
10546 std::cout <<
"c The total flow:" << std::endl;
10547 std::cout <<
"s " << flow << std::endl << std::endl;
10549 std::cout <<
"Source Label:" << color[vSource] << std::endl;
10550 std::cout <<
"Sink Label:" << color[vSink] << std::endl;
10552 for (it = imIn.begin(), iend = imIn.end(); it != iend;
10555 o1 = it.getOffset();
10556 if (color[o1] == color[vSource])
10557 imOut.setPixel(o1, 3);
10558 else if (color[o1] == color[vSink])
10559 imOut.setPixel(o1, 2);
10560 else if (color[o1] == 1)
10561 imOut.setPixel(o1, 4);
10567 template <
class ImageIn,
class ImageMarker,
typename _Beta,
typename _Sigma,
10568 class SE,
class ImageOut>
10569 RES_C t_MAP_MRF_edge_preserving(
const ImageIn &imIn,
10570 const ImageMarker &imMarker,
10571 const _Beta Beta,
const _Sigma Sigma,
10572 const SE &nl, ImageOut &imOut)
10574 MORPHEE_ENTER_FUNCTION(
"t_MAP_MRF_edge_preserving");
10576 std::cout <<
"Enter function t_MAP_MRF_edge_preserving" << std::endl;
10578 if ((!imOut.isAllocated())) {
10579 MORPHEE_REGISTER_ERROR(
"Not allocated");
10580 return RES_NOT_ALLOCATED;
10583 if ((!imIn.isAllocated())) {
10584 MORPHEE_REGISTER_ERROR(
"Not allocated");
10585 return RES_NOT_ALLOCATED;
10588 if ((!imMarker.isAllocated())) {
10589 MORPHEE_REGISTER_ERROR(
"Not allocated");
10590 return RES_NOT_ALLOCATED;
10594 typename ImageIn::const_iterator it, iend;
10595 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
10596 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
10601 typedef boost::adjacency_list_traits<boost::vecS, boost::vecS,
10604 typedef boost::adjacency_list<
10605 boost::listS, boost::vecS, boost::directedS,
10606 boost::property<boost::vertex_name_t, std::string>,
10608 boost::edge_capacity_t, double,
10609 boost::property<boost::edge_residual_capacity_t, double,
10610 boost::property<boost::edge_reverse_t,
10611 Traits::edge_descriptor>>>>
10616 double sigma = Sigma;
10618 boost::property_map<Graph_d, boost::edge_capacity_t>::type capacity =
10619 boost::get(boost::edge_capacity, g);
10621 boost::property_map<Graph_d, boost::edge_reverse_t>::type rev =
10622 get(boost::edge_reverse, g);
10624 boost::property_map<Graph_d, boost::edge_residual_capacity_t>::type
10625 residual_capacity = get(boost::edge_residual_capacity, g);
10628 Graph_d::edge_descriptor e1, e2, e3, e4;
10629 Graph_d::vertex_descriptor vSource, vSink;
10632 double moy_foreground = 0;
10633 double moy_background = 0;
10634 double nb_foreground = 0;
10635 double nb_background = 0;
10637 double mean_image = 0;
10638 double nb_pixels = 0;
10640 double mean_difference = 0;
10641 double nb_difference = 0;
10643 std::cout <<
"build graph vertices" << std::endl;
10644 for (it = imIn.begin(), iend = imIn.end(); it != iend;
10647 o0 = it.getOffset();
10648 int valmarker = imMarker.pixelFromOffset(o0);
10649 int val = imIn.pixelFromOffset(o0);
10650 boost::add_vertex(g);
10653 if (valmarker == 2) {
10654 moy_foreground = moy_foreground + val;
10656 }
else if (valmarker == 3) {
10657 moy_background = moy_background + val;
10661 mean_image = mean_image + val;
10664 neighb.setCenter(o0);
10665 double vall1 = (double) val / 255.0;
10667 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
10668 const offset_t o2 = nit.getOffset();
10669 double val2 = imIn.pixelFromOffset(o2);
10674 double vall2 = (double) val2 / 255.0;
10676 mean_difference + (vall2 - vall1) * (vall2 - vall1);
10682 mean_difference = mean_difference / nb_difference;
10683 mean_image = mean_image / nb_pixels;
10685 moy_foreground = moy_foreground / nb_foreground;
10686 moy_background = moy_background / nb_background;
10687 moy_foreground = moy_foreground / 255.0;
10688 moy_background = moy_background / 255.0;
10690 std::cout <<
"number of vertices: " << numVert << std::endl;
10691 std::cout <<
"Foreground Mean: " << moy_foreground << std::endl;
10692 std::cout <<
"Background Mean: " << moy_background << std::endl;
10693 std::cout <<
"Mean difference : " << mean_difference << std::endl;
10695 vSource = boost::add_vertex(g);
10696 vSink = boost::add_vertex(g);
10698 std::cout <<
"build graph edges" << std::endl;
10699 for (it = imIn.begin(), iend = imIn.end(); it != iend;
10702 o1 = it.getOffset();
10703 double val1 = imIn.pixelFromOffset(o1);
10704 int valmarker = imMarker.pixelFromOffset(o1);
10707 neighb.setCenter(o1);
10709 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
10710 const offset_t o2 = nit.getOffset();
10711 double val2 = imIn.pixelFromOffset(o2);
10721 ((
double) Beta) *
std::pow(std::abs(val1 - val2), 0.25);
10722 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
10723 boost::tie(e3, in1) = boost::add_edge(o2, o1, g);
10724 capacity[e4] = cost;
10725 capacity[e3] = cost;
10731 if (valmarker == 2) {
10732 boost::tie(e4, in1) = boost::add_edge(o1, vSink, g);
10733 boost::tie(e3, in1) = boost::add_edge(vSink, o1, g);
10734 capacity[e4] = (std::numeric_limits<double>::max)();
10735 capacity[e3] = (std::numeric_limits<double>::max)();
10738 }
else if (valmarker == 3) {
10739 boost::tie(e4, in1) = boost::add_edge(vSource, o1, g);
10740 boost::tie(e3, in1) = boost::add_edge(o1, vSource, g);
10741 capacity[e4] = (std::numeric_limits<double>::max)();
10742 capacity[e3] = (std::numeric_limits<double>::max)();
10746 double sigma = Sigma;
10747 double sigmab = 0.2;
10749 double val_f = (val1 - moy_foreground) * (val1 - moy_foreground) /
10750 (2 * sigma * sigma);
10751 double val_b = (val1 - moy_background) * (val1 - moy_background) /
10752 (2 * sigmab * sigmab);
10754 boost::tie(e4, in1) = boost::add_edge(vSource, o1, g);
10755 boost::tie(e3, in1) = boost::add_edge(o1, vSource, g);
10756 capacity[e4] = val_f;
10757 capacity[e3] = val_f;
10761 boost::tie(e4, in1) = boost::add_edge(o1, vSink, g);
10762 boost::tie(e3, in1) = boost::add_edge(vSink, o1, g);
10763 capacity[e4] = val_b;
10764 capacity[e3] = val_b;
10770 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap =
10771 boost::get(boost::vertex_index, g);
10772 std::vector<boost::default_color_type> color(boost::num_vertices(g));
10774 std::cout <<
"Compute Max flow" << std::endl;
10775 #if BOOST_VERSION >= 104700
10777 boykov_kolmogorov_max_flow(g, capacity, residual_capacity, rev,
10778 &color[0], indexmap, vSource, vSink);
10780 double flow = kolmogorov_max_flow(g, capacity, residual_capacity, rev,
10781 &color[0], indexmap, vSource, vSink);
10783 std::cout <<
"c The total flow:" << std::endl;
10784 std::cout <<
"s " << flow << std::endl << std::endl;
10786 std::cout <<
"Source Label:" << color[vSource] << std::endl;
10787 std::cout <<
"Sink Label:" << color[vSink] << std::endl;
10789 for (it = imIn.begin(), iend = imIn.end(); it != iend;
10792 o1 = it.getOffset();
10793 if (color[o1] == color[vSource])
10794 imOut.setPixel(o1, 3);
10795 else if (color[o1] == color[vSink])
10796 imOut.setPixel(o1, 2);
10797 else if (color[o1] == 1)
10798 imOut.setPixel(o1, 4);
10988 template <
class ImageIn,
class ImageMarker,
typename _Beta,
typename _Sigma,
10989 class SE,
class ImageOut>
10990 RES_C t_MAP_MRF_Potts(
const ImageIn &imIn,
const ImageMarker &imMarker,
10991 const _Beta Beta,
const _Sigma Sigma,
const SE &nl,
10994 MORPHEE_ENTER_FUNCTION(
"t_MAP_MRF_Potts");
10995 std::cout <<
"Enter function t_MAP_MRF_Potts" << std::endl;
10997 if ((!imOut.isAllocated())) {
10998 MORPHEE_REGISTER_ERROR(
"Not allocated");
10999 return RES_NOT_ALLOCATED;
11002 if ((!imIn.isAllocated())) {
11003 MORPHEE_REGISTER_ERROR(
"Not allocated");
11004 return RES_NOT_ALLOCATED;
11007 if ((!imMarker.isAllocated())) {
11008 MORPHEE_REGISTER_ERROR(
"Not allocated");
11009 return RES_NOT_ALLOCATED;
11013 typename ImageIn::const_iterator it, iend;
11014 morphee::selement::Neighborhood<SE, ImageIn> neighb(imIn, nl);
11015 typename morphee::selement::Neighborhood<SE, ImageIn>::iterator nit, nend;
11020 typedef boost::adjacency_list_traits<boost::vecS, boost::vecS,
11023 typedef boost::adjacency_list<
11024 boost::listS, boost::vecS, boost::directedS,
11025 boost::property<boost::vertex_name_t, std::string>,
11027 boost::edge_capacity_t, double,
11028 boost::property<boost::edge_residual_capacity_t, double,
11029 boost::property<boost::edge_reverse_t,
11030 Traits::edge_descriptor>>>>
11033 Graph_d::edge_descriptor e1, e2, e3, e4;
11034 Graph_d::vertex_descriptor vSource, vSink;
11038 double sigma[4][2];
11039 sigma[0][0] = Sigma;
11040 sigma[0][1] = Sigma;
11042 sigma[1][0] = Sigma;
11043 sigma[1][1] = Sigma;
11045 sigma[2][0] = Sigma;
11046 sigma[2][1] = Sigma;
11048 sigma[3][0] = Sigma;
11049 sigma[3][1] = Sigma;
11051 double combi_valeur[4][2];
11052 combi_valeur[0][0] = 1.0;
11053 combi_valeur[0][1] = 0.0;
11055 combi_valeur[1][0] = 0.75;
11056 combi_valeur[1][1] = 0.0;
11058 combi_valeur[2][0] = 0.5;
11059 combi_valeur[2][1] = 0.0;
11061 combi_valeur[3][0] = 0.0;
11062 combi_valeur[3][1] = 0.0;
11064 double combi_label[4][2];
11065 combi_label[0][0] = 4;
11066 combi_label[0][1] = 0;
11068 combi_label[1][0] = 3;
11069 combi_label[1][1] = 0;
11071 combi_label[2][0] = 2;
11072 combi_label[2][1] = 0;
11074 combi_label[3][0] = 1;
11075 combi_label[3][1] = 0;
11077 for (
int nbk = 0; nbk < nb_combi; nbk++) {
11080 boost::property_map<Graph_d, boost::edge_capacity_t>::type capacity =
11081 boost::get(boost::edge_capacity, g);
11083 boost::property_map<Graph_d, boost::edge_reverse_t>::type rev =
11084 get(boost::edge_reverse, g);
11086 boost::property_map<Graph_d, boost::edge_residual_capacity_t>::type
11087 residual_capacity = get(boost::edge_residual_capacity, g);
11089 std::cout <<
"build graph vertices" << std::endl;
11092 for (it = imIn.begin(), iend = imIn.end(); it != iend;
11095 o0 = it.getOffset();
11096 int val = imIn.pixelFromOffset(o0);
11097 boost::add_vertex(g);
11100 imOut.setPixel(o0, 0);
11104 vSource = boost::add_vertex(g);
11105 vSink = boost::add_vertex(g);
11107 std::cout <<
"build graph edges" << std::endl;
11108 boost::property_map<Graph_d, boost::vertex_index_t>::type indexmap =
11109 boost::get(boost::vertex_index, g);
11110 std::vector<boost::default_color_type> color(boost::num_vertices(g));
11111 std::cout <<
"build neighborhood edges and terminal links" << std::endl;
11113 for (it = imIn.begin(), iend = imIn.end(); it != iend;
11116 o1 = it.getOffset();
11117 neighb.setCenter(o1);
11118 double valCenter = imOut.pixelFromOffset(o1);
11119 double val1 = imIn.pixelFromOffset(o1);
11121 for (nit = neighb.begin(), nend = neighb.end(); nit != nend; ++nit) {
11122 const offset_t o2 = nit.getOffset();
11127 double cost = Beta;
11128 boost::tie(e4, in1) = boost::add_edge(o1, o2, g);
11129 boost::tie(e3, in1) = boost::add_edge(o2, o1, g);
11130 capacity[e4] = cost;
11131 capacity[e3] = cost;
11138 val1 = (val1 - combi_valeur[nbk][0]) * (val1 - combi_valeur[nbk][0]) /
11139 (2 * sigma[nbk][0] * sigma[nbk][0]);
11146 val2 = (val1 - 0.75) * (val1 - 0.75) /
11147 (2 * sigma[nbk][1] * sigma[nbk][1]);
11148 val3 = (val1 - 0.5) * (val1 - 0.5) /
11149 (2 * sigma[nbk][1] * sigma[nbk][1]);
11150 val4 = (val1) * (val1) / (2 * sigma[nbk][1] * sigma[nbk][1]);
11151 }
else if (nbk == 1) {
11152 val2 = (val1 - 1.0) * (val1 - 1.0) /
11153 (2 * sigma[nbk][1] * sigma[nbk][1]);
11154 val3 = (val1 - 0.5) * (val1 - 0.5) /
11155 (2 * sigma[nbk][1] * sigma[nbk][1]);
11156 val4 = (val1) * (val1) / (2 * sigma[nbk][1] * sigma[nbk][1]);
11157 }
else if (nbk == 2) {
11158 val2 = (val1 - 1.0) * (val1 - 1.0) /
11159 (2 * sigma[nbk][1] * sigma[nbk][1]);
11160 val3 = (val1 - 0.75) * (val1 - 0.75) /
11161 (2 * sigma[nbk][1] * sigma[nbk][1]);
11162 val4 = (val1) * (val1) / (2 * sigma[nbk][1] * sigma[nbk][1]);
11163 }
else if (nbk == 3) {
11164 val2 = (val1 - 1.0) * (val1 - 1.0) /
11165 (2 * sigma[nbk][1] * sigma[nbk][1]);
11166 val3 = (val1 - 0.75) * (val1 - 0.75) /
11167 (2 * sigma[nbk][1] * sigma[nbk][1]);
11168 val4 = (val1 - 0.5) * (val1 - 0.5) /
11169 (2 * sigma[nbk][1] * sigma[nbk][1]);
11172 boost::tie(e4, in1) = boost::add_edge(vSource, o1, g);
11173 boost::tie(e3, in1) = boost::add_edge(o1, vSource, g);
11174 capacity[e4] = val1;
11175 capacity[e3] = val1;
11179 boost::tie(e4, in1) = boost::add_edge(vSink, o1, g);
11180 boost::tie(e3, in1) = boost::add_edge(o1, vSink, g);
11181 capacity[e4] = std::min(std::min(val2, val3), val4);
11182 capacity[e3] = std::min(std::min(val2, val3), val4);
11187 std::cout <<
"Compute Max flow" << std::endl;
11188 #if BOOST_VERSION >= 104700
11190 boykov_kolmogorov_max_flow(g, capacity, residual_capacity, rev,
11191 &color[0], indexmap, vSource, vSink);
11193 double flow = kolmogorov_max_flow(g, capacity, residual_capacity, rev,
11194 &color[0], indexmap, vSource, vSink);
11196 std::cout <<
"c The total flow:" << std::endl;
11197 std::cout <<
"s " << flow << std::endl << std::endl;
11199 std::cout <<
"Source Label:" << color[vSource] << std::endl;
11200 std::cout <<
"Sink Label:" << color[vSink] << std::endl;
11202 for (
int i = 0; i < numVert; i++) {
11203 int valimout = imOut.pixelFromOffset(i);
11205 if (valimout == 0) {
11207 imOut.setPixel(i, combi_label[nbk][0]);
RES_T sqrt(const Image< T1 > &imIn, Image< T2 > &imOut)
sqrt() - square root of an image
Definition: DImageArith.hpp:926
RES_T pow(const Image< T1 > &imIn, Image< T2 > &imOut, double exponent=2)
pow() - power of an image
Definition: DImageArith.hpp:905
RES_T test(const Image< T1 > &imIn, const Image< T2 > &imInT, const Image< T2 > &imInF, Image< T2 > &imOut)
test() - Test
Definition: DImageArith.hpp:1109
histogram(const Image< T > &imIn, size_t *h)
Image histogram.
Definition: DImageHistogram.hpp:64
size_t label(const Image< T1 > &imIn, Image< T2 > &imOut, const StrElt &se=DEFAULT_SE)
label() - Image labelization
Definition: DMorphoLabel.hpp:564
double volume(const Image< T > &imIn)
colume() - Volume of an image
Definition: DMeasures.hpp:154
Definition: GeoCutsAlgo_impl_T.hpp:57