| 85 | }; |
| 86 | |
| 87 | int main( int argc, char* argv[]){ |
| 88 | if (argc<4){ |
| 89 | printf("Usage: %s image annotations output\n", argv[0] ); |
| 90 | return 1; |
| 91 | } |
| 92 | // Number of labels [use only 4 to make our lives a bit easier] |
| 93 | const int M = 4; |
| 94 | // Load the color image and some crude annotations (which are used in a simple classifier) |
| 95 | int W, H, GW, GH; |
| 96 | unsigned char * im = readPPM( argv[1], W, H ); |
| 97 | if (!im){ |
| 98 | printf("Failed to load image!\n"); |
| 99 | return 1; |
| 100 | } |
| 101 | unsigned char * anno = readPPM( argv[2], GW, GH ); |
| 102 | if (!anno){ |
| 103 | printf("Failed to load annotations!\n"); |
| 104 | return 1; |
| 105 | } |
| 106 | if (W!=GW || H!=GH){ |
| 107 | printf("Annotation size doesn't match image!\n"); |
| 108 | return 1; |
| 109 | } |
| 110 | // Get the labeling |
| 111 | VectorXs labeling = getLabeling( anno, W*H, M ); |
| 112 | const int N = W*H; |
| 113 | |
| 114 | // Get the logistic features (unary term) |
| 115 | // Here we just use the color as a feature |
| 116 | MatrixXf logistic_feature( 4, N ), logistic_transform( M, 4 ); |
| 117 | logistic_feature.fill( 1.f ); |
| 118 | for( int i=0; i<N; i++ ) |
| 119 | for( int k=0; k<3; k++ ) |
| 120 | logistic_feature(k,i) = im[3*i+k] / 255.; |
| 121 | |
| 122 | for( int j=0; j<logistic_transform.cols(); j++ ) |
| 123 | for( int i=0; i<logistic_transform.rows(); i++ ) |
| 124 | logistic_transform(i,j) = 0.01*(1-2.*random()/RAND_MAX); |
| 125 | |
| 126 | // Setup the CRF model |
| 127 | DenseCRF2D crf(W, H, M); |
| 128 | // Add a logistic unary term |
| 129 | crf.setUnaryEnergy( logistic_transform, logistic_feature ); |
| 130 | |
| 131 | // Add simple pairwise potts terms |
| 132 | crf.addPairwiseGaussian( 3, 3, new PottsCompatibility( 1 ) ); |
| 133 | // Add a longer range label compatibility term |
| 134 | crf.addPairwiseBilateral( 80, 80, 13, 13, 13, im, new MatrixCompatibility( MatrixXf::Identity(M,M) ) ); |
| 135 | |
| 136 | // Choose your loss function |
| 137 | // LogLikelihood objective( labeling, 0.01 ); // Log likelihood loss |
| 138 | // Hamming objective( labeling, 0.0 ); // Global accuracy |
| 139 | // Hamming objective( labeling, 1.0 ); // Class average accuracy |
| 140 | // Hamming objective( labeling, 0.2 ); // Hamming loss close to intersection over union |
| 141 | IntersectionOverUnion objective( labeling ); // Intersection over union accuracy |
| 142 | |
| 143 | int NIT = 5; |
| 144 | const bool verbose = true; |
nothing calls this directly
no test coverage detected