digits.png 2000 * 1000, wherein the size of each number is 20 * 20, a total of 5000 ((2000 * 1000) / (20 * 20)) numbers, type of [0-9], [0-9 ] 10 each digit numbers 5000/10 = 500 samples and their sequence into a single image is divided into 20 * 20
#include <the iostream> #include <opencv2 \ opencv.hpp> the using namespace CV; the using namespace STD; int main () { Mat IMG = imread ( " 1.png " ); Mat Gray; cvtColor (IMG, Gray, CV_BGR2GRAY) ; threshold (Gray, Gray, 0 , 255 , CV_THRESH_BINARY); // digits.png is 2000 * 1000, wherein the size of each number is 20 * 20, // total of 5000 ((2000 * 1000) / (20 * 20)) numbers, type of [0-9], // [0-9] has 10 digits each digital samples 5000/10 = 500 // into its individual divided images 20 and 20 * serialized (transformed into a one-dimensional array) int side = 20; int m = gray.rows / side; int n = gray.cols / side; Mat data, labels; for (int i = 0; i < m; i++) { int p = 0; int offsetRow = i * side; for (int j = 0; j < n; j++) { int offsetCol = j * side; // 截取20*20的小块 Mat tmp; gray(Range(offsetRow, offsetRow + side), Range(offsetCol, offsetCol + side)).copyTo(tmp); char path[50]; char path1[50]; int num = int(i/5); sprintf(path, ".\\train\\%d\\", num); sprintf(path1, "%d.jpg", p); stringstream ss; string filename; ss << path<< path1; ss >> filename; imwrite(filename, tmp); p++; } } }