團支書最頭疼的事情莫過于假期收截圖,例如當前的“暑假十課”,總有人久催不交

看這圖片上交的模板,通過改變右上角名字就能做到偷天換日,那么一個班近30,十課一共300張圖片,人工肯定是不現實的,但是OPENCV給我們提供了很好的環境,
方法:先采集RGB值,設定文字顏色閥值,然后將其與背景融合,再自行更換添加,
#include <sys/stat.h>
#include "opencv2/opencv.hpp"
#include "bits/stdc++.h"
#include "CvxText.h"
using namespace std;
using namespace cv;
Mat img;
void out_word();
string get_path(string path0, int num);
string output = "/home/rubo/機械五班";
string path;
string classmate[32] = {
"白一", "蔡清", "奇一", "曾一", "一源", "一語", "陳遠", "一凱", "一帥", "一凡", "陳瑤", "陳一", "大卓", "鄧一",
"董一", "方田", "馮一", "馮祺", "一旗", "付博", "一駿", "甘政", "一尚", "一鵬", "顧天", "黃一", "季姍",
"劉一", "黎一", "一曦", "澤江", "仲鈺",
};
static int ToWchar(char *&src, wchar_t *&dest, const char *locale = "zh_CN.utf8") {
if (src == NULL) {
dest = NULL;
return 0;
}
setlocale(LC_CTYPE, locale);
int w_size = mbstowcs(NULL, src, 0) + 1;
if (w_size == 0) {
dest = NULL;
return -1;
}
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;
}
void create_dir(string path0) {
int isCreate = mkdir(path0.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO);
if (!isCreate)
printf("create path:%s\n", path0.c_str());
else
printf("create path failed! error code : %s \n", path0.c_str());
}
void create_name(string path0, string name) {
string path1 = path0;
path1 = path1 + "/" + name;
create_dir(path1);
}
void create_sum() {
create_dir(output);
for (int i = 0; i < 32; i++) {
create_name(output, classmate[i]);
}
}
int main() {
out_word();
create_sum();
//printf("%d", strlen(classmate[0].c_str()));
for (int i = 0; i < 32; i++) {
for (int j = 1; j <= 10; j++) {
string result_path = get_path(output + "/" + classmate[i], j);
string from_path = "/home/rubo/圖片/暑假十課";
from_path = get_path(from_path, j);
//cout<<from_path<<" "<<result_path<<endl;
img = imread(from_path);
if (!img.data || img.channels() != 3) {
fprintf(stderr, "read image fail\n");
return -1;
}
CvxText text("/home/rubo/下載/chinese/iphone.ttf"); //指定字體
Scalar size1{40, 0.5, 0.1, 0}; // (字體大小, 無效的, 字符間距, 無效的 }
text.setFont(nullptr, &size1, nullptr, 0);
char *str;
int len = classmate[i].length();
str = (char *) malloc((len + 1) * sizeof(char));
classmate[i].copy(str, len, 0);
str[len] = '\0';
wchar_t *w_str;
ToWchar(str, w_str);
if (strlen(classmate[i].c_str()) >= 7) text.putText(img, w_str, Point(417, 172), Scalar(233, 68, 3));//三字
else text.putText(img, w_str, Point(428, 172), Scalar(233, 68, 3));//兩字
imwrite(result_path, img);
}
}
return 0;
}
string get_path(string path0, int num) {
return path0 + "/" + to_string(num) + ".png";;
}
//該方法可能產生誤檢點,但在可容忍的錯范圍內
Mat GetRedComponet(Mat srcImg) {
//如果直接對srcImg處理會改變main()函式中的實參
Mat dstImg = srcImg.clone();
Mat_<Vec3b>::iterator it = dstImg.begin<Vec3b>();
Mat_<Vec3b>::iterator itend = dstImg.end<Vec3b>();
for (; it != itend; it++) {
if ((*it)[0] > 230 && (*it)[1] < 230 && (*it)[2] < 230)//對紅色分量做閾值處理
{
// (*it)[0] = 0;
(*it)[1] = 0;
(*it)[2] = 0;//紅色分量保持不變
} else {
(*it)[0] = 0;
(*it)[1] = 0;
(*it)[2] = 0;
}
}
return dstImg;
}
void Inpainting(Mat oriImg, Mat maskImg) {
Mat grayMaskImg;
Mat element = getStructuringElement(MORPH_RECT, Size(7, 7));
dilate(maskImg, maskImg, element);//膨脹后結果作為修復掩膜
//將彩色圖轉換為單通道灰度圖,最后一個引數為通道數
cvtColor(maskImg, grayMaskImg, COLOR_RGB2GRAY, 1);
//修復影像的掩膜必須為8位單通道影像
Mat inpaintedImage;
inpaint(oriImg, grayMaskImg, inpaintedImage, 3, INPAINT_TELEA);
imwrite(path, inpaintedImage);
}
string get_path(int num) {
path = "/home/rubo/圖片/暑假十課/";
path = path + to_string(num) + ".png";
return path;
}
void out_word() {
Mat srcImg;
for (int i = 1; i <= 10; i++) {
get_path(i);
srcImg = imread(path, 1);
Mat imgComponet = GetRedComponet(srcImg);
Inpainting(srcImg, imgComponet);
}
}
玩歸玩,鬧歸鬧,十課的學習還是必要的
中文漢字庫參考:https://download.csdn.net/download/qq_31261509/10750526
參考:https://blog.csdn.net/zx249388847/article/details/79332179
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/295072.html
標籤:AI
