網上看到的,大多數用python寫的,opencv寫起來很簡單python
#include <iostream> #include <string> #include "cv.h" #include "highgui.h" # include<fstream> using namespace std; char toText(int g) { if (g <= 30) { return '#'; } else if (g > 30 && g <= 60) { return '&'; } else if (g > 60 && g <= 120) { return '$'; } else if (g > 120 && g <= 150) { return '*'; } else if (g > 150 && g <= 180) { return 'o'; } else if (g > 180 && g <= 210) { return '!'; } else if (g > 210 && g <= 240) { return ';'; } else { return ' '; } } int main() { IplImage *img=cvLoadImage("1.png",0); //cvShowImage("img",img); //cvWaitKey(); ofstream writeText; writeText.open("data.txt"); for (int h = 0; h < img->height; h += 12) { string ss; for (int w = 0; w < img->width; w += 6) { int number=cvGetReal2D(img,h,w); char text=toText(number); ss+=text; } ss+='\n'; writeText<<ss; } writeText.close(); return 0; }