opencv 把图片转化成 字符画

网上看到的,大多数用python写的,opencv写起来很简单

#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;
}

  

 

posted @ 2017-08-15 15:11  brother_sharp  阅读(1028)  评论(0编辑  收藏  举报