diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 93a42be64b89874318360cdcaa8cd739ec6bdb9b..9dccabf0841cb1aec4c76a3af56d2e73b9cce9a5 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -10,3 +10,11 @@ target_link_libraries(color_approach ball_tracking ${Boost_LIBRARIES} ) + +add_executable(log_reg_approach + img_proc/log_reg_approach.cpp + ) +target_link_libraries(log_reg_approach + ball_tracking + ${Boost_LIBRARIES} + ) diff --git a/examples/img_proc/log_reg_approach.cpp b/examples/img_proc/log_reg_approach.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d4a86b7091a3167e300d1c49164795dd50f38c4e --- /dev/null +++ b/examples/img_proc/log_reg_approach.cpp @@ -0,0 +1,81 @@ + +#include <ball_tracking/img_proc.hpp> +#include <ball_tracking/utils.hpp> +#include <opencv2/opencv.hpp> +#include <opencv2/highgui/highgui.hpp> +#include <iostream> +#include <boost/program_options.hpp> +#include <json.hpp> + +using namespace cv; +using namespace std; +using namespace ball_tracking; +using json = nlohmann::json; + +int main(int argc, char** argv) { + try { + namespace po = boost::program_options; + po::options_description desc("Options"); + desc.add_options() + ("help", "Produce help message") + ("input,i", po::value<string>(), "path to the input image") + ("conf,c", po::value<string>(), "path to the JSON configuration") + ("output,o", po::value<string>(), "path to the output image"); + po::variables_map vm; + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + + if (vm.count("help")) { + cout << desc << endl; + return 0; + } + if (!vm.count("input")) { + cerr << "Error: You should provide the image to process" << endl; + return 1; + } + + json jobj = load_json(vm["conf"].as<string>()); + Mat model_coef = json2cvmat(jobj.at("model_coefs")); + + Mat back = imread(jobj.at("background"), CV_LOAD_IMAGE_COLOR); + if(! back.data ) // Check for invalid input background + { + cout << "Could not open or find the backgroundimage" << std::endl ; + return -1; + } + string fname = vm["input"].as<string>(); + Mat img = imread(fname, CV_LOAD_IMAGE_COLOR); + + imshow("Color", back); + waitKey(0); + //Calculate the data with coefs + Mat im_proc = quadf_log_reg(img, back, model_coef); + cout << "pix data:" << im_proc.at<double>(340, 290) << endl + << im_proc.at<double>(342, 291) << endl + << im_proc.at<double>(290, 340) << endl + << im_proc.at<double>(2, 2) << endl; + MatIterator_<double> it_ans = im_proc.begin<double>(),end=im_proc.end<double>(); + //MatIterator_<Vec3b> it=im_proc.begin<Vec3b>(), end=im_proc.end<Vec3b>(); + while(it_ans != end){ + //cout << "pix data:" << *it_ans << endl; + double prob = 1.0 / (1.0 + exp(-*it_ans)); + //*it_ans = 255*prob; + *it_ans = (prob > 0.95) ? 255 : 0; + it_ans++; + } + imshow("Color", im_proc); + waitKey(0); + + //exp(-0.5*im_proc, im_proc); + //im_proc = 255*im_proc; + //imshow("Processed", im_proc); + //waitKey(0); + if (vm.count("output")) { + imwrite(vm["output"].as<string>(), im_proc); + } + return 0; + } catch (std::exception& ex) { + cerr << "Exception: " << ex.what() << endl; + return 1; + } +}