#include "THSGPDataCapture.h"
|
#include <set>
|
#include "Win32Util.h"
|
|
|
|
cv::Mat THSGPDataCapture::grayImgs(cv::Mat oimg) {
|
cv::Mat grayImg = cv::Mat::zeros(oimg.rows, oimg.cols, CV_8UC1);//ImgUtil::grayImage(oimg);
|
|
uchar* imgData = (uchar*)malloc(sizeof(uchar) * oimg.rows * oimg.cols);
|
if (oimg.channels() == 3)
|
{
|
openCLExcuter->rgb2Gray(oimg.data, oimg.cols, oimg.rows, imgData);
|
}
|
else {
|
openCLExcuter->rgba2Gray(oimg.data, oimg.cols, oimg.rows, imgData);
|
}
|
grayImg.data = imgData;
|
|
return grayImg;
|
}
|
|
|
|
void THSGPDataCapture::init() {
|
openCLExcuter = new OpenCLExcuter();
|
openCLExcuter->init();
|
}
|
|
|
|
|
list<list<GPDataStruct>> THSGPDataCapture::captureGPData(cv::Mat grayImg, SplitType types[], int length_per_num, int startColumn, int endColumn) {
|
|
|
list<int*> rowData = THSActionUtil::splitPlateRowArea(grayImg);
|
int count = 0;
|
int* rowIndex = (int*)malloc(sizeof(int) * 4 * rowData.size());
|
for (std::list<int*>::iterator ele = rowData.begin();ele != rowData.end();ele++) {
|
|
string path = "C:\\Users\\Administrator\\Desktop\\ocr\\limit_up\\";
|
path.append(to_string(count)).append(".jpg");
|
//cv::imwrite(path, cv::Mat(grayImg, cv::Rect((*ele)[0], (*ele)[1], (*ele)[2] - (*ele)[0] + 1, (*ele)[3] - (*ele)[1] + 1)));
|
rowIndex[4 * count + 0] = (*ele)[0];
|
rowIndex[4 * count + 1] = (*ele)[1];
|
rowIndex[4 * count + 2] = (*ele)[2];
|
rowIndex[4 * count + 3] = (*ele)[3];
|
free(*ele);
|
count++;
|
}
|
//ÿÐеÄÔªËØ¸öÊý
|
int ele_count_per_line = sizeof(types) / sizeof(types[0]);
|
int* splitResult = (int*)malloc(sizeof(int) * 4 * ele_count_per_line * rowData.size());
|
int line_number_count = ele_count_per_line * length_per_num;
|
|
openCLExcuter->splitPlateContentRowData(grayImg.data, grayImg.cols, grayImg.rows, rowIndex, rowData.size(), ele_count_per_line, startColumn, endColumn, splitResult);
|
|
//·Ö¸îÊý×Ö
|
unsigned char* zeroData = (unsigned char*)malloc(sizeof(unsigned char) * _NUMBER_GP_CODE_WIDTH * _NUMBER_GP_CODE_HEIGHT);
|
for (int r = 0;r < _NUMBER_GP_CODE_HEIGHT;r++) {
|
for (int c = 0;c < _NUMBER_GP_CODE_WIDTH;c++)
|
{
|
zeroData[r * _NUMBER_GP_CODE_WIDTH + c] = ImgUtil::NUMS_GP_CODE[0].data.ptr<uchar>(r)[c];
|
}
|
}
|
unsigned char* totalNumberData = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_GP_CODE_HEIGHT * rowData.size()) * _NUMBER_GP_CODE_WIDTH * 10 * line_number_count);
|
|
uchar* types_char = (uchar*)malloc(sizeof(uchar) * ele_count_per_line);
|
for (int i = 0;i < ele_count_per_line;i++) {
|
types_char[i] = types[i];
|
}
|
|
UcharDataInfo typesData = UcharDataInfo();
|
typesData.length = 2;
|
typesData.data = types_char;
|
|
|
|
|
openCLExcuter->splitPlateNum(grayImg, IntDataInfo({ splitResult,(int)(ele_count_per_line * rowData.size()) }), UcharDataInfo({ totalNumberData, -1 }), typesData, zeroData, _NUMBER_GP_CODE_WIDTH, _NUMBER_GP_CODE_HEIGHT, ele_count_per_line, length_per_num);
|
free(types_char);
|
|
|
//ʶ±ðÊý×Ö
|
uchar* templateNums = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_GP_CODE_HEIGHT * rowData.size()) * _NUMBER_GP_CODE_WIDTH * 10 * line_number_count);
|
openCLExcuter->createNumberTemplates(rowData.size(), _NUMBER_GP_CODE_WIDTH, _NUMBER_GP_CODE_HEIGHT, line_number_count, ImgUtil::numsOneLevel_gpcode, templateNums);
|
uchar** numberResult = openCLExcuter->recognition_numbers(totalNumberData, templateNums, rowData.size() * _NUMBER_GP_CODE_HEIGHT, _NUMBER_GP_CODE_WIDTH * 10 * line_number_count, _NUMBER_GP_CODE_WIDTH, _NUMBER_GP_CODE_HEIGHT, line_number_count);
|
|
list<list<GPDataStruct>> dataList;
|
//½âÎöÊý¾Ý£¬GPDataStruct
|
for (int i = 0;i < rowData.size();i++) {
|
|
list<GPDataStruct> lineData;
|
|
for (int p = 0;p < ele_count_per_line;p++) {
|
GPDataStruct industryData = GPDataStruct();
|
switch (types[p]) {
|
case NUM_TYPE_TIME: {
|
//ʱ¼ä
|
string time = "";
|
for (int j = length_per_num - 6;j < length_per_num;j++)
|
{
|
if (j - (length_per_num - 6) == 2 || j - (length_per_num - 6) == 4)
|
time.append(":");
|
time.append(to_string(numberResult[i][length_per_num * p + j]));
|
}
|
industryData.type = NUM_TYPE_TIME;
|
industryData.content = time;
|
}break;
|
case NUM_TYPE_MONEY: {
|
string money = "";
|
for (int j = 0;j < length_per_num;j++)
|
{
|
if (j == length_per_num - 2) {
|
money.append(".");
|
}
|
money.append(to_string(numberResult[i][length_per_num * p + j]));
|
}
|
|
char* priceCode;
|
priceCode = new char[100];
|
sprintf_s(priceCode, 100, "%.2lf", stod(money));
|
std::string _str(priceCode);
|
delete[] priceCode;
|
money = _str;
|
|
industryData.type = NUM_TYPE_MONEY;
|
industryData.content = money;
|
}break;
|
case NUM_TYPE_PRICE:
|
{
|
string price = "";
|
for (int j = 0;j < length_per_num;j++)
|
{
|
if (j == length_per_num - 2) {
|
price.append(".");
|
}
|
price.append(to_string(numberResult[i][length_per_num * p + j]));
|
}
|
|
char* priceCode;
|
priceCode = new char[100];
|
sprintf_s(priceCode, 100, "%.2lf", stod(price));
|
std::string _str(priceCode);
|
delete[] priceCode;
|
price = _str;
|
|
industryData.type = NUM_TYPE_PRICE;
|
industryData.content = price;
|
}
|
break;
|
case NUM_TYPE_CODE: {
|
//¹ÉƱ´úÂë
|
string code = "";
|
for (int j = length_per_num - 6;j < length_per_num;j++)
|
{
|
code.append(to_string(numberResult[i][length_per_num * p + j]));
|
}
|
industryData.type = NUM_TYPE_CODE;
|
industryData.content = code;
|
}
|
break;
|
|
case NUM_TYPE_NUM:
|
//Êý×Ö
|
string num = "";
|
for (int j = 0;j < length_per_num;j++)
|
{
|
num.append(to_string(numberResult[i][length_per_num * p + j]));
|
}
|
num = to_string(stoi(num));
|
|
industryData.type = NUM_TYPE_NUM;
|
industryData.content = num;
|
break;
|
}
|
lineData.push_back(industryData);
|
}
|
free(numberResult[i]);
|
dataList.push_back(lineData);
|
}
|
free(numberResult);
|
|
|
|
list<int> moneyIndexs;
|
for (int p = 0;p < ele_count_per_line;p++) {
|
if (types[p] == NUM_TYPE_MONEY) {
|
moneyIndexs.push_back(p);
|
}
|
}
|
|
if (moneyIndexs.size() > 0)
|
{
|
|
int* unitData = (int*)malloc(sizeof(int) * 4 * moneyIndexs.size() * rowData.size());
|
|
|
|
//ʶ±ð½ð¶îµ¥Î»
|
int line = 0;
|
for (list<list<GPDataStruct>>::iterator ele = dataList.begin();ele != dataList.end();ele++) {
|
int index = 0;
|
for (list<int>::iterator e = moneyIndexs.begin();e != moneyIndexs.end();e++) {
|
int start_index = (line * moneyIndexs.size() + index) * 4;
|
unitData[start_index + 0] = splitResult[line * 4 * ele_count_per_line + *e * 4 + 0];
|
unitData[start_index + 1] = splitResult[line * 4 * ele_count_per_line + *e * 4 + 1];
|
unitData[start_index + 2] = splitResult[line * 4 * ele_count_per_line + *e * 4 + 2];
|
unitData[start_index + 3] = splitResult[line * 4 * ele_count_per_line + *e * 4 + 3];
|
string path = "C:\\Users\\Administrator\\Desktop\\ocr\\limit_up\\";
|
path.append(to_string(line)).append("_1.jpg");
|
//cv::imwrite(path, cv::Mat(grayImg,cv::Rect(unitData[start_index + 0], unitData[start_index + 1], unitData[start_index + 2]- unitData[start_index + 0]+1, unitData[start_index + 3]- unitData[start_index + 1]+1)));
|
index++;
|
}
|
line++;
|
}
|
int* unitResult = (int*)malloc(sizeof(int) * moneyIndexs.size() * dataList.size());
|
openCLExcuter->recognitionPlateMoneyUnit(grayImg.data, grayImg.cols, grayImg.rows, unitData, moneyIndexs.size(), dataList.size(), unitResult);
|
int index = 0;
|
for (list<list<GPDataStruct>>::iterator ele = dataList.begin();ele != dataList.end();ele++) {
|
|
int data_index = index * moneyIndexs.size();
|
|
int cIndex = 0;
|
for (list<GPDataStruct>::iterator e = (*ele).begin();e != (*ele).end();e++) {
|
if ((*e).type == NUM_TYPE_MONEY) {
|
switch (unitResult[data_index + cIndex])
|
{
|
case 0:
|
(*e).unit = MONEY_UNIT_Y;
|
break;
|
case 1:
|
(*e).unit = MONEY_UNIT_W;
|
break;
|
default:
|
break;
|
}
|
|
cIndex++;
|
}
|
}
|
index++;
|
}
|
free(unitData);
|
free(unitResult);
|
}
|
|
free(rowIndex);
|
free(splitResult);
|
free(zeroData);
|
free(totalNumberData);
|
free(templateNums);
|
free(grayImg.data);
|
grayImg.release();
|
|
|
return dataList;
|
}
|
|
bool THSGPDataCapture::isInited() {
|
return inited;
|
}
|