From c108e5ba42168841311b74034d89c31556d628c4 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期二, 12 七月 2022 17:24:10 +0800
Subject: [PATCH] '完善'

---
 ConsoleApplication/THSActionUtil.cpp |  288 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 265 insertions(+), 23 deletions(-)

diff --git a/ConsoleApplication/THSActionUtil.cpp b/ConsoleApplication/THSActionUtil.cpp
index 6f01774..720c295 100644
--- a/ConsoleApplication/THSActionUtil.cpp
+++ b/ConsoleApplication/THSActionUtil.cpp
@@ -44,13 +44,13 @@
 }
 
 //获取副屏2
-HWND getThirdWindow() {
+HWND THSActionUtil::getThirdWindow() {
 	list<HWND> wlist = Win32Util::searchWindow("同花顺(");
 	list<HWND>::iterator ele;
 	for (ele = wlist.begin();ele != wlist.end();ele++) {
 		HWND hwnd = *ele;
 		string str = Win32Util::getWindowName(hwnd);
-		if (isSecondScreen(str))
+		if (isThirdScreen(str))
 		{
 			return hwnd;
 		}
@@ -235,7 +235,7 @@
 
 	std::list<string> addList;
 	std::list<GPCodeArea> delList;
-	std::list<GPCodeArea> resultList = recognitionGPCodeNum(img, areaList,recognitionManager);
+	std::list<GPCodeArea> resultList = recognitionGPCodeNum(img, areaList, recognitionManager);
 
 	for (std::list<GPCodeArea>::reverse_iterator ele = resultList.rbegin();ele != resultList.rend();++ele) {
 
@@ -295,7 +295,7 @@
 	}
 	img = ImgUtil::grayImage(oimg);
 	areaList = recognitionGPArea(img);
-	resultList = recognitionGPCodeNum(img, areaList,recognitionManager);
+	resultList = recognitionGPCodeNum(img, areaList, recognitionManager);
 	for (std::list<GPCodeArea>::iterator ele = resultList.begin();ele != resultList.end();++ele) {
 		bool contains = false;
 		std::list<string>::iterator e;
@@ -363,9 +363,8 @@
 	}
 }
 
-list<GPCodeArea> splitGPCodeArea(cv::Mat img, int start_row, int start_col, int end_row, int end_col) {
 
-	list<GPCodeArea> resultList;
+std::list<int*> THSActionUtil::splitPlateRowArea(cv::Mat img, int start_row, int start_col, int end_row, int end_col) {
 
 	//分隔行内容
 	int emptyStartRow = -1;
@@ -390,17 +389,18 @@
 				break;
 			}
 
-			if (startf > -1 && endf > -1 && emptyEndRow - emptyStartRow > 3) {
+			if (startf > -1 && endf > -1 && (dataItemList.size()>0 &&  emptyEndRow - emptyStartRow > 3|| emptyEndRow - emptyStartRow>0)) {
 				//内容坐标
 				   //LogUtil::debug("内容的高度为:%d \n", endf - startf);
-				int* dd = (int*)malloc(sizeof(int) * 4);
-				*dd = start_col;
-				*(dd + 1) = startf;
-				*(dd + 2) = end_col;
-				*(dd + 3) = endf;
+
 				//行数据高大于6才为有效的行高
 				if (endf - startf > 6)
 				{
+					int* dd = (int*)malloc(sizeof(int) * 4);
+					*dd = start_col;
+					*(dd + 1) = startf;
+					*(dd + 2) = end_col;
+					*(dd + 3) = endf;
 					dataItemList.push_back(dd);
 				}
 				startf = -1;
@@ -424,6 +424,101 @@
 			//数据结束
 		}
 	}
+
+	return dataItemList;
+}
+
+
+//分隔板块行数据
+std::list<int*> THSActionUtil::splitPlateRowArea(cv::Mat img) {
+
+	int rows = img.rows;
+	int cols = img.cols;
+	int r;
+	int contentStartRow = -1;
+	for (r = 5;r < img.rows;r++) {
+		if (ImgDivider::isRowFull(img, r, cols - 100, cols - 5, 2)) {
+			contentStartRow = r;
+		}
+
+		if (contentStartRow > -1) {
+			break;
+		}
+	}
+
+	if (contentStartRow < 0) {
+		throw string("起始行分隔出错");
+	}
+
+	//分隔列
+	int c = 0;
+	int startC = -1;
+	int endC = -1;
+	for (c = 50;c < cols;c++) {
+		if (ImgDivider::isColFull(img, c, contentStartRow + 5, contentStartRow + 100, 2)) {
+			if (startC < 0) {
+				startC = c;
+			}
+			else {
+				if (c - startC < 20) {
+					startC = c;
+				}
+				else {
+					endC = c;
+					break;
+				}
+			}
+		}
+	}
+
+	if (startC < 0 || endC < 0) {
+		throw string("内容框分隔出错");
+	}
+
+	//再次分隔内容框,排除干扰
+	int cut_index = -1;
+	for (int r = contentStartRow + 5;r < contentStartRow + 5 + 100;r++) {
+		
+		if (ImgDivider::isRowFull(img, r, startC, endC, 2,25,30)) {
+			cut_index = r;
+		}
+		else {
+			if (cut_index>0&& r - cut_index > 30) {
+				contentStartRow = cut_index;
+				break;
+			}
+		}
+	}
+
+
+
+
+
+
+	startC += 1;
+	endC -= 1;
+
+	int	start_row = contentStartRow + 25;
+	int	start_col = startC;
+	int	end_row = rows;
+	int	end_col = endC;
+	return splitPlateRowArea(img, start_row, start_col, end_row, end_col);
+
+}
+
+
+
+
+
+list<GPCodeArea> splitGPCodeArea(cv::Mat img, int start_row, int start_col, int end_row, int end_col) {
+
+	list<GPCodeArea> resultList;
+
+	//分隔行内容
+	std::list<int*> dataItemList = THSActionUtil::splitPlateRowArea(img, start_row, start_col, end_row, end_col);
+
+	
+
 
 	//分隔列
 	//分隔每一列的数据
@@ -530,6 +625,12 @@
 		}
 
 
+		//内存释放
+		for (std::list<int*>::iterator ele = rowDataList.begin();ele != rowDataList.end();ele++) {
+			free(*ele);
+		}
+
+		free(*ele);
 
 		//保存数据
 		if (false)
@@ -622,7 +723,7 @@
 	return resultList;
 }
 
-std::list<GPCodeArea>   THSActionUtil::recognitionGPCodeNum(cv::Mat img, std::list<GPCodeArea> areaList, RecognitionManager *recognitionManager) {
+std::list<GPCodeArea>   THSActionUtil::recognitionGPCodeNum(cv::Mat img, std::list<GPCodeArea> areaList, RecognitionManager* recognitionManager) {
 
 	//识别数字
 	std::list<GPCodeArea> codeList;
@@ -642,7 +743,7 @@
 				path.append(".jpg");
 				cv::imwrite(path, nums);
 			}
-		
+
 
 			std::list<cv::Mat> list2 = splitGPCodeNum(nums);
 
@@ -666,7 +767,7 @@
 				code.append(to_string(*ele1));
 			}
 			codeArea.code = code;
-			cout << code << endl;
+			//cout << code << endl;
 			codeList.push_back(codeArea);
 		}
 	}
@@ -678,7 +779,7 @@
 std::list<string> THSActionUtil::recognitionGPCode(cv::Mat img, RecognitionManager* recognitionManager) {
 	cv::Mat grayImg = ImgUtil::grayImage(img);
 	std::list<GPCodeArea> areaList = recognitionGPArea(grayImg);
-	std::list<GPCodeArea>  list = recognitionGPCodeNum(grayImg, areaList,recognitionManager);
+	std::list<GPCodeArea>  list = recognitionGPCodeNum(grayImg, areaList, recognitionManager);
 	std::list<string> resultList;
 	for (std::list<GPCodeArea>::iterator ele = list.begin();ele != list.end();ele++) {
 		resultList.push_back((*ele).code);
@@ -700,21 +801,23 @@
 			}
 			else {
 				if (r - contentStartRow > 10) {
-					contentEndRow = r;
+					//contentEndRow = r;
 
 				}
 
 			}
 		}
-
-		if (contentStartRow > -1 && contentEndRow > -1) {
+		//contentStartRow > -1 && contentEndRow > -1
+		if (contentStartRow > -1) {
 			break;
 		}
 	}
-
-	if (contentStartRow < 0 || contentEndRow < 0) {
+	//contentStartRow < 0 || contentEndRow < 0
+	if (contentStartRow < 0 ) {
 		throw string("起始行或结束行分隔出错");
 	}
+
+	contentEndRow = contentStartRow + 50;
 
 	//分隔列
 	list<int*> dataColIndexs;
@@ -788,16 +891,148 @@
 		int startx = *p;
 		int endx = *(p + 1);
 
+		int endRow = 0;
+		//判断结束行
+		for (int r = contentStartRow + 10;r < img.rows;r++) {
+			bool full = ImgDivider::isRowFull(img, r, startx, endx,2 , 10, 30);
+			if (full) {
+				endRow = r;
+				break;
+			}
+		}
+
+
+
 		GPCodeArea area = GPCodeArea();
 		area.startx = startx;
 		area.endx = endx;
 		area.starty = contentStartRow;
-		area.endy = contentEndRow;
+		area.endy = endRow;
 		areaList.push_back(area);
+
+		free(*ele);
 	}
 	return areaList;
 }
 
+
+
+//分隔L2交易队列
+std::list<GPCodeArea> splitL2TradeQueue(cv::Mat img) {
+	int cols = img.cols;
+	int contentStartRow = -1;
+	int contentEndRow = -1;
+	for (int r = 5;r < img.rows;r++) {
+		if (ImgDivider::isRowFull(img, r, 0, img.cols/3, 2, 10, 30)&& ImgDivider::isRowFull(img, r, img.cols*2 / 3, img.cols, 2, 10, 30)) {
+			if (contentStartRow < 0)
+			{
+				contentStartRow = r;
+				break;
+			}
+		}
+	}
+	//contentStartRow < 0 || contentEndRow < 0
+	if (contentStartRow < 0) {
+		throw string("起始行或结束行分隔出错");
+	}
+
+	contentEndRow = contentStartRow + 50;
+
+	//分隔列
+	list<int*> dataColIndexs;
+	int startf = -1;
+	int endf = -1;
+	int startIndex = -1;
+	for (int i = 10;i < cols;i++) {
+		if (startIndex == -1) {
+			startIndex = i;
+		}
+
+		bool full = ImgDivider::isColFull(img, i, contentStartRow, contentEndRow, 2);
+		if (full) {
+			if (startf < 0)
+			{
+				startf = i;
+				endf = i;
+			}
+			else {
+				endf = i;
+			}
+		}
+		else {
+			if (startf > -1 && endf > -1) {
+				int width = endf - startf + 1;
+				int* dd = (int*)malloc(sizeof(int) * 2);
+
+				*dd = startIndex;
+				*(dd + 1) = startf - 1;
+
+				dataColIndexs.push_back(dd);
+				startIndex = i;
+			}
+			startf = -1;
+			endf = -1;
+		}
+
+	}
+
+	if (startf > -1 && endf > -1) {
+		int width = endf - startf + 1;
+		int* dd = (int*)malloc(sizeof(int) * 2);
+
+		*dd = startIndex;
+		*(dd + 1) = startf - 1;
+		LogUtil::debug("列数据:%d-%d\n", *dd, *(dd + 1));
+
+		dataColIndexs.push_back(dd);
+
+		startf = -1;
+		endf = -1;
+	}
+
+	if (cols - startIndex > 50) {
+
+		int* dd = (int*)malloc(sizeof(int) * 2);
+
+		*dd = startIndex;
+		*(dd + 1) = cols - 1;
+
+		dataColIndexs.push_back(dd);
+	}
+
+
+
+
+	std::list<GPCodeArea> areaList;
+	list<int*>::iterator ele;
+	for (ele = dataColIndexs.begin();ele != dataColIndexs.end();ele++) {
+		int* p = *ele;
+		int startx = *p;
+		int endx = *(p + 1);
+
+		int endRow = 0;
+		//判断结束行
+		for (int r = contentStartRow + 10;r < img.rows;r++) {
+			bool full = ImgDivider::isRowFull(img, r, startx, endx, 2, 10, 30);
+			if (full) {
+				endRow = r;
+				break;
+			}
+		}
+
+
+
+		GPCodeArea area = GPCodeArea();
+		area.startx = startx;
+		area.endx = endx;
+		area.starty = contentStartRow;
+		area.endy = endRow;
+		areaList.push_back(area);
+
+		free(*ele);
+	}
+	return areaList;
+}
 
 
 std::map<int, string> THSActionUtil::getListenL2GPCodes(RecognitionManager* recognitionManager) {
@@ -814,7 +1049,7 @@
 }
 
 
-std::list<GPCodeArea> THSActionUtil::getListenL2GPAreaAndCode(RecognitionManager *recognitionManager) {
+std::list<GPCodeArea> THSActionUtil::getListenL2GPAreaAndCode(RecognitionManager* recognitionManager) {
 	//截图当前有哪些股票代码
 	list<HWND> wlist = Win32Util::searchWindow("同花顺(");
 	HWND mainPage = 0;
@@ -850,6 +1085,13 @@
 	int index = 0;
 	for (std::list<GPCodeArea>::iterator ele = areaList.begin();ele != areaList.end();ele++) {
 		GPCodeArea area = *ele;
+		if (false) {
+			string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gpcode\\";
+			path.append("cate_").append(to_string(index)).append(".jpg");
+			cv::imwrite(path, cv::Mat(img, cv::Rect(area.startx, area.starty, area.endx - area.startx + 1, area.endy - area.starty + 1)));
+		}
+
+
 		index++;
 		std::list<GPCodeArea> resultList = splitGPCodeArea(img, area.starty + 20, area.startx, area.endy, area.endx);
 

--
Gitblit v1.8.0