MCPcopy Create free account
hub / github.com/HealthX-Lab/MedCLIP-SAMv2 / postprocess_crf

Function postprocess_crf

postprocessing/postprocess_saliency_maps.py:14–67  ·  view source on GitHub ↗
(args)

Source from the content-addressed store, hash-verified

12 return 1 / (1 + np.exp(-x))
13
14def postprocess_crf(args):
15 files = os.listdir(args.sal_path)
16
17 if (not os.path.exists(args.output_path)):
18 os.makedirs(args.output_path)
19
20 for file in tqdm(files):
21
22 img = cv2.imread(args.input_path+'/'+file, 1)
23 annos = cv2.imread(args.sal_path+'/'+file, 0)
24 annos = cv2.resize(annos, (img.shape[1], img.shape[0]))
25 output = args.output_path+'/'+file
26
27 # Setup the CRF model
28 d = dcrf.DenseCRF2D(img.shape[1], img.shape[0], args.m)
29
30 anno_norm = annos / 255.
31 n_energy = -np.log((1.0 - anno_norm + args.epsilon)) / (args.tau * sigmoid(1 - anno_norm))
32 p_energy = -np.log(anno_norm + args.epsilon) / (args.tau * sigmoid(anno_norm))
33
34 U = np.zeros((args.m, img.shape[0] * img.shape[1]), dtype='float32')
35 U[0, :] = n_energy.flatten()
36 U[1, :] = p_energy.flatten()
37
38 d.setUnaryEnergy(U)
39
40 d.addPairwiseGaussian(sxy=args.gaussian_sxy, compat=3)
41 d.addPairwiseBilateral(sxy=args.bilateral_sxy, srgb=args.bilateral_srgb, rgbim=img, compat=5)
42
43 # Do the inference
44 Q = d.inference(1)
45 map = np.argmax(Q, axis=0).reshape((img.shape[0], img.shape[1]))
46
47 # Save the output as image
48 segmented_image = map.astype('uint8')*255
49
50 nb_blobs, im_with_separated_blobs, stats, _ = cv2.connectedComponentsWithStats(segmented_image)
51 sizes = stats[:, cv2.CC_STAT_AREA]
52
53 # Sort sizes (ignoring the background at index 0)
54 sorted_sizes = sorted(sizes[1:], reverse=True)
55
56 # Determine the top K sizes
57 top_k_sizes = sorted_sizes[:args.num_contours]
58
59 im_result = np.zeros_like(im_with_separated_blobs)
60
61 for index_blob in range(1, nb_blobs):
62 if sizes[index_blob] in top_k_sizes:
63 im_result[im_with_separated_blobs == index_blob] = 255
64
65 segmented_image = im_result
66
67 cv2.imwrite(output, segmented_image)
68
69def postprocess_thresholding(args):
70 files = os.listdir(args.sal_path)

Callers 1

Calls 2

sigmoidFunction · 0.85
logMethod · 0.80

Tested by

no test coverage detected