| 1 | #include <cvd/colourmap.h> |
| 2 | #include <cvd/image_io.h> |
| 3 | #include <cvd/tensor_voting.h> |
| 4 | |
| 5 | #include <TooN/SymEigen.h> |
| 6 | |
| 7 | #include <cxxopts.hpp> |
| 8 | |
| 9 | #include <cerrno> |
| 10 | #include <cstring> |
| 11 | |
| 12 | using namespace std; |
| 13 | using namespace CVD; |
| 14 | using namespace TooN; |
| 15 | using namespace cxxopts; |
| 16 | |
| 17 | int main(int argc, char** argv) |
| 18 | { |
| 19 | try |
| 20 | { |
| 21 | Options options("tensor_voting", "Apply tensor voting to an image."); |
| 22 | |
| 23 | options.add_options()("k,kappa", "kappa (larger gives straighter lines)", value<double>())("s,sigma", "sigma (larger givers longer kernels)", value<double>())("i,input", "input image (default is stdin)", value<string>())("o,output", "output image (default is stdout)", value<string>())("h,help", "Help."); |
| 24 | |
| 25 | options.parse(argc, argv); |
| 26 | |
| 27 | if(options.count("help")) |
| 28 | { |
| 29 | cout << options.help({ "", "Group" }) << endl; |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | istream* is = &cin; |
| 34 | ifstream fin; |
| 35 | |
| 36 | if(options.count("input")) |
| 37 | { |
| 38 | string fn = options["input"].as<string>(); |
| 39 | fin.open(fn); |
| 40 | if(!fin.good()) |
| 41 | { |
| 42 | cerr << "Error opening \"" << fn << "\": " << strerror(errno) << endl; |
| 43 | return 1; |
| 44 | } |
| 45 | is = &fin; |
| 46 | } |
| 47 | |
| 48 | ostream* os = &cout; |
| 49 | ofstream fout; |
| 50 | ImageType::ImageType t = ImageType::PNM; |
| 51 | |
| 52 | if(options.count("output")) |
| 53 | { |
| 54 | string fn = options["output"].as<string>(); |
| 55 | fout.open(fn); |
| 56 | if(!fout.good()) |
| 57 | { |
| 58 | cerr << "Error opening \"" << fn << "\": " << strerror(errno) << endl; |
| 59 | return 1; |
| 60 | } |
| 61 | |
| 62 | t = string_to_image_type(fn); |
| 63 | |
| 64 | os = &fout; |
| 65 | } |
| 66 | |
| 67 | Image<float> im = img_load(*is); |
| 68 | |
| 69 | Image<Matrix<2>> tv = dense_tensor_vote_gradients(im, options["sigma"].as<double>(), options["kappa"].as<double>(), 0.02, 256); |
| 70 | |
| 71 | Image<float> r(im.size()); |
| 72 | float hi = 0; |
| 73 | |
| 74 | for(int y = 0; y < r.size().y; y++) |
| 75 | for(int x = 0; x < r.size().x; x++) |
| 76 | { |
| 77 | SymEigen<2> s(tv[y][x]); |
| 78 | r[y][x] = s.get_evalues()[1] - s.get_evalues()[0]; |
| 79 | hi = max(hi, r[y][x]); |
| 80 | } |
| 81 | |
| 82 | Image<Rgb<byte>> out(r.size()); |
| 83 | |
| 84 | for(int y = 0; y < r.size().y; y++) |
| 85 | for(int x = 0; x < r.size().x; x++) |
| 86 | out[y][x] = Colourmap<Rgb<byte>>::grey(r[y][x] / hi); |
| 87 | |
| 88 | img_save(out, *os, t); |
| 89 | } |
| 90 | catch(const cxxopts::OptionException& e) |
| 91 | { |
| 92 | cerr << "Error: " << e.what() << endl; |
| 93 | return 1; |
| 94 | } |
| 95 | catch(Exceptions::All e) |
| 96 | { |
| 97 | cerr << "Error: " << e.what << endl; |
| 98 | return 1; |
| 99 | } |
| 100 | } |