#pragma once
|
#include <list>
|
#include <iostream>
|
#include <math.h>
|
#include <opencv2/opencv.hpp>
|
#include <opencv2/highgui.hpp>
|
#include <opencv2/core.hpp>
|
#include <opencv2/imgcodecs.hpp>
|
#include <io.h>
|
|
using namespace std;
|
|
enum OperateType {
|
//Âò
|
OPERATE_BUY,
|
//Âò³·
|
OPERATE_BUY_CANCEL,
|
//Âô
|
OPERATE_SELL,
|
//Âô³·
|
OPERATE_SELL_CANCEL,
|
//´íÎó
|
OPERATE_OPERATE_ERROR,
|
};
|
|
class NumberData {
|
|
public:
|
NumberData() {
|
};
|
cv::Mat data;
|
std::string num;
|
|
};
|
|
class RecognitionUtil
|
{
|
private:
|
|
#ifndef _NUMBER_DATA_DEFAINE_
|
#define _NUMBER_DATA_DEFAINE_
|
static std::list<NumberData> numSimples;
|
static std::list<NumberData> timeNumSimples;
|
#endif
|
|
public:
|
static void init() {
|
|
//½«Êý×ÖÑù±¾ÔØÈëÄÚ´æÖÐ
|
numSimples.clear();
|
for (int i = 0;i < 10;i++) {
|
std::string sdir = "C:\\Users\\Administrator\\Desktop\\ocr\\number\\";
|
if (_access(sdir.c_str(), 0)) {
|
sdir = "sample\\number\\";
|
}
|
|
std::string path = sdir.append(std::to_string(i)).append(".jpg");
|
NumberData data = NumberData();
|
data.data = cv::imread(path.c_str(), cv::IMREAD_GRAYSCALE);
|
data.num = std::to_string(i);
|
numSimples.push_back(data);
|
}
|
//½«Ê±¼äÑù±¾ÔØÈëÄÚ´æÖÐ
|
for (int i = 0;i < 60;i++) {
|
std::string sdir = "C:\\Users\\Administrator\\Desktop\\ocr\\time\\";
|
if (_access(sdir.c_str(), 0)) {
|
sdir = "sample\\time\\";
|
}
|
std::string path = sdir;
|
std::string num;
|
if (i < 10) {
|
num = std::to_string(0).append(std::to_string(i));
|
}
|
else {
|
num = std::to_string(i);
|
}
|
|
path = path.append(num).append(".jpg");
|
|
|
NumberData data = NumberData();
|
data.data = cv::imread(path.c_str(), cv::IMREAD_GRAYSCALE);
|
data.num = num;
|
timeNumSimples.push_back(data);
|
}
|
}
|
//»ñȡʱ¼ä
|
static std::string getTime(cv::Mat img) {
|
NumberData data = knn(img, timeNumSimples);
|
return data.num;
|
}
|
//»ñÈ¡Êý×Ö
|
static std::string getNumber(cv::Mat img) {
|
NumberData data = knn(img, numSimples);
|
return data.num;
|
}
|
|
static NumberData knn(cv::Mat target, std::list<NumberData> simples) {
|
//imshow("¼ì²âÄ¿±ê", target);
|
std::list<long> knnValue;
|
std::list<NumberData>::iterator ele;
|
for (ele = simples.begin(); ele != simples.end();ele++) {
|
int cols = (*ele).data.cols;
|
int rows = (*ele).data.rows;
|
if (cols != target.cols || rows != target.rows) {
|
continue;
|
}
|
|
int count = 0;
|
for (int i = 0;i < rows;i++) {
|
for (int j = 0; j < cols; j++)
|
{
|
uchar svalue = (*ele).data.ptr<uchar>(i)[j];
|
uchar tvalue = target.ptr<uchar>(i)[j];
|
//50ÊÇÈ¥³ýÔëÉù
|
svalue = svalue > 50 ? 1 : 0;
|
tvalue = tvalue > 50 ? 1 : 0;
|
int result = abs(svalue - tvalue);
|
count += result;
|
}
|
}
|
|
if (count == 0) {
|
return (*ele);
|
}
|
else {
|
knnValue.push_back(count);
|
}
|
}
|
|
//ÐèÒªÅÅÐò
|
knnValue.sort();
|
ele = simples.begin();
|
return (*ele);
|
}
|
|
};
|