Skip to content

Commit 037f429

Browse files
committed
update maxflow for multiple channels
1 parent c2600eb commit 037f429

File tree

7 files changed

+283
-66
lines changed

7 files changed

+283
-66
lines changed

LICENSE

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Copyright 2019 University of Electronic Science and Technology of China
2+
3+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4+
5+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6+
7+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8+
9+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10+
11+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

MANIFEST.in

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
include maxflow_python/*h
2+
include densecrf_python/*h
3+
include dependency/maxflow-v3.0/*h
4+
include dependency/maxflow-v3.0/*inc
5+
recursive-include dependency/densecrf/include/ *
6+
recursive-include dependency/densecrf/external/liblbfgs *
7+
include dependency/densecrf/src/*h
8+
include dependency/densecrf/external/liblbfgs/include/*h
9+
recursive-include dependency/densecrf3d/include/ *
10+
recursive-include dependency/densecrf3d/external/liblbfgs *
11+
include dependency/densecrf3d/src/*h
12+
include dependency/densecrf3d/external/liblbfgs/include/*h
13+
14+

examples/demo_maxflow.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
def demo_maxflow():
99
I = Image.open('../data/brain.png')
1010
Iq = np.asarray(I.convert('L'), np.float32)
11+
# Iq = np.asarray(I, np.float32)
1112
P = np.asarray(Image.open('../data/brain_mask.png').convert('L'), np.float32) / 255
1213

1314
fP = 0.5 + (P - 0.5) * 0.8
@@ -24,7 +25,8 @@ def demo_maxflow():
2425

2526
def demo_interactive_maxflow():
2627
I = Image.open('../data/brain.png')
27-
Iq = np.asarray(I.convert('L'), np.float32)
28+
# Iq = np.asarray(I.convert('L'), np.float32)
29+
Iq = np.asarray(I, np.float32)
2830
P = np.asarray(Image.open('../data/brain_mask.png').convert('L'), np.float32) / 255
2931

3032
fP = 0.5 + (P - 0.5) * 0.8

maxflow_python/maxflow.cpp

+116-59
Large diffs are not rendered by default.

maxflow_python/maxflow3d.cpp

-6
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,3 @@ static PyMethodDef Methods[] = {
317317
{"interactive_max_flow3d", interactive_max_flow3d_wrapper, METH_VARARGS, "computing max flow 3d"},
318318
{NULL, NULL, 0, NULL}
319319
};
320-
321-
//PyMODINIT_FUNC
322-
//initmax_flow3d(void) {
323-
// (void) Py_InitModule("max_flow3d", Methods);
324-
// import_array();
325-
//}

maxflow_python/util.cpp

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include "util.h"
2+
#include <iostream>
3+
#include <cmath>
4+
5+
// for 2d images
6+
template<typename T>
7+
T get_pixel(const T * data, int height, int width, int h, int w)
8+
{
9+
return data[h * width + w];
10+
}
11+
12+
template<typename T>
13+
std::vector<T> get_pixel_vector(const T * data, int height, int width, int channel, int h, int w)
14+
{
15+
std::vector<T> pixel_vector(channel);
16+
for (int c = 0; c < channel; c++){
17+
pixel_vector[c]= data[h * width * channel + w * channel + c];
18+
}
19+
return pixel_vector;
20+
}
21+
22+
template<typename T>
23+
void set_pixel(T * data, int height, int width, int h, int w, T value)
24+
{
25+
data[h * width + w] = value;
26+
}
27+
28+
template<typename T>
29+
float get_l2_distance(std::vector<T> p1, std::vector<T> p2)
30+
{
31+
T sq_sum = 0.0;
32+
for(int d = 0; d < p1.size(); d++)
33+
{
34+
sq_sum = sq_sum + (p1[d] - p2[d]) * (p1[d] - p2[d]);
35+
}
36+
float dis = sqrt(sq_sum);
37+
return dis;
38+
}
39+
40+
template
41+
float get_pixel<float>(const float * data, int height, int width, int h, int w);
42+
43+
template
44+
int get_pixel<int>(const int * data, int height, int width, int h, int w);
45+
46+
template
47+
std::vector<float> get_pixel_vector<float>(const float * data, int height, int width, int channel, int h, int w);
48+
49+
template
50+
unsigned char get_pixel<unsigned char>(const unsigned char * data, int height, int width, int h, int w);
51+
52+
53+
template
54+
void set_pixel<float>(float * data, int height, int width, int h, int w, float value);
55+
56+
template
57+
void set_pixel<int>(int * data, int height, int width, int h, int w, int value);
58+
59+
template
60+
void set_pixel<unsigned char>(unsigned char * data, int height, int width, int h, int w, unsigned char value);
61+
62+
template
63+
float get_l2_distance(std::vector<float> p1, std::vector<float> p2);
64+
65+
66+
// for 3d images
67+
template<typename T>
68+
T get_pixel(const T * data, int depth, int height, int width, int d, int h, int w)
69+
{
70+
return data[(d*height + h) * width + w];
71+
}
72+
73+
template<typename T>
74+
std::vector<T> get_pixel_vector(const T * data, int depth, int height, int width, int channel, int d, int h, int w)
75+
{
76+
std::vector<T> pixel_vector(channel);
77+
for (int c = 0; c < channel; c++){
78+
pixel_vector[c]= data[d*height*width*channel + h * width * channel + w * channel + c];
79+
}
80+
return pixel_vector;
81+
}
82+
83+
template<typename T>
84+
void set_pixel(T * data, int depth, int height, int width, int d, int h, int w, T value)
85+
{
86+
data[(d*height + h) * width + w] = value;
87+
}
88+
89+
template
90+
float get_pixel<float>(const float * data, int depth, int height, int width, int d, int h, int w);
91+
92+
template
93+
std::vector<float> get_pixel_vector<float>(const float * data, int depth, int height, int width, int channel, int d, int h, int w);
94+
95+
template
96+
int get_pixel<int>(const int * data, int depth, int height, int width, int d, int h, int w);
97+
98+
template
99+
unsigned char get_pixel<unsigned char>(const unsigned char * data,
100+
int depth, int height, int width,
101+
int d, int h, int w);
102+
103+
104+
template
105+
void set_pixel<float>(float * data, int depth, int height, int width, int d, int h, int w, float value);
106+
107+
template
108+
void set_pixel<int>(int * data, int depth, int height, int width, int d, int h, int w, int value);
109+
110+
template
111+
void set_pixel<unsigned char>(unsigned char * data,
112+
int depth, int height, int width,
113+
int d, int h, int w, unsigned char value);
114+

maxflow_python/util.h

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <vector>
2+
using namespace std;
3+
4+
// for 2d images
5+
template<typename T>
6+
T get_pixel(const T * data, int height, int width, int h, int w);
7+
8+
template<typename T>
9+
std::vector<T> get_pixel_vector(const T * data, int height, int width, int channel, int h, int w);
10+
11+
template<typename T>
12+
void set_pixel(T * data, int height, int width, int h, int w, T value);
13+
14+
template<typename T>
15+
float get_l2_distance(std::vector<T> p1, std::vector<T> p2);
16+
17+
// for 3d images
18+
template<typename T>
19+
T get_pixel(const T * data, int depth, int height, int width, int d, int h, int w);
20+
21+
template<typename T>
22+
std::vector<T> get_pixel_vector(const T * data, int depth, int height, int width, int channel, int d, int h, int w);
23+
24+
template<typename T>
25+
void set_pixel(T * data, int depth, int height, int width, int d, int h, int w, T value);

0 commit comments

Comments
 (0)