Dan Strother is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | #include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
#include <cv.h>
#include <highgui.h>
#include "dlsc_stereobm_models.h"
int img_to_memh(const std::string &filename, const cv::Mat &img) {
std::ofstream memh;
memh.open(filename.c_str());
memh << "// " << std::dec << img.cols << "x" << img.rows << std::endl;
if(img.type() == CV_8UC1) {
memh << "// 8-bit" << std::endl;
for(int y=0;y<img.rows;++y) {
memh << "// row: " << std::dec << y << std::endl;
const uint8_t *row = img.ptr<uint8_t>(y);
for(int x=0;x<img.cols;++x) {
memh << std::setw(2) << std::setfill('0') << std::hex << (unsigned int)(row[x]) << " ";
}
memh << std::endl;
}
} else {
memh << "// 16-bit" << std::endl;
for(int y=0;y<img.rows;++y) {
memh << "// row: " << std::dec << y << std::endl;
const uint16_t *row = img.ptr<uint16_t>(y);
for(int x=0;x<img.cols;++x) {
memh << std::setw(4) << std::setfill('0') << std::hex << (unsigned int)(row[x]) << " ";
}
memh << std::endl;
}
}
memh.close();
return 0;
}
int main(int argc, char *argv[]) {
dlsc_stereobm_params params;
std::string leftfile;
std::string rightfile;
std::string outfile;
bool use_readmemh;
po::options_description desc("Allowed options");
desc.add_options()
("xsobel", po::value<bool>(¶ms.xsobel)->default_value(true), "Use XSOBEL pre-filtering")
("data-max", po::value<int>(¶ms.data_max)->default_value(14), "Maximum XSOBEL output")
("disparities", po::value<int>(¶ms.disparities)->default_value(64), "Disparity levels")
("sad-window", po::value<int>(¶ms.sad_window)->default_value(17), "Sum-of-absolute-differences window")
("texture", po::value<int>(¶ms.texture)->default_value(0), "Texture filtering")
("sub-bits-extra", po::value<int>(¶ms.sub_bits_extra)->default_value(4), "Extra bits for sub-pixel interpolation")
("sub-bits", po::value<int>(¶ms.sub_bits)->default_value(4), "Bits for sub-pixel interpolation")
("unique-mul", po::value<int>(¶ms.unique_mul)->default_value(0), "Uniqueness ratio filtering multiplier")
("unique-div", po::value<int>(¶ms.unique_div)->default_value(4), "Uniqueness ratio filtering divisor")
("left", po::value<std::string>(&leftfile), "Left image file input")
("right", po::value<std::string>(&rightfile), "Right image file input")
("output", po::value<std::string>(&outfile)->default_value("stereo"), "Output files prefix")
("width", po::value<int>(¶ms.width)->default_value(-1), "Width of output image")
("height", po::value<int>(¶ms.height)->default_value(-1), "Height of output image")
("scale", po::value<bool>(¶ms.scale)->default_value(false), "Scale input images")
("readmemh", po::value<bool>(&use_readmemh)->default_value(false), "Use Verilog $readmemh format for output")
;
po::variables_map vm;
po::store(po::parse_command_line(argc,argv,desc),vm);
po::notify(vm);
std::string of_inleft,of_inright,of_outleft,of_outright,of_disp,of_valid,of_filtered;
std::string ext = use_readmemh ? ".memh" : ".jpg";
of_inleft = outfile + "_in_left" + ext;
of_inright = outfile + "_in_right" + ext;
of_outleft = outfile + "_left" + ext;
of_outright = outfile + "_right" + ext;
of_disp = outfile + "_disp" + ext;
of_valid = outfile + "_valid" + ext;
of_filtered = outfile + "_filtered" + ext;
cv::Mat il = cv::imread(leftfile,0);
cv::Mat ir = cv::imread(rightfile,0);
if(!il.data || !ir.data) {
std::cerr << "failed to open input file(s)" << std::endl;
return 1;
}
if(il.size() != ir.size()) {
std::cerr << "left and right images must have save size" << std::endl;
return 1;
}
cv::Mat ilf,irf,id,valid,filtered;
dlsc_stereobm_invoker(il,ir,ilf,irf,id,valid,filtered,params);
if(use_readmemh) {
// write output
img_to_memh(of_inleft, il);
img_to_memh(of_inright, ir);
img_to_memh(of_outleft, ilf);
img_to_memh(of_outright, irf);
img_to_memh(of_disp, id);
img_to_memh(of_valid, valid);
img_to_memh(of_filtered, filtered);
} else {
// normalize for viewing
ilf.convertTo(ilf,CV_8U,256.0/(1.0*params.data_max));
irf.convertTo(irf,CV_8U,256.0/(1.0*params.data_max));
id .convertTo(id, CV_8U,256.0/(1.0*params.disparities*(1<<params.sub_bits)));
// mask out filtered pixels
id &= ~filtered & valid;
// write output
cv::imwrite(of_inleft, il);
cv::imwrite(of_inright, ir);
cv::imwrite(of_outleft, ilf);
cv::imwrite(of_outright, irf);
cv::imwrite(of_disp, id);
cv::imwrite(of_valid, valid);
cv::imwrite(of_filtered, filtered);
}
return 0;
}
|