#include "L2TradeQueueUtil.h"
|
|
#include "Win32Util.h"
|
|
list<ImgArea> L2TradeQueueUtil::splitViewElements(cv::Mat img)
|
{
|
|
list<int*> rowDataList = splitRows(img);
|
if (rowDataList.size() != 2) {
|
for (list<int*>::iterator ele = rowDataList.begin(); ele != rowDataList.end(); ++ele) {
|
free(*ele);
|
}
|
throw string("·Ö¸ôÐгö´í");
|
}
|
|
list<ImgArea> posList;
|
|
//·Ö¸ô³öίÂô¶ÓÁÐʱ¼ä£¬Î¯Âôµ¥¼Û£¬Î¯ÂôÊÖÊý£¬Î¯Âò¶ÓÁÐʱ¼ä£¬Î¯Âòµ¥¼Û£¬Î¯ÂòÊÖÊý
|
int* rowData = *(rowDataList.begin());
|
//ίÂô¶ÓÁÐʱ¼ä
|
list<int*> dataList = splitCols(img, 0, rowData[0], img.cols / 2, rowData[1]);
|
if (dataList.size() > 1)
|
{
|
list<int*>::iterator ele = dataList.begin();
|
++ele;
|
posList.push_back({ (*ele)[0],(*ele)[1],(*ele)[2],(*ele)[3] });
|
}
|
else {
|
posList.push_back({ -1,-1,-1,-1 });
|
}
|
freeData(dataList);
|
//ίÂò¶ÓÁÐʱ¼ä
|
dataList = splitCols(img, img.cols / 2 + 1, rowData[0], img.cols - 1, rowData[1]);
|
if (dataList.size() > 1)
|
{
|
list<int*>::iterator ele = dataList.begin();
|
++ele;
|
posList.push_back({ (*ele)[0],(*ele)[1],(*ele)[2],(*ele)[3] });
|
}
|
else {
|
posList.push_back({ -1,-1,-1,-1 });
|
}
|
|
free(rowData);
|
rowData = *(++rowDataList.begin());
|
//ίÂôµ¥¼Û£¬Î¯ÂôÊÖÊý
|
freeData(dataList);
|
dataList = splitCols(img, 0, rowData[0], img.cols / 2, rowData[1]);
|
if (dataList.size() > 2)
|
{
|
list<int*>::iterator ele = dataList.begin();
|
++ele;
|
posList.push_back({ (*ele)[0],(*ele)[1],(*ele)[2],(*ele)[3] });
|
++ele;
|
posList.push_back({ (*ele)[0],(*ele)[1],(*ele)[2],(*ele)[3] });
|
}
|
else {
|
posList.push_back({ -1,-1,-1,-1 });
|
posList.push_back({ -1,-1,-1,-1 });
|
}
|
freeData(dataList);
|
//ίÂòµ¥¼Û£¬Î¯ÂòÊÖÊý
|
dataList = splitCols(img, img.cols / 2 + 1, rowData[0], img.cols - 1, rowData[1]);
|
if (dataList.size() > 2)
|
{
|
list<int*>::iterator ele = dataList.begin();
|
++ele;
|
posList.push_back({ (*ele)[0],(*ele)[1],(*ele)[2],(*ele)[3] });
|
++ele;
|
posList.push_back({ (*ele)[0],(*ele)[1],(*ele)[2],(*ele)[3] });
|
}
|
else {
|
posList.push_back({-1,-1,-1,-1});
|
posList.push_back({-1,-1,-1,-1});
|
}
|
freeData(dataList);
|
free(rowData);
|
|
if (FALSE) {
|
//±£´æ
|
int index = -1;
|
for (list<ImgArea>::iterator ele = posList.begin(); ele != posList.end(); ++ele) {
|
index++;
|
ImgArea data = *ele;
|
if (data.startx>-1) {
|
//±£´æÊý¾Ý
|
cv::Mat mat = cv::Mat(img, cv::Rect(data.startx, data.starty, data.endx - data.startx + 1, data.endy - data.starty + 1));
|
string path = "C:\\Users\\Administrator\\Desktop\\ocr\\l2_trade\\";
|
path.append(to_string(index)).append(".png");
|
cv::imwrite(path, mat);
|
}
|
}
|
}
|
|
return posList;
|
}
|
|
list<ImgArea> L2TradeQueueUtil::splitBuyQueue(cv::Mat img,int identify)
|
{
|
if (FALSE)
|
{
|
cv::imwrite(string("C:\\Users\\Administrator\\Desktop\\ocr\\trade_queue\\demo\\").append(to_string(identify)).append("_").append(to_string(rand())).append(".png"), img);
|
}
|
list<ImgArea> resultList;
|
//cv::Mat binary;
|
//¶þÖµ»¯£¬·½±ã´¦Àí
|
//threshold(img, binary, _IMG_BINARY_THRESHOLD, 255, cv::THRESH_BINARY);
|
|
|
//·Ö¸ôµÚÒ»ÐÐ
|
int rows = img.rows;
|
int startCol = 0;
|
int endCol = img.cols - 1;
|
list<int*> rowDataList;
|
int full_start = -1;
|
int full_end = -1;
|
for (int r = 1; r < rows; r++) {
|
if (ImgDivider::isRowFull(img, r, startCol, endCol, 1,49,51)) {
|
if (full_start < 0) {
|
full_start = r;
|
}
|
if (r - full_start > 10) {
|
int* pos = (int*)malloc(sizeof(int) * 2);
|
pos[0] = full_start + 1;
|
pos[1] = r - 1;
|
rowDataList.push_back(pos);
|
full_start = r;
|
}
|
else {
|
full_start = r;
|
}
|
}
|
}
|
|
if (rowDataList.size() < 1) {
|
throw string("ÎÞÊý¾Ý");
|
}
|
endCol = img.cols - 1;
|
int index = 0;
|
for (list<int*>::iterator ele = rowDataList.begin(); ele != rowDataList.end(); ++ele) {
|
int* pos = *ele;
|
int startRow = pos[0];
|
int endRow = pos[1];
|
free(pos);
|
//È¥³ýÉÏÏ¿հ×,ÓÒ²àÁô°×10
|
for (int i = startRow; i <= endRow; i++) {
|
if (!ImgDivider::isRowEmpty(img, i, startCol, endCol,1, _IMG_BINARY_THRESHOLD)) {
|
startRow = i;
|
break;
|
}
|
}
|
for (int i = endRow; i >= startRow; i--) {
|
if (!ImgDivider::isRowEmpty(img, i, startCol, endCol,1, _IMG_BINARY_THRESHOLD)) {
|
endRow = i;
|
break;
|
}
|
}
|
|
//if (identify == 4|| identify ==6) {
|
// printf("½øÈëµ÷ÊÔ");
|
//}
|
|
if (endRow - startRow > 5)
|
{
|
|
list<int*> elements = splitCols(img, startCol, startRow, endCol - 1, endRow, 3);
|
|
for (list<int*>::iterator e = elements.begin(); e != elements.end(); ++e) {
|
int* ps = *e;
|
ImgArea area;
|
area.startx = ps[0];
|
area.starty = ps[1];
|
area.endx = ps[2];
|
area.endy = ps[3];
|
resultList.push_back(area);
|
free(ps);
|
if (FALSE)
|
{
|
cv::Mat sm = cv::Mat(img, cv::Rect(area.startx, area.starty, area.endx - area.startx + 1, area.endy - area.starty + 1));
|
cv::imwrite(string("C:\\Users\\Administrator\\Desktop\\ocr\\trade_queue\\").append(to_string(index)).append(".png"), sm);
|
}
|
index++;
|
}
|
}
|
}
|
return resultList;
|
}
|
|
HWND L2TradeQueueUtil::getTradeQueueBuyHWND(HWND tradeQueueHWND)
|
{
|
//ÔÝʱ·ÅÔÚÕâ¶ù
|
HWND temp = NULL;
|
while (TRUE)
|
{
|
temp = FindWindowExA(tradeQueueHWND, temp, "#32770", "ÂòÂô¶ÓÁÐ");
|
if (Win32Util::isWindowShow(temp) || temp <= 0) {
|
break;
|
}
|
}
|
if (temp <= 0) {
|
throw string("δÕÒµ½ÂòÂô¶ÓÁоä±ú");
|
}
|
|
HWND childTemp = NULL;
|
|
|
HWND rightHWND=NULL;
|
int rightPOS=-1;
|
|
while (TRUE)
|
{
|
childTemp = FindWindowExA(temp, childTemp, "#32770", NULL);
|
if (childTemp <= 0) {
|
break;
|
}
|
else {
|
if (Win32Util::isWindowShow(childTemp)) {
|
//»ñÈ¡×îÓÒ²àµÄÄÚÈÝ¿ò¾ä±ú
|
RECT rect;
|
Win32Util::getWindowRect(childTemp, &rect);
|
if (rightPOS == -1) {
|
rightHWND = childTemp;
|
rightPOS = rect.right;
|
}
|
else if (rect.right > rightPOS) {
|
rightHWND = childTemp;
|
rightPOS = rect.right;
|
}
|
}
|
}
|
}
|
|
if (rightHWND <= 0) {
|
throw string("ÂòÈë³É½»¶ÓÁоä±ú²éÕÒʧ°Ü");
|
}
|
rightHWND = FindWindowExA(rightHWND, NULL, "SysListView32", NULL);
|
if (rightHWND <= 0) {
|
throw string("ÂòÈë³É½»¶ÓÁоä±ú²éÕÒʧ°Ü");
|
}
|
return rightHWND;
|
}
|
|
void L2TradeQueueUtil::filterTradeQueueBuyMat(cv::Mat& grayImg)
|
{
|
list<ImgArea> areas;
|
int startR = -1;
|
for (int r = 0; r < grayImg.rows; r++) {
|
int startc = -1;
|
int endc = -1;
|
bool empty = TRUE;
|
for (int c = 0; c < grayImg.cols; c++) {
|
uchar data = grayImg.ptr<uchar>(r)[c];
|
if (data >= 178 && data <= 180) {
|
empty = FALSE;
|
if (startc < 0) {
|
startc = c;
|
endc = c;
|
}
|
endc = c;
|
}
|
else {
|
if (startc > 0 && endc - startc > 10 && startR < 0) {
|
ImgArea area;
|
area.startx = startc;
|
area.endx = endc;
|
area.starty = r;
|
area.endy = -1;
|
areas.push_back(area);
|
startc = -1;
|
endc = -1;
|
}
|
}
|
}
|
if (areas.size() > 0 && startR < 0) {
|
startR = r;
|
}
|
|
if (!empty && areas.size() > 0) {
|
continue;
|
}
|
|
if (startR > 0 && empty) {
|
printf("ÆðʼÐÐ%d, ½áÊøÐУº%d Êý¾ÝÌõÊý£º%d\n", startR, r, areas.size());
|
for (list<ImgArea>::iterator ele = areas.begin(); ele != areas.end(); ++ele) {
|
ImgArea area = *ele;
|
area.endy = r - 1;
|
printf("%d,%d,%d,%d\n", area.startx, area.starty, area.endx, area.endy);
|
for (int r = area.starty; r <= area.endy; r++) {
|
for (int c = area.startx; c <= area.endx; c++) {
|
uchar data = grayImg.ptr<uchar>(r)[c];
|
if (data >= _IMG_BINARY_THRESHOLD) {
|
grayImg.ptr<uchar>(r)[c] = 0;
|
}
|
else {
|
grayImg.ptr<uchar>(r)[c] = 255;
|
}
|
}
|
}
|
}
|
|
printf("------------------------\n");
|
|
startR = -1;
|
areas.clear();
|
}
|
}
|
}
|
list<int*> L2TradeQueueUtil::splitRows(cv::Mat img)
|
{
|
list<int*> rowDataList;
|
|
//ͼÏñ·Ö¸ô
|
//ÏÈ·Ö¸ô
|
int start_row = -1;
|
int end_row = -1;
|
for (int r = 0; r < img.rows; r++) {
|
if (ImgDivider::isRowEmpty(img, r, 1, img.cols/4,1, _IMG_BINARY_THRESHOLD)) {
|
if (end_row - start_row >=10) {
|
//ÓÐÊý¾Ý
|
int* rowRata = (int*)malloc(sizeof(int) * 2);
|
rowRata[0] = start_row;
|
rowRata[1] = end_row;
|
rowDataList.push_back(rowRata);
|
start_row = -1;
|
end_row = -1;
|
if (rowDataList.size() >= 2) {
|
break;
|
}
|
}
|
start_row = r + 1;
|
}
|
else {
|
if (start_row < 0) {
|
start_row = r;
|
}
|
end_row =r;
|
|
}
|
}
|
|
return rowDataList;
|
}
|
|
list<int*> L2TradeQueueUtil::splitCols(cv::Mat img, int startx, int starty, int endx, int endy, int minContentWidth)
|
{
|
if (FALSE)
|
{
|
cv::imwrite("C:\\Users\\Administrator\\Desktop\\ocr\\l2_trade\\test.png", cv::Mat(img, cv::Rect(startx, starty, endx - startx + 1, endy - starty + 1)));
|
}
|
list<int*> splitList;
|
int empty_start = -1;
|
int empty_end = -1;
|
int start_col = -1;
|
int end_col = -1;
|
for (int c = startx; c <= endx; c++) {
|
if (ImgDivider::isColEmpty(img, c, starty, endy,_IMG_BINARY_THRESHOLD)) {
|
if (empty_start < 0) {
|
empty_start = c;
|
}
|
empty_end = c;
|
if (empty_end - empty_start > 10) {
|
if (end_col - start_col >= minContentWidth) {
|
int* itemData = (int*)malloc(sizeof(int) * 4);
|
itemData[0] = start_col;
|
itemData[1] = starty;
|
itemData[2] = empty_start - 1;
|
itemData[3] = endy;
|
splitList.push_back(itemData);
|
start_col = -1;
|
end_col = -1;
|
}
|
}
|
|
}
|
else {
|
if (start_col < 0) {
|
start_col = c;
|
}
|
end_col = c;
|
empty_start = -1;
|
empty_end = -1;
|
}
|
}
|
if (end_col - start_col >= minContentWidth) {
|
int* itemData = (int*)malloc(sizeof(int) * 4);
|
itemData[0] = start_col;
|
itemData[1] = starty;
|
itemData[2] = empty_start - 1;
|
itemData[3] = endy;
|
splitList.push_back(itemData);
|
start_col = -1;
|
end_col = -1;
|
}
|
|
return splitList;
|
}
|
|
void L2TradeQueueUtil::freeData(list<int*> dataList)
|
{
|
if (dataList.size()>0) {
|
|
for (list<int*>::iterator ele = dataList.begin(); ele != dataList.end(); ele++) {
|
free(*ele);
|
}
|
|
}
|
|
}
|