Skip to content

Commit 42402df

Browse files
committed
Utils example also store output.
So it can be used usefully without matplotlib.
1 parent 94d1cdd commit 42402df

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

examples/utils_example.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66
import numpy as np
77
import cv2
88
import pydensecrf.densecrf as dcrf
9-
import matplotlib.pylab as plt
109
from skimage.segmentation import relabel_sequential
1110

1211
from pydensecrf.utils import compute_unary, create_pairwise_bilateral, \
1312
create_pairwise_gaussian
1413

15-
if len(sys.argv) != 3:
16-
print("Usage: python {} IMAGE ANNO".format(sys.argv[0]))
14+
if len(sys.argv) != 4:
15+
print("Usage: python {} IMAGE ANNO OUTPUT".format(sys.argv[0]))
16+
print("")
17+
print("IMAGE and ANNO are inputs and OUTPUT is where the result should be written.")
1718
sys.exit(1)
1819

1920
fn_im = sys.argv[1]
2021
fn_anno = sys.argv[2]
22+
fn_output = sys.argv[3]
2123

2224
##################################
2325
### Read images and annotation ###
@@ -73,16 +75,13 @@
7375
### Do inference and compute map ###
7476
####################################
7577
Q = d.inference(5)
76-
map = np.argmax(Q, axis=0).reshape(img.shape[:2])
77-
78-
res = map.astype('float32') * 255 / map.max()
79-
plt.imshow(res)
80-
plt.show()
81-
78+
MAP = np.argmax(Q, axis=0).astype('float32')
79+
MAP *= 255 / MAP.max()
80+
MAP = MAP.reshape(img.shape[:2])
81+
cv2.imwrite(fn_output, MAP.astype('uint8'))
8282

8383
# Manually inference
8484
Q, tmp1, tmp2 = d.startInference()
8585
for i in range(5):
8686
print("KL-divergence at {}: {}".format(i, d.klDivergence(Q)))
8787
d.stepInference(Q, tmp1, tmp2)
88-

0 commit comments

Comments
 (0)