共享代码用~
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[65535]; 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, 65535)) { 27 src[n].push_back(tmp); 28 memset(tmp, 0, sizeof(tmp)); 29 } 30 is.close(); 31 } 32 } 33 void Show() { 34 getchar(); 35 system("cls"); 36 HANDLE hout; 37 COORD pos = {0, 0}; 38 for (int i = 0; i < src.size(); i++) { 39 hout = GetStdHandle(STD_OUTPUT_HANDLE); 40 SetConsoleCursorPosition(hout, pos); 41 SetConsoleTextAttribute(hout, 0x0f); 42 for (int j = 0; j < src[i].size(); j++) printf("%s ", src[i][j].c_str()); 43 Sleep(22); 44 } 45 } 46 protected: 47 vector<vector<string>> src; 48 string getTextFileName(int x) { 49 stringstream ss; 50 string file; 51 ss << x; ss >> file; 52 file += ".txt"; 53 return file; 54 } 55 }; 56 57 58 int main() { 59 //ConvertSize::Do("../pic/","../done/", 160, 120); 60 //ConvertType::Do("../done/", "../digit/", 160, 120); 61 62 Play player; 63 player.Init("../digit/"); 64 player.Show(); 65 }
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 };