Today I heard a lot of comrades write graduation paper repetition rate is too high, Daniel said with the picture instead of the word, I would like to use OPENCV to see if can make, sure enough or can be!!! The main difficulty is that the normal format of the picture background is opaque, you need to use the background transparent PNG format picture on the line.
Main ideas and steps:
1. First configure the FreeType and OpenCV, add the compiled Lib, with the include directory and CvxText.h and CvxText.cpp on the line, reference [1]
2. Talk about the idea, mainly is the OPENCV version of the problem caused by the function of the iplimage, and the function
Set the original image text
Text.puttext (IMAGESRC, MSG, Cvpoint (1, Size_zi), color);
can only accept Iplimage format parameters, so save to PNG, it is more troublesome.
PNG is a 4-channel, installed Bgra to place. Our idea is to follow the original directly to the image overlay text, create a new image with the same size of the text, and then binary, according to the binary template to generate a new PNG text image.
Of course, binary algorithm search an adaptive threshold algorithm on the net the effect is very good: reference [3]
3. Generated a transparent text image, pasted into the paper, estimated that the system of duplicate query is also powerless. After a short time to do some programming interface and character segmentation, you can directly sell the goods.
Of course, the font and size, the upper and lower margins are can be set, after the next step to write inside the program.
Implementation results:
Main code:
AddChinese.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include " CvxText.h "#pragma comment (lib," Freetype255d.lib ") #pragma comment (lib," Opencv_core2410d.lib ") #pragma commen T (Lib, "Opencv_highgui2410d.lib") #pragma comment (lib, "Opencv_imgproc2410d.lib") using namespace std;using namespace CV; #define Row_block 2#define column_block 2//writePng.cpp: The entry point that defines the console application. int Run_test_png (Mat &mat,string image_name) {/* Use the parameters you set to save the picture *///mat Mat (480, 640, CV_8UC4);//createalphamat ( MAT);vector<int> Compression_params;compression_params.push_back (cv_imwrite_png_compression); compression_ Params.push_back (9); In PNG format, the default parameter is 3.try {imwrite (Image_name, Mat, compression_params);} catch (runtime_error& ex) {fprintf (stderr, "Exception converting image to PNG format:%s\n", Ex.what ()); return 1;} fprintf (stdout, "Saved PNG file with Alpha data.\n"); Waitkey (0); return 0;} int colourEd (Mat &template_src, Mat &mat_png, Cvscalar color) {for (int i = 0; i < template_src.rows; ++i) {for (int j = 0 ; J < Template_src.cols; ++J) {vec4b& Bgra = Mat_png.at<vec4b> (i, j);//int temp = template_src.at<uchar> (i,j); if (Template_ Src.at<uchar> (i,j) = = 0) {Bgra[0] = color.val[0]; B Channel Bgra[1] = color.val[1];//g Channel bgra[2] = color.val[2];//r Channel bgra[3] = 255;//alpha Channel all set to transparent full transparent to 0, otherwise 255}else{bgra[3 ] = 0;//alpha Channel is all set to transparent full transparent to 0, otherwise 255}}}return 0;} void Imagebinarization (Iplimage *src) {/* Grayscale image binary, adaptive threshold Threshold*/int i,j,width,height,step,chanel,threshold;/* Size is the image size, SVG is the grayscale histogram mean, VA is the variance */float size,avg,va,maxva,p,a,s;unsigned char *datasrc;float histogram[256];width = src- >width;height = SRC->HEIGHT;DATASRC = (unsigned char *) Src->imagedata;step = src->widthstep/sizeof (char); Chanel = src->nchannels;/* calculate histogram and normalized histogram*/for (i=0; i<256; i++) Histogram[i] = 0;for (i=0; i
Complete Project Download:
http://download.csdn.net/detail/wangyaninglm/8486521
Reference documents:
http://blog.csdn.net/fengbingchun/article/details/8029337
http://www.oschina.net/code/snippet_1447359_36028
http://blog.csdn.net/hustspy1990/article/details/6301592
OpenCV generated by Chinese characters (transparent)----can be against the paper check weight!!!