implémentation de lecture et écriture de fichiers csv pour l'enregistrement des descripteurs de Fourrier
This commit is contained in:
parent
0ce09ddc13
commit
05cb35b230
7 changed files with 133 additions and 54 deletions
|
@ -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)
|
||||
|
||||
|
|
|
@ -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})
|
||||
|
|
28
tests/examples/test-csv.cpp
Normal file
28
tests/examples/test-csv.cpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
#include <math.hpp>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <file.hpp>
|
||||
#include <math.hpp>
|
||||
|
||||
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<math::csignal, std::string> p1 = {s1, "label1"};
|
||||
std::pair<math::csignal, std::string> 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;
|
||||
}
|
|
@ -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})
|
||||
|
|
93
tests/src/file.hpp
Normal file
93
tests/src/file.hpp
Normal file
|
@ -0,0 +1,93 @@
|
|||
#include <boost/filesystem.hpp>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#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<std::string>& 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<math::csignal, std::string> 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<std::string> dirs;
|
||||
read_directory(path, dirs);
|
||||
for (auto dir: dirs) {
|
||||
std::vector<std::string> files;
|
||||
read_directory(path+"/"+dir, files);
|
||||
|
||||
std::string label = dir;
|
||||
int count = 0;
|
||||
for (int i=0; count<size/4 && i<files.size(); ++i) {
|
||||
try {
|
||||
math::csignal d = math::img2desc(path+"/"+dir+"/"+files[i], cmax, threshold);
|
||||
res.push_back({d, label});
|
||||
count++;
|
||||
} catch (std::length_error& e) {
|
||||
std::cout << "No contour: image skiped." << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << res.size() << std::endl;
|
||||
}
|
||||
return res;
|
||||
}
|
|
@ -1,28 +1,10 @@
|
|||
#include <map>
|
||||
#include "file.hpp"
|
||||
#include "math.hpp"
|
||||
#include <stdexcept>
|
||||
#include <queue>
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <iterator>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
using dataset = std::vector<std::pair<math::csignal, std::string>>;
|
||||
|
||||
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<std::string>& 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<std::string> dirs;
|
||||
read_directory(path, dirs);
|
||||
for (auto dir: dirs) {
|
||||
std::vector<std::string> files;
|
||||
read_directory(path+"/"+dir, files);
|
||||
|
||||
std::string label = dir;
|
||||
int count = 0;
|
||||
for (int i=0; count<size/4 && i<files.size(); ++i) {
|
||||
try {
|
||||
math::csignal d = img2desc(path+"/"+dir+"/"+files[i], cmax, threshold);
|
||||
res.push_back({d, label});
|
||||
count++;
|
||||
} catch (std::length_error& e) {
|
||||
std::cout << "No contour: image skiped." << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << res.size() << std::endl;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
int k = 20;
|
||||
int size = 100;
|
||||
std::string path;
|
||||
|
@ -109,8 +62,8 @@ int main(int argc, char** argv) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
dataset references = get_data(path, size, cmax, threshold);
|
||||
math::csignal sample = img2desc(path+"/arret/arret0199.jpg", cmax, threshold);
|
||||
math::dataset references = get_data(path, size, cmax, threshold);
|
||||
math::csignal sample = math::img2desc(path+"/arret/arret0199.jpg", cmax, threshold);
|
||||
std::priority_queue<std::pair<double, std::string>, std::vector<std::pair<double, std::string>>, pair_comp> neighbors;
|
||||
std::map<std::string, int> labels;
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ namespace math {
|
|||
using csignal = std::vector<complex>;
|
||||
using contour = std::vector<cv::Point>;
|
||||
constexpr double pi() {return std::atan(1)*4;}
|
||||
using dataset = std::vector<std::pair<csignal, std::string>>;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue