From 086ec74e94654e92b3a1c6f42612d12ef33ff4b2 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期五, 28 十月 2022 14:41:08 +0800
Subject: [PATCH] '优化自启动机制'

---
 ConsoleApplication/TradeQueueCapture.cpp |  267 ++++++++++++++++++++++++++++++----------------------
 1 files changed, 153 insertions(+), 114 deletions(-)

diff --git a/ConsoleApplication/TradeQueueCapture.cpp b/ConsoleApplication/TradeQueueCapture.cpp
index 2da684b..6dee7b0 100644
--- a/ConsoleApplication/TradeQueueCapture.cpp
+++ b/ConsoleApplication/TradeQueueCapture.cpp
@@ -12,30 +12,10 @@
 
 
 
-int* TradeQueueCapture::split(cv::Mat img) {
-
-	int full_row_start = -1;
-	int full_row_end = -1;
-
+list<int*> TradeQueueCapture::split(cv::Mat img) {
 	list<int*> dataItemList;
-	//获取中间的分割线
-	for (int r = 5;r < img.rows;r++) {
-		if (ImgDivider::isRowFull(img, r, 5, img.cols - 5, 2)) {
-			if (full_row_start < 0) {
-				full_row_start = r;
-				full_row_end = r;
-			}
-			else {
-				full_row_end = r;
-			}
-		}
-		else {
-			if (full_row_start > -1 && full_row_end > -1) {
-				break;
-			}
-		}
-	}
-	int start_row = full_row_end + 1;
+	// 从图片中间分隔
+	int start_row = img.rows / 2 + 1;
 	//从分割线开始获取第一行的数据
 	int start_index = -1;
 	int end_index = -1;
@@ -53,106 +33,160 @@
 			}
 		}
 	}
-	int  start_col = -1;
-	int end_col = -1;
-	for (int c = img.cols - 1;c >= 0;c--) {
-		if (!ImgDivider::isColEmpty(img, c, start_index, end_index)) {
-			if (start_col < 0) {
-				start_col = c;
-			}
-			end_col = c;
-		}
-		else {
-			if (end_col > -1 && end_col - c > 5) {
+	int start_col = 0;
+	//已经下单的
+	if (end_index - start_index + 1 > _NUMBER_TRADE_QUEUE_HEIGHT + 4) {
+		//切割开始点
+		for (int c = img.cols - 5; c >= 0; c--) {
+			if (ImgDivider::isColFull(img,c,start_index,end_index,1)) {
+				start_col = c + 1;
 				break;
 			}
 		}
 	}
-	int* pos = (int*)malloc(sizeof(int) * 4);
-	pos[0] = end_col;
-	pos[1] = start_index;
-	pos[2] = start_col;
-	pos[3] = end_index;
+	 start_row = -1;
+	int end_row = -1;
+	if (start_col > 0) {
+	//去除上下的黑边
+		for (int r = start_index; r <= end_index; r++) {
+			if (!ImgDivider::isRowEmpty(img, r, start_col, img.cols - 1)) {
+				if (start_row < 0) {
+					start_row = r;
+					end_row = r;
+				}
+				end_row = r;
+			}
+			else {
+				if (start_row > 0 && end_row > 0) {
+					break;
+				}
+			}
+		}
+	}else {
+		start_col = 0;
+		start_row = start_index;
+		end_row = end_index;
+	}
 
-	return pos;
+	std::list<int*> posList;
+	if (start_row < 0 || end_row < 0) {
+		return posList;
+	}
+	 
+  
+	int start_col_ = -1;
+	int end_col_ = -1;
+	for (int c = img.cols - 1;c >= start_col;c--) {
+		if (!ImgDivider::isColEmpty(img, c, start_row, end_row)) {
+			if (start_col_ < 0) {
+				start_col_ = c;
+			}
+			end_col_ = c;
+		}
+		else {
+			if (end_col_ > -1 && end_col_ - c > 5) {
+				int* pos = (int*)malloc(sizeof(int) * 4);
+				pos[0] = end_col_;
+				pos[1] = start_row;
+				pos[2] = start_col_;
+				pos[3] = end_row;
+				posList.push_back(pos);
+				start_col_ = -1;
+				end_col_ = -1;
+				if (posList.size() >= 2) {
+					break;
+				}
+			}
+		}
+	}
+
+	if (start_col_ > -1 && end_col_ > -1 && posList.size()<2) {
+		int* pos = (int*)malloc(sizeof(int) * 4);
+		pos[0] = end_col_;
+		pos[1] = start_row;
+		pos[2] = start_col_;
+		pos[3] = end_row;
+		posList.push_back(pos);
+	}	
+
+	return posList;
 
 }
 
 
-int TradeQueueCapture::recognition_buy_1_volumn(cv::Mat img, int* pos) {
-	cv::Mat nums = cv::Mat(img, cv::Rect(pos[0], pos[1], pos[2] - pos[0] + 1, pos[3] - pos[1] + 1));
-	//cv::imwrite("C:\\Users\\Administrator\\Desktop\\ocr\\num.png", nums);
-
-	list<cv::Mat> matList = ImgUtil::splitNum(nums);
-	//string basePath = "C:\\Users\\Administrator\\Desktop\\ocr\\number_trade\\";
-	//for (list<cv::Mat>::iterator ele = matList.begin();ele != matList.end();ele++) {
-	//		string path = basePath;
-	//		path.append(to_string(clock()));
-	//		path.append(".jpg");
-	//		cv::imwrite(path,*ele);
-	//}
-
-
-
-
-
+TradeQueueResult TradeQueueCapture::recognition_buy_1_volumn(cv::Mat img, list<int*> posList) {
 	int c = 0;
 	unsigned char* zeroData = (unsigned char*)malloc(sizeof(unsigned char) * _NUMBER_TRADE_QUEUE_WIDTH * _NUMBER_TRADE_QUEUE_HEIGHT);
 	for (int r = 0;r < _NUMBER_TRADE_QUEUE_HEIGHT;r++) {
 		for (int c = 0;c < _NUMBER_TRADE_QUEUE_WIDTH;c++)
 		{
-			zeroData[r * _NUMBER_TRADE_QUEUE_WIDTH + c] = ImgUtil::NUMS_LEVEL2[0].data.ptr<uchar>(r)[c];
+			zeroData[r * _NUMBER_TRADE_QUEUE_WIDTH + c] = ImgUtil::NUMS_TRADE_QUEUE[0].data.ptr<uchar>(r)[c];
 		}
 	}
 
-	int length_per_num = matList.size();
-	int ele_count = 1;
-	unsigned char* totalNumberData = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_TRADE_QUEUE_HEIGHT * ele_count) * _NUMBER_TRADE_QUEUE_WIDTH * 10 * length_per_num);
-	//模板赋值
-	int index = 0;
-	for (list<cv::Mat>::iterator ele = matList.begin();ele != matList.end();ele++) {
-		cv::Mat mat = *ele;
-		mat = ImgUtil::formatNumTradeQueue(mat);
-
-		int x0 = index * 10 * _NUMBER_TRADE_QUEUE_WIDTH;
-
-		for (int n = 0;n < 10;n++)
-		{
-			int x1 = n * _NUMBER_TRADE_QUEUE_WIDTH;
-			for (int r = 0;r < mat.rows;r++) {
-				int y = r;
-				for (int c = 0;c < mat.cols;c++) {
-					int x = x0 + x1 + c;
-					int pos = y * _NUMBER_TRADE_QUEUE_WIDTH * 10 * length_per_num + x;
-					totalNumberData[pos] = mat.ptr<uchar>(r)[c];
-				}
-			}
-		}
-		index++;
-	}
-
-
-	uchar* templateNums = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_TRADE_QUEUE_HEIGHT * 1) * _NUMBER_TRADE_QUEUE_WIDTH * 10 * length_per_num);
-
-
-
-	openCLExcuter->createNumberTemplates(1, _NUMBER_TRADE_QUEUE_WIDTH, _NUMBER_TRADE_QUEUE_HEIGHT, length_per_num, ImgUtil::numsOneLevel_trade_queue, templateNums);
-
-
-
-	uchar** numberResult = openCLExcuter->recognition_numbers(totalNumberData, templateNums, 1 * _NUMBER_TRADE_QUEUE_HEIGHT, _NUMBER_TRADE_QUEUE_WIDTH * 10 * length_per_num, _NUMBER_TRADE_QUEUE_WIDTH, _NUMBER_TRADE_QUEUE_HEIGHT, length_per_num);
-
-
-
-
-	string st = "";
-	for (int j = 0;j < length_per_num;j++)
+	int length_per_num = 8;
+	int ele_count = 2;
+	
+	int* splitResult = (int*)malloc(sizeof(int) * 1 * 4 * ele_count);
+	
+	list<int*>::iterator ele = posList.begin();
+	int index = 1;
+	for (list<int*>::iterator ele = posList.begin(); ele != posList.end(); ++ele)
 	{
-		st.append(to_string(numberResult[0][j]));
+		splitResult[index * 4 + 0] = (*ele)[0];
+		splitResult[index * 4 + 1] = (*ele)[1];
+		splitResult[index * 4 + 2] = (*ele)[2];
+		splitResult[index * 4 + 3] = (*ele)[3];
+		index--;
+	}
+
+	int line_number_count = ele_count * length_per_num;
+	unsigned char* totalNumberData = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_TRADE_QUEUE_HEIGHT * 1) * _NUMBER_TRADE_QUEUE_WIDTH * 10 * line_number_count);
+	unsigned char types[] = { NUM_TYPE_PRICE, NUM_TYPE_NUM};
+	UcharDataInfo typesData = UcharDataInfo();
+	typesData.length = ele_count;
+	typesData.data = types;
+
+	openCLExcuter->splitTradeQueueNum(img, IntDataInfo({ splitResult,(int)(ele_count * 1) }), UcharDataInfo({ totalNumberData, -1 }), typesData, zeroData, _NUMBER_TRADE_QUEUE_WIDTH, _NUMBER_TRADE_QUEUE_HEIGHT, ele_count, length_per_num);
+
+
+	uchar* templateNums = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_TRADE_QUEUE_HEIGHT * 1) * _NUMBER_TRADE_QUEUE_WIDTH * 10 * line_number_count);
+
+
+
+	openCLExcuter->createNumberTemplates(1, _NUMBER_TRADE_QUEUE_WIDTH, _NUMBER_TRADE_QUEUE_HEIGHT, line_number_count, ImgUtil::numsOneLevel_trade_queue, templateNums);
+
+
+
+	uchar** numberResult = openCLExcuter->recognition_numbers(totalNumberData, templateNums, 1 * _NUMBER_TRADE_QUEUE_HEIGHT, _NUMBER_TRADE_QUEUE_WIDTH * 10 * line_number_count, _NUMBER_TRADE_QUEUE_WIDTH, _NUMBER_TRADE_QUEUE_HEIGHT, line_number_count);
+
+
+
+
+	TradeQueueResult result;
+	
+	for (int i = 0; i < ele_count; i++)
+	{
+		string st = "";
+		for (int j = 0; j < length_per_num; j++)
+		{
+			if (j == length_per_num - 2 && i==0) {
+				st.append(".");
+			}
+			st.append(to_string(numberResult[0][length_per_num* i + j]));
+		
+		}
+
+		if (i == 0) {
+			result.buy_1_price = st;
+		}
+		else if (i == 1) {
+			result.buy_1_volumn = stoi(st);
+		}
 	}
 
 	free(numberResult[0]);
-	int volumn = stoi(st);
+	int volumn = 0;//stoi(st);
 	//printf("结果:%d\n", volumn);
 
 	free(totalNumberData);
@@ -160,7 +194,9 @@
 	free(zeroData);
 	free(templateNums);
 	img.release();
-	return volumn;
+	
+
+	return result;
 }
 TradeQueueResult TradeQueueCapture::recognition_buy_1_volumn(HWND hwnd) {
 	//判断窗口是否显示
@@ -229,10 +265,10 @@
 	uchar* data = (uchar*)malloc(sizeof(uchar) * img.cols * img.rows);
 	if (img.channels() == 3)
 	{
-		openCLExcuter->rgb2ThresholdInvert(img.data, img.cols, img.rows, data, 210);
+		openCLExcuter->rgb2ThresholdInvert(img.data, img.cols, img.rows, data, 150);
 	}
 	else if (img.channels() == 4) {
-		openCLExcuter->rgba2ThresholdInvert(img.data, img.cols, img.rows, data, 210);
+		openCLExcuter->rgba2ThresholdInvert(img.data, img.cols, img.rows, data, 150);
 	}
 	cv::Mat grayImg = cv::Mat::zeros(img.rows, img.cols, CV_8UC1);
 	grayImg.data = data;
@@ -240,14 +276,17 @@
 	//string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gray.jpg";
 	//cv::imwrite(path, grayImg);
 
-	int* pos = split(grayImg);
-	int volumn = recognition_buy_1_volumn(grayImg, pos);
-	free(pos);
+	list<int*> posList = split(grayImg);
+	if (posList.size() < 2) {
+		free(data);
+		throw string("数据分隔出错");
+	}
+
+	TradeQueueResult result  = recognition_buy_1_volumn(grayImg, posList);
+	for (list<int*>::iterator ele = posList.begin(); ele != posList.end(); ele++) {
+		free(*ele);
+	}
 	free(data);
-
-
-	TradeQueueResult result = TradeQueueResult();
-	result.buy_1_volumn = volumn;
 
 	//std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
 	// wstring 转string,支持中文
@@ -259,7 +298,7 @@
 }
 
 
-int TradeQueueCapture::recognition_buy_1_volumn(cv::Mat grayImg) {
-	int* rowDataList = split(grayImg);
+TradeQueueResult TradeQueueCapture::recognition_buy_1_volumn(cv::Mat grayImg) {
+	list<int*> rowDataList = split(grayImg);
 	return recognition_buy_1_volumn(grayImg, rowDataList);
 }

--
Gitblit v1.8.0