소스 검색

添加Opencv相关图像处理函数。

Jeff 5 년 전
부모
커밋
688a809343
4개의 변경된 파일105개의 추가작업 그리고 3개의 파일을 삭제
  1. 88 0
      GameAssist/GameAssist/Assist.cpp
  2. 4 0
      GameAssist/GameAssist/Assist.h
  3. 1 0
      GameAssist/GameAssist/GameAssistDlg.cpp
  4. 12 3
      GameAssist/GameAssist/stdafx.h

+ 88 - 0
GameAssist/GameAssist/Assist.cpp

@@ -666,4 +666,92 @@ namespace GAssist
 		return false;
 	}
 
+	BOOL OpenImage(std::string strImag, cv::Mat img)
+	{
+		if (!PathFileExists(strImag.c_str()))
+			return FALSE;
+
+		if (img.data != NULL)
+			img.release();
+
+		img = cv::imread(strImag.c_str(), cv::IMREAD_COLOR);
+		if (img.data == NULL)
+			return FALSE;
+
+		return TRUE;
+	}
+
+	BOOL SetImgThreshold(cv::Mat srcImg, cv::Mat thresholdImg, long nThresholdVal, long nMaxThresholdVal, int type)
+	{
+		if (srcImg.data == NULL)
+			return FALSE;
+
+		if (thresholdImg.data)
+			thresholdImg.release();
+
+		switch (type)
+		{
+			case 0://全局阀值;
+				{
+					// 转成灰阶图;
+					cv::cvtColor(srcImg, thresholdImg, cv::COLOR_BGR2GRAY);
+					// 再高斯模糊处理(滤波);
+					cv::GaussianBlur(thresholdImg, thresholdImg, cv::Size(5, 5), 0, 0);
+					// 二值化;//全局化指定的阀值与返回值相等;
+					double dRetVal = cv::threshold(thresholdImg, thresholdImg, nThresholdVal, nMaxThresholdVal, cv::THRESH_BINARY);
+				}
+				break;
+			case 1://自适应阀值;
+				{
+					// 转成灰阶图;
+					cv::cvtColor(srcImg, thresholdImg, cv::COLOR_BGR2GRAY);
+					// 再高斯模糊处理(滤波);
+					cv::GaussianBlur(thresholdImg, thresholdImg, cv::Size(3, 3), 0, 0);
+					// 自适应阀值;
+					cv::medianBlur(thresholdImg, thresholdImg, 3);
+					// 局部二值化;//blocksize一般取3、5、7
+					cv::adaptiveThreshold(thresholdImg, thresholdImg, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 3, 4.5);
+				}
+				break;
+			case 2:// otsu阀值;
+				{
+					cv::cvtColor(srcImg, thresholdImg, cv::COLOR_BGR2GRAY);
+					// 高斯模糊;
+					cv::GaussianBlur(thresholdImg, thresholdImg, cv::Size(3, 3), 0, 0);
+					cv::threshold(thresholdImg, thresholdImg, nThresholdVal, nMaxThresholdVal, cv::THRESH_BINARY | cv::THRESH_OTSU);
+				}
+				break;
+			default:
+				break;
+		}
+
+		return 0;
+	}
+
+	BOOL GetImgMatchtemplate(std::string strSrcImg, std::string strTempImg, RECT& rc, double lowestMatchValue)
+	{
+		cv::Mat srcImg, tempImg, matchImg;
+		if (!OpenImage(strSrcImg, srcImg) || !OpenImage(strTempImg, tempImg))
+			return FALSE;
+
+		cv::matchTemplate(srcImg, tempImg, matchImg, cv::TM_CCOEFF_NORMED);
+		// 归一化到0~1
+		//cv::normalize(matchImg, matchImg, 0, 1, cv::NORM_MINMAX, -1);
+
+		double lv_nMinVal = 0.0;
+		double lv_nMaxVal = 0.0;
+		cv::Point lv_nMinLoc = cv::Point(0, 0);
+		cv::Point lv_nMaxLoc = cv::Point(0, 0);
+		cv::Point lv_MatchLoc = cv::Point(0, 0);
+		cv::minMaxLoc(matchImg, &lv_nMinVal, &lv_nMaxVal, &lv_nMinLoc, &lv_nMaxLoc);
+
+		rc.left = lv_nMaxLoc.x;
+		rc.top = lv_nMaxLoc.y;
+		rc.right = rc.left + matchImg.cols;
+		rc.bottom = rc.top + matchImg.rows;
+
+		// 是否大于最低匹配度;
+		return (lv_nMaxVal >= lowestMatchValue ? TRUE : FALSE);
+	}
+
 }

+ 4 - 0
GameAssist/GameAssist/Assist.h

@@ -67,6 +67,10 @@ namespace GAssist
 	BOOL SaveBitmap(HBITMAP hBitmpa, std::string strSavePath);
 	void SaveHwndToBmpFile(HWND hWnd, LPCTSTR lpszPath);
 	bool IsCoveredByOtherWindow(HWND  hWnd);
+
+	BOOL OpenImage(std::string strImag, cv::Mat img);
+	BOOL SetImgThreshold(cv::Mat srcImg, cv::Mat thresholdImg, long nThresholdVal, long nMaxThresholdVal, int type = 0);
+	BOOL GetImgMatchtemplate(std::string strSrcImg, std::string strTempImg, RECT &rc, double lowestMatchValue = 0.900);
 };
 
 #endif // __ASSIST__

+ 1 - 0
GameAssist/GameAssist/GameAssistDlg.cpp

@@ -101,6 +101,7 @@ BOOL CGameAssistDlg::OnInitDialog()
 	SetIcon(m_hIcon, FALSE);		// 设置小图标
 
 	// TODO: 在此添加额外的初始化代码
+	cv::Mat imgTmp;
 
 	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
 }

+ 12 - 3
GameAssist/GameAssist/stdafx.h

@@ -38,9 +38,18 @@
 #include <afxcontrolbars.h>     // šŚÄÜÇřşÍżŘźţĚőľÄ MFC Ö§łÖ
 
 
-
-
-
+#include "opencv2/opencv_modules.hpp"
+
+#include "opencv2/core.hpp"
+#include "opencv2/imgproc.hpp"
+#include "opencv2/imgproc/imgproc_c.h"
+#include "opencv2/imgcodecs.hpp"
+#include "opencv2/videoio.hpp"
+#include "opencv2/highgui.hpp"
+#include "opencv2/core/utility.hpp"
+#include "opencv2/core/utils/trace.hpp"
+#include "opencv2/core/hal/hal.hpp"
+#include "opencv2/cvconfig.h"