共享代码用~
1 #pragma once 2 #pragma warning(disable:4996) 3 //#include "convert_image.hpp" 4 #include <Windows.h> 5 #include <iostream> 6 #include <string> 7 #include <istream> 8 #include <ostream> 9 #include <fstream> 10 #include <sstream> 11 #include <vector> 12 using namespace std; 13 14 class Play { 15 public: 16 const int total = 6570; 17 void Init(string inputPath) { 18 ifstream is; 19 char tmp[256]; 20 src.clear(); 21 for (int k = 1; k <= total; k++) { 22 is.open(inputPath + getTextFileName(k)); 23 src.push_back(vector<string>()); 24 size_t n = src.size() - 1; 25 src[n].clear(); memset(tmp, 0, sizeof(tmp)); 26 while (is.getline(tmp, 80)) { 27 src[n].push_back(tmp); 28 memset(tmp, 0, sizeof(tmp)); 29 } 30 is.close(); 31 } 32 } 33 void Show() { 34 for (int i = 0; i < src.size(); i++) { 35 system("cls"); 36 for (int j = 0; j < src[i].size(); j++) printf("%s\n", src[i][j].c_str()); 37 Sleep(20); 38 } 39 } 40 protected: 41 vector<vector<string>> src; 42 string getTextFileName(int x) { 43 stringstream ss; 44 string file; 45 ss << x; ss >> file; 46 file += ".txt"; 47 return file; 48 } 49 }; 50 51 52 int main() { 53 //ConvertSize::Do("../pic/","../done/", 40, 30); 54 //ConvertType::Do("../done/", "../digit/", 40, 30); 55 Play player; 56 player.Init("../digit/"); 57 player.Show(); 58 }
main.cpp
1 #include <iostream> 2 #include <string> 3 #include <istream> 4 #include <ostream> 5 #include <fstream> 6 #include <sstream> 7 #include <vector> 8 #include <opencv.hpp> 9 10 using namespace cv; 11 using namespace std; 12 13 class Utils { 14 public: 15 static const int total = 6570; 16 static string getFileName(int x) { 17 stringstream ss; 18 string file; 19 ss << x; ss >> file; 20 file += ".jpg"; 21 return file; 22 } 23 static void saveImage(string outputPath, Mat& dst) { 24 imwrite(outputPath, dst); 25 } 26 }; 27 28 class ConvertSize : public Utils { 29 public: 30 static void Do(string inputPath, string outputPath, size_t width, size_t height) { 31 Mat dst; 32 string fileName, tmp; 33 for (int i = 1; i <= total; i++) { 34 fileName = getFileName(i); 35 dealSize(inputPath + fileName, dst, Size(width, height)); 36 saveImage(outputPath + fileName, dst); 37 } 38 } 39 protected: 40 static void dealSize(string inputPath, Mat& dst, Size size) { 41 Mat src; dst = Mat(); 42 src = imread(inputPath); 43 resize(src, dst, size); 44 } 45 }; 46 47 class ConvertType : public Utils { 48 public: 49 static void Do(string inputPath, string outputPath, size_t width, size_t height) { 50 string fileName; 51 for (int i = 1; i <= total; i++) { 52 fileName = getFileName(i); 53 dealType(inputPath + fileName, outputPath + fileName, Size(width, height)); 54 } 55 } 56 protected: 57 // 1 : dark 0 : light 58 static void dealType(string inputPath, string outputPath, Size size) { 59 outputPath.pop_back(); outputPath.pop_back(); outputPath.pop_back(); 60 outputPath += "txt"; 61 Mat src = imread(inputPath); 62 vector<vector<char> > dst; 63 for (int i = 0; i < src.rows; i++) { 64 dst.push_back(vector<char>()); 65 for (int j = 0; j < src.cols; j++) { 66 auto tot = src.at<Vec3b>(i, j)[0] + src.at<Vec3b>(i, j)[1] + src.at<Vec3b>(i, j)[2]; 67 if (tot / 3 > 200) dst[i].push_back(‘ ‘); 68 else dst[i].push_back(‘0‘); 69 } 70 } 71 ofstream of; 72 of.open(outputPath); 73 for (int i = 0; i < dst.size(); i++) { 74 for (int j = 0; j < dst[i].size(); j++) { 75 of << dst[i][j]; 76 } 77 of << endl; 78 } 79 of.close(); 80 } 81 };
convert_image.hpp
时间: 2024-10-12 15:52:16