#include <iostream> #include <ctype.h> #include <ft2build.h> #include FT_FREETYPE_H #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <sys/timeb.h> #include <time.h> #include <fstream> #include <stdlib.h> #include <string> #include <vector> #include <unistd.h> using namespace std; // //漢字 // int myputText(cv::Mat img, const char *text, cv::Point pos, cv::Scalar color); // void myputWChar(cv::Mat img, wchar_t wc, cv::Point &pos, cv::Scalar color); /*時間打印*/ char* log_Time(void) { struct tm *ptm; struct timeb stTimeb; static char szTime[19]; ftime(&stTimeb); ptm = localtime(&stTimeb.time); sprintf(szTime, "%02d-%02d %02d:%02d:%02d.%03d",ptm->tm_mon+1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec, stTimeb.millitm); szTime[18] = 0; return szTime; } class CvxText { public: CvxText(const char* freeType); virtual ~CvxText(); void getFont(int* type, cv::Scalar* size=NULL, bool* underline=NULL, float* diaphaneity=NULL); void setFont(int* type, cv::Scalar* size=NULL, bool* underline=NULL, float* diaphaneity=NULL); void restoreFont(); int putText(cv::Mat& img, char* text, cv::Point pos); int putText(cv::Mat& img, const wchar_t* text, cv::Point pos); int putText(cv::Mat& img, const char* text, cv::Point pos, cv::Scalar color); int putText(cv::Mat& img, const wchar_t* text, cv::Point pos, cv::Scalar color); private: CvxText& operator=(const CvxText&); void putWChar(cv::Mat& img, wchar_t wc, cv::Point& pos, cv::Scalar color); FT_Library m_library; FT_Face m_face; int m_fontType; cv::Scalar m_fontSize; bool m_fontUnderline; float m_fontDiaphaneity; }; // 打開字庫 CvxText::CvxText(const char* freeType) { assert(freeType != NULL); // 打開字庫文件, 創建一個字體 if(FT_Init_FreeType(&m_library)) throw; if(FT_New_Face(m_library, freeType, 0, &m_face)) throw; // 設置字體輸出參數 restoreFont(); // 設置C語言的字符集環境 setlocale(LC_ALL, ""); } // 釋放FreeType資源 CvxText::~CvxText() { FT_Done_Face(m_face); FT_Done_FreeType(m_library); } void CvxText::getFont(int* type, cv::Scalar* size, bool* underline, float* diaphaneity) { if (type) *type = m_fontType; if (size) *size = m_fontSize; if (underline) *underline = m_fontUnderline; if (diaphaneity) *diaphaneity = m_fontDiaphaneity; } void CvxText::setFont(int* type, cv::Scalar* size, bool* underline, float* diaphaneity) { // 參數合法性檢查 if (type) { if(type >= 0) m_fontType = *type; } if (size) { m_fontSize.val[0] = std::fabs(size->val[0]); m_fontSize.val[1] = std::fabs(size->val[1]); m_fontSize.val[2] = std::fabs(size->val[2]); m_fontSize.val[3] = std::fabs(size->val[3]); } if (underline) { m_fontUnderline = *underline; } if (diaphaneity) { m_fontDiaphaneity = *diaphaneity; } FT_Set_Pixel_Sizes(m_face, (int)m_fontSize.val[0], 0); } // 恢復原始的字體設置 void CvxText::restoreFont() { m_fontType = 0; // 字體類型(不支持) m_fontSize.val[0] = 20; // 字體大小 m_fontSize.val[1] = 0.5; // 空白字符大小比例 m_fontSize.val[2] = 0.1; // 間隔大小比例 m_fontSize.val[3] = 0; // 旋轉角度(不支持) m_fontUnderline = false; // 下畫線(不支持) m_fontDiaphaneity = 1.0; // 色彩比例(可產生透明效果) // 設置字符大小 FT_Set_Pixel_Sizes(m_face, (int)m_fontSize.val[0], 0); } // 輸出函數(顏色默認為白色) int CvxText::putText(cv::Mat& img, char* text, cv::Point pos) { return putText(img, text, pos, CV_RGB(255, 255, 255)); } int CvxText::putText(cv::Mat& img, const wchar_t* text, cv::Point pos) { return putText(img, text, pos, CV_RGB(255,255,255)); } int CvxText::putText(cv::Mat& img, const char* text, cv::Point pos, cv::Scalar color) { if (img.data == NULL) return -1; if (text == NULL) return -1; int i; for (i = 0; text[i] != '\0'; ++i) { wchar_t wc = text[i]; // 解析雙字節符號 if(!isascii(wc)) mbtowc(&wc, &text[i++], 2); // 輸出當前的字符 putWChar(img, wc, pos, color); } return i; } int CvxText::putText(cv::Mat& img, const wchar_t* text, cv::Point pos, cv::Scalar color) { if (img.data == NULL) return -1; if (text == NULL) return -1; int i; for(i = 0; text[i] != '\0'; ++i) { // 輸出當前的字符 putWChar(img, text[i], pos, color); } return i; } // 輸出當前字符, 更新m_pos位置 void CvxText::putWChar(cv::Mat& img, wchar_t wc, cv::Point& pos, cv::Scalar color) { // 根據unicode生成字體的二值位圖 FT_UInt glyph_index = FT_Get_Char_Index(m_face, wc); FT_Load_Glyph(m_face, glyph_index, FT_LOAD_DEFAULT); FT_Render_Glyph(m_face->glyph, FT_RENDER_MODE_MONO); FT_GlyphSlot slot = m_face->glyph; // 行列數 int rows = slot->bitmap.rows; int cols = slot->bitmap.width; for (int i = 0; i < rows; ++i) { for(int j = 0; j < cols; ++j) { int off = i * slot->bitmap.pitch + j/8; if (slot->bitmap.buffer[off] & (0xC0 >> (j%8))) { int r = pos.y - (rows-1-i); int c = pos.x + j; if(r >= 0 && r < img.rows && c >= 0 && c < img.cols) { cv::Vec3b pixel = img.at<cv::Vec3b>(cv::Point(c, r)); cv::Scalar scalar = cv::Scalar(pixel.val[0], pixel.val[1], pixel.val[2]); // 進行色彩融合 float p = m_fontDiaphaneity; for (int k = 0; k < 4; ++k) { scalar.val[k] = scalar.val[k]*(1-p) + color.val[k]*p; } img.at<cv::Vec3b>(cv::Point(c, r))[0] = (unsigned char)(scalar.val[0]); img.at<cv::Vec3b>(cv::Point(c, r))[1] = (unsigned char)(scalar.val[1]); img.at<cv::Vec3b>(cv::Point(c, r))[2] = (unsigned char)(scalar.val[2]); } } } } // 修改下一個字的輸出位置 double space = m_fontSize.val[0]*m_fontSize.val[1]; double sep = m_fontSize.val[0]*m_fontSize.val[2]; pos.x += (int)((cols? cols: space) + sep); } static int ToWchar(char* &src, wchar_t* &dest, const char *locale = "zh_CN.utf8") { if (src == NULL) { dest = NULL; return 0; } // 根據環境變量設置locale setlocale(LC_CTYPE, locale); // 得到轉化為需要的寬字符大小 int w_size = mbstowcs(NULL, src, 0) + 1; // w_size = 0 說明mbstowcs返回值為-1。即在運行過程中遇到了非法字符(很有可能使locale // 沒有設置正確) if (w_size == 0) { dest = NULL; return -1; } //wcout << "w_size" << w_size << endl; dest = new wchar_t[w_size]; if (!dest) { return -1; } int ret = mbstowcs(dest, src, strlen(src)+1); if (ret <= 0) { return -1; } return 0; } int wmain() { //printf("打開圖之前[%s]\n", log_Time()); // create image cv::Mat image; //std::cout << "size (empty picture): " << image.size().height << " , "<< image.size().width << std::endl; CvxText text("./simhei.ttf"); //指定字體 cv::Scalar size1{ 40, 0.5, 0.1, 0 }; // (字體大小, 無效的, 字符間距, 無效的 } text.setFont(nullptr, &size1, nullptr, 0); vector<int> compression_params; compression_params.push_back(CV_IMWRITE_JPEG_QUALITY); //選擇jpeg compression_params.push_back(60); //在這個填入你要的圖片質量 printf("start[%s]\n", log_Time()); for (int i=0;i<10000;i++) { // open image cv::Mat image; image = cv::imread("20211103182311_4.186817.jpg"); if (!image.data) { printf("=========================\n"); return 0; } //printf("打開圖之后[%s]\n", log_Time()); /*圖片大小*/ //std::cout << "size (after reading): " << image.size().height << " , "<< image.size().width << std::endl; //畫框 cv::rectangle( image,cv::Point( 155,693),cv::Point( 349,1073),cv::Scalar(0,0,255),3,1); //字母文字 //cv::putText(image, "HELLO", cv::Point(160, 1065), CV_FONT_HERSHEY_COMPLEX, 1, cv::Scalar(0,0, 255), 2, 1); char* str = (char *)"WZH"; wchar_t *w_str; ToWchar(str,w_str); text.putText(image, w_str, cv::Point(160, 1065), cv::Scalar(0, 0, 255)); //cv::waitKey(0); //printf("繪制圖之后[%s]\n", log_Time()); cv::imwrite("output.jpg", image,compression_params); if (i%1000 == 0) printf("第%d張圖保存[%s]\n", i,log_Time()); } printf("end[%s]\n", log_Time()); return 0; } int main() { //printf("打開圖之前[%s]\n", log_Time()); // create image cv::Mat image; //std::cout << "size (empty picture): " << image.size().height << " , "<< image.size().width << std::endl; CvxText text("./simhei.ttf"); //指定字體 cv::Scalar size1{ 40, 0.5, 0.1, 0 }; // (字體大小, 無效的, 字符間距, 無效的 } text.setFont(nullptr, &size1, nullptr, 0); vector<int> compression_params; compression_params.push_back(CV_IMWRITE_JPEG_QUALITY); //選擇jpeg compression_params.push_back(60); //在這個填入你要的圖片質量 printf("start[%s]\n", log_Time()); pid_t pid = fork(); if (pid < 0){ printf("error\n"); return 1; } else if (pid == 0){ for (int i=1;i<=5000;i++) { // open image cv::Mat image; image = cv::imread("20211103182311_4.186817.jpg"); if (!image.data) { return 0; } cv::rectangle( image,cv::Point( 155,693),cv::Point( 349,1073),cv::Scalar(0,0,255),3,1); char* str = (char *)"WZH"; wchar_t *w_str; ToWchar(str,w_str); text.putText(image, w_str, cv::Point(160, 1065), cv::Scalar(0, 0, 255)); cv::imwrite("output1.jpg", image,compression_params); if (i%1000 == 0 || i==5000) printf("[son] 第%d張圖保存[%s]\n", i,log_Time()); } } else { for (int i=1;i<=5000;i++) { // open image cv::Mat image; image = cv::imread("20211103182311_4.186818.jpg"); if (!image.data) { return 0; } cv::rectangle( image,cv::Point( 155,693),cv::Point( 349,1073),cv::Scalar(0,0,255),3,1); char* str = (char *)"WZH"; wchar_t *w_str; ToWchar(str,w_str); text.putText(image, w_str, cv::Point(160, 1065), cv::Scalar(0, 0, 255)); cv::imwrite("output2.jpg", image,compression_params); if (i%1000 == 0 || i==5000) printf("[fat] 第%d張圖保存[%s]\n", i,log_Time()); } } printf("end[%s]\n", log_Time()); return 0; }
需要指定字體文件的路徑,修改圖片打開位置和文件保存位置。wmain為單進程程序,main為雙進程邏輯,時間可以減半。