Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <opencv2/core/core.hpp>
- #include <opencv2/core/mat.hpp>
- #include <opencv2/core/operations.hpp>
- #include <algorithm>
- #include <cstdint>
- #include <fstream>
- #include <iostream>
- #include <string>
- cv::Mat parseCSV(const std::string& path) {
- std::ifstream fin { path };
- std::uint32_t row_size = 0, col_size = 0;
- while (!fin.eof()) {
- std::string line;
- std::getline(fin, line);
- if (line.empty())
- break;
- row_size++;
- std::uint32_t col = std::count(line.begin(), line.end(), ',');
- col++;
- col_size = std::max(col, col_size);
- }
- fin.clear();
- fin.seekg(0);
- cv::Mat_<int> new_m(row_size, col_size);
- std::uint32_t i = 0, j = 0;
- while (!fin.eof()) {
- std::string line;
- std::getline(fin, line);
- if (line.empty())
- break;
- std::istringstream line_stream { line };
- for (std::string s; std::getline(line_stream, s, ',');) {
- new_m(i, j++) = std::stoi(s);
- }
- ++i;
- j = 0;
- }
- fin.close();
- return new_m;
- }
- void writeCSV(const std::string& path, const cv::Mat& m) {
- std::ofstream fout { path };
- fout << cv::format(m, "csv");
- fout.close();
- }
- int main() {
- std::string path { "data.csv" };
- cv::Mat m = parseCSV(path);
- m.at<int>(0, 0) = 100;
- // std::cout << m << std::endl;
- path = "new_" + path;
- writeCSV(path, m);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement