From 05cb35b230083ca180fc02350e2bb3a6b26ba4d4 Mon Sep 17 00:00:00 2001 From: Guillaume Courrier Date: Tue, 17 Dec 2019 21:56:55 +0100 Subject: [PATCH] =?UTF-8?q?impl=C3=A9mentation=20de=20lecture=20et=20?= =?UTF-8?q?=C3=A9criture=20de=20fichiers=20csv=20pour=20l'enregistrement?= =?UTF-8?q?=20des=20descripteurs=20de=20Fourrier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/CMakeLists.txt | 1 + tests/examples/CMakeLists.txt | 2 +- tests/examples/test-csv.cpp | 28 +++++++++++ tests/src/CMakeLists.txt | 4 +- tests/src/file.hpp | 93 +++++++++++++++++++++++++++++++++++ tests/src/knn.cpp | 53 ++------------------ tests/src/math.hpp | 6 +++ 7 files changed, 133 insertions(+), 54 deletions(-) create mode 100644 tests/examples/test-csv.cpp create mode 100644 tests/src/file.hpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2da5c72..9391895 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -3,6 +3,7 @@ project(miniprojet) set(PROJECT_CFLAGS "-Wall -Wextra -Wno-missing-braces -std=c++1z") find_package(OpenCV REQUIRED) +find_package(Boost COMPONENTS system filesystem REQUIRED) add_compile_options(-std=c++17) diff --git a/tests/examples/CMakeLists.txt b/tests/examples/CMakeLists.txt index edfba88..cdbb2b2 100644 --- a/tests/examples/CMakeLists.txt +++ b/tests/examples/CMakeLists.txt @@ -9,7 +9,7 @@ file( foreach(f ${usage_examples}) get_filename_component(exampleName ${f} NAME_WE) add_executable(${exampleName} ${f}) - target_link_libraries(${exampleName} ${OpenCV_LIBS}) + target_link_libraries(${exampleName} ${OpenCV_LIBS} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY}) install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${exampleName} DESTINATION bin RENAME ${CMAKE_PROJECT_NAME}-${exampleName}) diff --git a/tests/examples/test-csv.cpp b/tests/examples/test-csv.cpp new file mode 100644 index 0000000..b89f3c1 --- /dev/null +++ b/tests/examples/test-csv.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include + +int main(int argc, char** argv) { + math::complex c1 = {0.1, 0.2}; + math::complex c2 = {0.3, 0.4}; + math::complex c3 = {0.5, 0.6}; + math::complex c4 = {0.7, 0.8}; + math::csignal s1 = {c1, c2}; + math::csignal s2 = {c3, c4}; + std::pair p1 = {s1, "label1"}; + std::pair p2 = {s2, "label2"}; + math::dataset d = {p1, p2}; + save_as_csv(d, "test.csv"); + + math::dataset d2 = load_csv("test.csv", s1.size()); + + for (auto p: d2) { + for (auto x: p.first) { + std::cout << x << " "; + } + std::cout << p.second << std::endl; + } + return 0; +} diff --git a/tests/src/CMakeLists.txt b/tests/src/CMakeLists.txt index 80aaa9f..a92c8b5 100644 --- a/tests/src/CMakeLists.txt +++ b/tests/src/CMakeLists.txt @@ -1,10 +1,8 @@ add_executable(traitement traitement.cpp) target_link_libraries(traitement ${OpenCV_LIBS}) -find_package(Boost COMPONENTS system filesystem REQUIRED) - add_executable(knn knn.cpp) -target_link_libraries(knn ${OpenCV_LIBS} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY}) +target_link_libraries(knn ${OpenCV_LIBS} ${Boost_SYSTEM_LIBRARY} ${Boost_FILESYSTEM_LIBRARY}) add_executable(neural_network neural_network.cpp) target_link_libraries(neural_network ${OpenCV_LIBS} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY}) diff --git a/tests/src/file.hpp b/tests/src/file.hpp new file mode 100644 index 0000000..0dde7e6 --- /dev/null +++ b/tests/src/file.hpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include "math.hpp" + +struct path_leaf_string { + std::string operator()(const boost::filesystem::directory_entry& entry) const + { + return entry.path().leaf().string(); + } +}; + +void read_directory(const std::string& name, std::vector& v) { + boost::filesystem::path p(name); + boost::filesystem::directory_iterator start(p); + boost::filesystem::directory_iterator end; + std::transform(start, end, std::back_inserter(v), path_leaf_string()); +} + +void save_as_csv(const math::dataset& dataset, std::string filename) { + // file pointer + std::fstream fout; + + // opens an existing csv file or creates a new file. + fout.open(filename, std::ios::out); + + std::string name; + + // Insert the data to file + for (auto p: dataset) { + for (auto x: p.first) { + fout << x.real() << ',' + << x.imag() << ','; + } + fout << p.second; + fout << '\n'; + } +} + +math::dataset& load_csv(std::string filename, int size) { + math::dataset* d = new math::dataset(); + std::fstream fin; + + // Open an existing file + fin.open(filename, std::ios::in); + std::string line, word, temp; + + while (fin >> temp) { + std::pair p; + + std::getline(fin, line); + std::stringstream s(temp); + + int count = 0; + while (std::getline(s, word, ',')) { + if (count == size) { + p.second = word; + } else { + double a = atof(word.c_str()); + std::getline(s, word, ','); + double b = atof(word.c_str()); + p.first.push_back(math::complex(a, b)); + } + count++; + } + d->push_back(p); + } + return *d; +} + +math::dataset get_data(std::string path, int size, int cmax, int threshold) { + math::dataset res; + std::vector dirs; + read_directory(path, dirs); + for (auto dir: dirs) { + std::vector files; + read_directory(path+"/"+dir, files); + + std::string label = dir; + int count = 0; + for (int i=0; count +#include "file.hpp" #include "math.hpp" #include #include #include -#include -#include #include -#include - -using dataset = std::vector>; - -struct path_leaf_string { - std::string operator()(const boost::filesystem::directory_entry& entry) const - { - return entry.path().leaf().string(); - } -}; - -void read_directory(const std::string& name, std::vector& v) { - boost::filesystem::path p(name); - boost::filesystem::directory_iterator start(p); - boost::filesystem::directory_iterator end; - std::transform(start, end, std::back_inserter(v), path_leaf_string()); -} double distance(math::csignal& v1, math::csignal& v2, int n){ if (v1.size() != v2.size()) { @@ -64,37 +46,8 @@ struct pair_comp { }; }; -math::csignal img2desc(std::string filename, int cmax, int threshold) { - cv::Mat img = cv::imread(filename, CV_LOAD_IMAGE_COLOR); - return math::descriptors(img, cmax, threshold); -} - -dataset get_data(std::string path, int size, int cmax, int threshold) { - dataset res; - std::vector dirs; - read_directory(path, dirs); - for (auto dir: dirs) { - std::vector files; - read_directory(path+"/"+dir, files); - - std::string label = dir; - int count = 0; - for (int i=0; count, std::vector>, pair_comp> neighbors; std::map labels; diff --git a/tests/src/math.hpp b/tests/src/math.hpp index 6fe90c5..45a9b1d 100644 --- a/tests/src/math.hpp +++ b/tests/src/math.hpp @@ -14,6 +14,7 @@ namespace math { using csignal = std::vector; using contour = std::vector; constexpr double pi() {return std::atan(1)*4;} + using dataset = std::vector>; void display_abs(const csignal& s) { int count=0; @@ -391,4 +392,9 @@ namespace math { return desc; } + + math::csignal img2desc(std::string filename, int cmax, int threshold) { + cv::Mat img = cv::imread(filename, CV_LOAD_IMAGE_COLOR); + return descriptors(img, cmax, threshold); + } }