这是一个简单有趣的小程序(其实笔者很早就像实现一下)基于opencv的字符表情制作,这里主要的思路是:
- 将图片进行一定的调整大小,由于用txt中单个字符比较大大约一行只能打大约一百169个字符,所以图像的列数不能超过169(为了更好地感受到视觉效果,建议在添加图片之前将图片进行裁剪);
- 将图像进行转换由RGB的彩图转换为GRAY的灰度图;
- 将灰度图用threshold函数进行二值转换;
- 创建并打开一个.txt文件
- 对二值图片进行遍历当二值图片中的像素为空是”“,在文件中写入空格,否则写入”#“(当然也可以是别的字符);
- 关闭txt
其中threshold共有需5个变量分别为原图像,转换后保存的图像,过滤掉最大以及最小阈值的值,以及阈值类型:CV_THRESH_BINARY 和 CV_THRESH_BINARY_INV
#include<opencv2\highgui\highgui.hpp>
#include<opencv.hpp>
#include<iostream>
#include<cv.h>
#include<stdio.h>
#include<stdlib.h>
using namespace cv;
using namespace std;
int main()
{
Mat src;
src = imread("1.jpg");
Mat temp = Mat::zeros(100, 100, CV_8UC3);
resize(src, temp, temp.size());
Mat dst;
cvtColor(temp, dst, CV_RGB2GRAY);
Mat two;
threshold(dst, two, 100, 255, CV_THRESH_BINARY);
imshow("two", two);
int u = two.rows;
int v = two.cols;
FILE* fp;
fp=fopen("1.txt", "w");
for (int i = 0; i < two.rows; i++)
{
for (int j = 0; j < two.cols; j++)
{
if (two.at<char>(i, j) == -1)
fprintf(fp, " ");
else
fprintf(fp, "#");
}
fprintf(fp, "\n");
}
fclose(fp);
waitKey();
}
下面是测试的图片及效果:
###
########
# #######
######
## ###### # ######
# #### ######
# # #####
# ## ###
# ####
# # ## ##
# ## # ##
# # # ## # #
##### ### ##
####### # ##
######### ##
######## # #
####### ###
###### ###
##### ##
### # # #
# # ### # #
# # ## # # #
# ### # # ## # #
# # # ###### # ## # #
# # #### # ## #
# # ##### # #
# # ### ### #
# ## ## #
##### # ## # #
###### #### ### # #
####### ######### # # #
######## #### #### ###### # # #
######## ## ### ####### # #
######## # # ####### # #
####### # ####### #
###### # # ######## #
# #### # # ####### #
# # # # ####### #
# # # ######## #
# # # # #### ##
# ## #
# # # #
# # #
#
#
#
#
# #
# #
# # #
# #
# ##
# #####
# ####
# ####
# ###
# ###
# ##
# #
#
# #
# #
# #
#
# #
# #
#
#
# #
# #
# #
#
# #
# #
# #
# #
# #
# # #
# ### # #
# ## ## #
# ## #
### ## #
## #
# # # ######## # # ##### ############## ## ### ######### ################ #################### ################### ############## ###################### ##################### ###################### ######################## ######################## ########################## ########################## # ######################### ########################## ########################## ########################### ############################ ########################### ############################ ############################# ############################## ############################# ############################# ############################# ############################## ############################# ############################## ############################## ############################ ####### ##################### ############################## ######## #################### ############################## ####### ################## ############################## ####### ########### #### ################### ########## ####### ######## #### ############################## ####### #### #### ######## ####### # # ###### ####### ### ####### # ###### ########### ######## ### ########## # #### # ## #### ####### # ### ####### ## # #### #### #### ####### # ### # ### ## #### ## ### #### ###### # # ## ### # # # # # ## # #### # ## ### ## ### ### ### # ## # # # ### # ## # ## ### # ## ### # # ## ## # ### ## # # # ### # # ## # # ## # ### ## # # # # # # # ## # # ## # # # # # ## # # # # ## ### # # # # # #### #### # ## # # ## ### ###### # ## ## ###### # # ##### ### ######## # # #### #### ######### # # ### ## ###### ########## ## ####### ####### # ########### # # ########## ###### ### ############ ## ### ## ############ ##### ### # ############# #### # #### ####### ######### ## ############## ##### ###################### ### # ########### ##### ######### # ##################### #### # ### ############################ # ########### ####### ### # ### ################ ###### ### # #################### #### #### ####################### ### # # ################### ### # #### ####################### ### # # #### ################### # #### ####################### ### # # ## # ################## #### ########################## ### # ############### #### ########################### ## ################# ### ####### ############### ## ### # ################ # # ### ##### ############ # # ## ############# ### ## # ### ### ########### # ### #### #### ###### ##### ### #### #### ############ ### #################### ####### ### # #### ###### ########## ##### # ###### ####################### #### ######## ########## #### ## ####### ###################### #### ################### #### # ### ######################### ### ################## ######### # ### ######################### ################################# # ######################## ############################### # ## ############ ######## ################################ ## ## ###### ############# ################################ #### ## ###### ############# ################################ #### ### ################# ## ############################## # ###### ### #################### ############################## # ###### ##################### ### ########################### # ###################### ######### ########################## # # ###################### ######## ######################### # # ################# ### ####### ######################## # ####### ##### #### ##### ######################### # # ########## ## ############ ############### ######### # # ############ ####### #### ## # # # # ######### ##### # ## # ##### ############# ### ##### ####### # #########