1 module dcv.example.stereo; 2 3 import std.math; 4 import std.range; 5 import std.stdio; 6 7 import mir.ndslice.algorithm : reduce; 8 9 import dcv.imgproc; 10 import dcv.io.image; 11 import dcv.plot; 12 import dcv.multiview.stereo.matching; 13 14 void main(string[] args) 15 { 16 if (args.length != 4) 17 { 18 writeln("Usage: stereo <left image> <right image> <true disparity image>"); 19 return; 20 } 21 22 //Load the input images 23 auto left = imread(args[1]); 24 auto right = imread(args[2]); 25 auto groundTruth = imread(args[3]); 26 27 //Create a matcher 28 auto props = StereoPipelineProperties(left.width, left.height, left.channels); 29 auto matcher = semiGlobalMatchingPipeline(props); 30 31 //Estimate the disparity 32 auto estimate = matcher.evaluate(left, right); 33 34 //Scale by a factor 4 for displaying 35 estimate[] *= 4; 36 37 //Compute the accuracy. In this case we consider something within 3 units correct. Note that we have scaled everything up by a factor of 4. 38 float c = 0; 39 40 float evalAccum(float accum, uint est, uint gt) 41 { 42 if (gt != 0) 43 { 44 c++; 45 return accum + (abs(cast(float)est - cast(float)gt) <= 12.0f ? 1.0f : 0.0f); 46 } 47 else 48 { 49 return accum; 50 } 51 } 52 53 auto acc = reduce!(evalAccum)(0.0f, estimate, groundTruth.sliced.rgb2gray); 54 55 writeln((acc / cast(float)c) * 100, "% accuracy (<=3px)"); 56 57 //Display estimated disparity and true disparity 58 imshow(estimate); 59 imshow(groundTruth); 60 waitKey(); 61 }