diff --git a/lines.cpp b/lines.cpp index 4a46ca9..572a29b 100644 --- a/lines.cpp +++ b/lines.cpp @@ -1,5 +1,8 @@ #include #include +#include +#include +#include #ifdef HAS_OPENCV3 #include //Any OPENCV3 code @@ -22,18 +25,103 @@ auto BROWN = cv::Scalar(90,100,60); auto BLACK = cv::Scalar(0,0,0); auto YELLOW = cv::Scalar(20,200,200); +void usage(char* program, std::ostream &buf) { + buf << "Usage: " << program << "[options] IMAGE-INPUT" << std::endl; + buf << "Identify the image as a book in perspective, get deperspectivized content" << std::endl; + buf << std::endl; + buf << "Options:" << std::endl; + buf << " -p PROFILE Save the transformation matrix in PROFILE" << std::endl; + buf << " -l LEFTPAGE Save the left page in LEFTPAGE" << std::endl; + buf << " -r RIGHTPAGE Save the right page in RIGHTPAGE" << std::endl; + buf << " -h HELP Show this help and exit" << std::endl; +} + +class Options +{ + public: + char *left; + char *right; + char *profile; + char *input; + Options(); + Options(int argc, char*argv[]); + bool parse(int argc, char*argv[]); + ~Options(); +}; +Options::Options() +{ + left = right = profile = input = NULL; +} +Options::~Options() +{ + if(left == NULL) { + free(left); + } + if(right == NULL) { + free(right); + } + if(profile == NULL) { + free(profile); + } + //input doesn't need to be free-d: it points to argv[argc-1] +} +Options::Options(int argc, char *argv[]) +{ + left = right = profile = input = NULL; + parse(argc, argv); +} + +/* Options::parse parse arguments, return true if help is requested */ +bool Options::parse(int argc, char *argv[]) +{ + int c; + while((c=getopt(argc, argv, "p:l:r:h")) != -1) + { + switch(c) + { + case 'l': + left = strdup(optarg); + break; + case 'r': + right = strdup(optarg); + break; + case 'p': + profile = strdup(optarg); + break; + case 'h': + return true; + break; + default: + throw std::runtime_error("Parsing error: invalid argument"); + } + } + if(optind >= argc) { + std::cerr << "Error: " << argv[0] << " needs an argument" << std::endl; + throw std::runtime_error("Parsing error: argument needed"); + } + input = argv[optind++]; + if(optind < argc) { + std::cerr << "Error: too many arguments supplied" << std::endl; + throw std::runtime_error("Parsing error: too many arguments"); + } + return false; +} int main(int argc, char *argv[]) { - char const *fname = "files/masckera.png"; - if( 1(i); + for(int j=0; j < 3; j++) { + profilebuf << std::fixed << std::setprecision(16); + profilebuf << row[j] << "\t"; //yes, there's a trailing tab + } + } + profilebuf << std::endl; + } + profilebuf.close(); + } + #ifdef _DEBUG cv::line(img, corn_1, middle1, MAGENTA, dotwidth>>3); cv::line(img, corn_2, middle1, MAGENTA, dotwidth>>3); @@ -158,13 +265,19 @@ int main(int argc, char *argv[]) break; } #endif - if(2 params; + + + //TODO: distinguish left and right + std::vector params; + if(args.left || args.right) { params.push_back(CV_IMWRITE_PNG_COMPRESSION); params.push_back(9); - Mat dst; - img.convertTo(dst, CV_8UC3); - cv::imwrite(argv[2], dst, params); + if(args.left) { + cv::imwrite(args.left, rect[0], params); + } + if(args.right) { + cv::imwrite(args.right, rect[1], params); + } } return EXIT_SUCCESS;