From 125db633619a0b4c7bd1d498ea2bf1cefa4f73d3 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期四, 09 二月 2023 18:54:13 +0800 Subject: [PATCH] '远程OCR' --- ConsoleApplication/L2TradeQueueUtil.cpp | 238 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 231 insertions(+), 7 deletions(-) diff --git a/ConsoleApplication/L2TradeQueueUtil.cpp b/ConsoleApplication/L2TradeQueueUtil.cpp index 232f62b..91f7c8d 100644 --- a/ConsoleApplication/L2TradeQueueUtil.cpp +++ b/ConsoleApplication/L2TradeQueueUtil.cpp @@ -1,6 +1,8 @@ #include "L2TradeQueueUtil.h" -list<ImgArea> L2TradeQueueUtil::splitElements(cv::Mat img) +#include "Win32Util.h" + +list<ImgArea> L2TradeQueueUtil::splitViewElements(cv::Mat img) { list<int*> rowDataList = splitRows(img); @@ -74,7 +76,7 @@ freeData(dataList); free(rowData); - if (false) { + if (FALSE) { //保存 int index = -1; for (list<ImgArea>::iterator ele = posList.begin(); ele != posList.end(); ++ele) { @@ -93,6 +95,216 @@ 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; @@ -129,23 +341,25 @@ return rowDataList; } -list<int*> L2TradeQueueUtil::splitCols(cv::Mat img, int startx, int starty, int endx, int endy) +list<int*> L2TradeQueueUtil::splitCols(cv::Mat img, int startx, int starty, int endx, int endy, int minContentWidth) { - cv::imwrite("C:\\Users\\Administrator\\Desktop\\ocr\\l2_trade\\test.png", cv::Mat(img, cv::Rect(startx, starty, endx - startx, endy - starty))); - + 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)) { + 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 >= 6) { + if (end_col - start_col >= minContentWidth) { int* itemData = (int*)malloc(sizeof(int) * 4); itemData[0] = start_col; itemData[1] = starty; @@ -167,6 +381,16 @@ 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; } -- Gitblit v1.8.0