REVA-QCAV/utils/crf.py

27 lines
659 B
Python
Raw Normal View History

2017-08-19 08:59:51 +00:00
import numpy as np
import pydensecrf.densecrf as dcrf
2017-08-19 08:59:51 +00:00
def dense_crf(img, output_probs):
h = output_probs.shape[0]
w = output_probs.shape[1]
output_probs = np.expand_dims(output_probs, 0)
output_probs = np.append(1 - output_probs, output_probs, axis=0)
d = dcrf.DenseCRF2D(w, h, 2)
U = -np.log(output_probs)
U = U.reshape((2, -1))
U = np.ascontiguousarray(U)
img = np.ascontiguousarray(img)
d.setUnaryEnergy(U)
2017-08-21 16:00:07 +00:00
d.addPairwiseGaussian(sxy=20, compat=3)
d.addPairwiseBilateral(sxy=30, srgb=20, rgbim=img, compat=10)
2017-08-19 08:59:51 +00:00
2017-08-21 16:00:07 +00:00
Q = d.inference(5)
2017-08-19 08:59:51 +00:00
Q = np.argmax(np.array(Q), axis=0).reshape((h, w))
return Q