|
4 | 4 |
|
5 | 5 | import sys |
6 | 6 | import numpy as np |
7 | | -import cv2 |
8 | 7 | import pydensecrf.densecrf as dcrf |
9 | | -from skimage.segmentation import relabel_sequential |
10 | 8 |
|
11 | | -from pydensecrf.utils import compute_unary, create_pairwise_bilateral, \ |
12 | | - create_pairwise_gaussian |
| 9 | +# Get im{read,write} from somewhere: cv2, skimage, or scipy. |
| 10 | +try: |
| 11 | + from cv2 import imread, imwrite |
| 12 | +except ImportError: |
| 13 | + try: |
| 14 | + from skimage.io import imread, imsave |
| 15 | + imwrite = imsave |
| 16 | + except ImportError: |
| 17 | + from scipy.misc import imread, imsave |
| 18 | + imwrite = imsave |
| 19 | + |
| 20 | +from pydensecrf.utils import compute_unary, create_pairwise_bilateral, create_pairwise_gaussian |
13 | 21 |
|
14 | 22 | if len(sys.argv) != 4: |
15 | 23 | print("Usage: python {} IMAGE ANNO OUTPUT".format(sys.argv[0])) |
|
24 | 32 | ################################## |
25 | 33 | ### Read images and annotation ### |
26 | 34 | ################################## |
27 | | -img = cv2.imread(fn_im) |
28 | | -labels, _, _ = relabel_sequential(cv2.imread(fn_anno, 0)) |
29 | | - |
30 | | -# Compute the number of classes in the label image |
31 | | -M = len(set(labels.flat)) |
| 35 | +img = imread(fn_im) |
| 36 | + |
| 37 | +# Convert the annotation's RGB color to a single 32-bit integer color 0xBBGGRR |
| 38 | +anno_rgb = imread(fn_anno).astype(np.uint32) |
| 39 | +anno_lbl = anno_rgb[:,:,0] + (anno_rgb[:,:,1] << 8) + (anno_rgb[:,:,2] << 16) |
| 40 | + |
| 41 | +# Convert the 32bit integer color to 1, 2, ... labels. |
| 42 | +# Note that all-black, i.e. the value 0 for background will stay 0. |
| 43 | +colors, labels = np.unique(anno_lbl, return_inverse=True) |
| 44 | + |
| 45 | +# And create a mapping back from the labels to 32bit integer colors. |
| 46 | +# But remove the all-0 black, that won't exist in the MAP! |
| 47 | +colors = colors[1:] |
| 48 | +colorize = np.empty((len(colors), 3), np.uint8) |
| 49 | +colorize[:,0] = (colors & 0x0000FF) |
| 50 | +colorize[:,1] = (colors & 0x00FF00) >> 8 |
| 51 | +colorize[:,2] = (colors & 0xFF0000) >> 16 |
| 52 | + |
| 53 | +# Compute the number of classes in the label image. |
| 54 | +# We subtract one because the number shouldn't include the value 0 which stands |
| 55 | +# for "unknown" or "unsure". |
| 56 | +M = len(set(labels.flat)) - 1 |
| 57 | +print(M, " labels and \"unknown\" 0: ", set(labels.flat)) |
32 | 58 |
|
33 | 59 | ########################### |
34 | 60 | ### Setup the CRF model ### |
35 | 61 | ########################### |
36 | 62 | use_2d = False |
| 63 | +# use_2d = True |
37 | 64 | if use_2d: |
| 65 | + print("Using 2D specialized functions") |
| 66 | + |
38 | 67 | # Example using the DenseCRF2D code |
39 | 68 | d = dcrf.DenseCRF2D(img.shape[1], img.shape[0], M) |
40 | 69 |
|
41 | 70 | # get unary potentials (neg log probability) |
42 | | - U = compute_unary(labels, M) |
| 71 | + U = compute_unary(labels, M, GT_PROB=0.7) |
43 | 72 | d.setUnaryEnergy(U) |
44 | 73 |
|
45 | 74 | # This adds the color-independent term, features are the locations only. |
|
52 | 81 | kernel=dcrf.DIAG_KERNEL, |
53 | 82 | normalization=dcrf.NORMALIZE_SYMMETRIC) |
54 | 83 | else: |
| 84 | + print("Using generic 2D functions") |
| 85 | + |
55 | 86 | # Example using the DenseCRF class and the util functions |
56 | | - d = dcrf.DenseCRF(img.shape[0] * img.shape[1], M) |
| 87 | + d = dcrf.DenseCRF(img.shape[1] * img.shape[0], M) |
57 | 88 |
|
58 | 89 | # get unary potentials (neg log probability) |
59 | | - U = compute_unary(labels, M) |
| 90 | + U = compute_unary(labels, M, GT_PROB=0.7) |
60 | 91 | d.setUnaryEnergy(U) |
61 | 92 |
|
62 | 93 | # This creates the color-independent features and then add them to the CRF |
|
74 | 105 |
|
75 | 106 |
|
76 | 107 | #################################### |
77 | | -### Do inference and compute map ### |
| 108 | +### Do inference and compute MAP ### |
78 | 109 | #################################### |
| 110 | + |
| 111 | +# Run five inference steps. |
79 | 112 | Q = d.inference(5) |
80 | | -MAP = np.argmax(Q, axis=0).astype('float32') |
81 | | -MAP *= 255 / MAP.max() |
82 | | -MAP = MAP.reshape(img.shape[:2]) |
83 | | -cv2.imwrite(fn_output, MAP.astype('uint8')) |
84 | 113 |
|
85 | | -# Manually inference |
| 114 | +# Find out the most probable class for each pixel. |
| 115 | +MAP = np.argmax(Q, axis=0) |
| 116 | + |
| 117 | +# Convert the MAP (labels) back to the corresponding colors and save the image. |
| 118 | +MAP = colorize[MAP,:] |
| 119 | +imsave(fn_output, MAP.reshape(img.shape)) |
| 120 | + |
| 121 | +# Just randomly manually run inference iterations |
86 | 122 | Q, tmp1, tmp2 = d.startInference() |
87 | 123 | for i in range(5): |
88 | 124 | print("KL-divergence at {}: {}".format(i, d.klDivergence(Q))) |
|
0 commit comments