Jelajahi Sumber

补充内存释放,解决内存泄漏;

scbc.sat2 5 tahun lalu
induk
melakukan
0b2361e6c5
2 mengubah file dengan 72 tambahan dan 16 penghapusan
  1. 68 15
      cv/cv/cv.cpp
  2. 4 1
      cv/cv/cv.h

+ 68 - 15
cv/cv/cv.cpp

@@ -466,25 +466,33 @@ CV_API bool MatchSingleImage(cv::Mat& img, cv::Mat templImg, double& matchVal, c
 		0);
 	// 输出到文件中;
 	imwrite("showImg.png", showImg);
+	// 释放对象;
+	showImg.release();
 #endif
 
+	// 释放对象;
+	resultImg.release();
+
 	return true;
 }
 
 CV_API bool MatchSingleImage(cv::Mat& img, std::string strTmpImg, double& matchVal, cv::Rect& matchRect)
 {
-	if (img.empty() || !PathFileExists(strTmpImg.c_str()))
+	if (img.empty())
 		return false;
 
 	// 读取模板图片;
-	cv::Mat templ = imread(strTmpImg.c_str(), cv::IMREAD_COLOR);
-	if (templ.empty())
+	cv::Mat templImg = imread(strTmpImg.c_str(), cv::IMREAD_COLOR);
+	if (templImg.empty()) {
 		return false;
+	}
 
 	cv::Mat resultImg;
-	cv::matchTemplate(img, templ, resultImg, cv::TM_CCOEFF_NORMED);
-	if (resultImg.empty() )
+	cv::matchTemplate(img, templImg, resultImg, cv::TM_CCOEFF_NORMED);
+	if (resultImg.empty()) {
+		templImg.release();
 		return false;
+	}
 
 	// 最小和最大匹配值;
 	double lv_nMinVal = 0.0;
@@ -499,8 +507,8 @@ CV_API bool MatchSingleImage(cv::Mat& img, std::string strTmpImg, double& matchV
 	// 匹配区域,使用最大区域;
 	matchRect.x = lv_nMaxLoc.x;
 	matchRect.y = lv_nMaxLoc.y;
-	matchRect.width = templ.cols;
-	matchRect.height = templ.rows;
+	matchRect.width = templImg.cols;
+	matchRect.height = templImg.rows;
 
 #ifdef _DEBUG
 	// 在原图上框出模板位置;
@@ -508,29 +516,43 @@ CV_API bool MatchSingleImage(cv::Mat& img, std::string strTmpImg, double& matchV
 	img.copyTo(showImg);
 	cv::rectangle(showImg,
 		cv::Point(lv_nMaxLoc.x, lv_nMaxLoc.y), 
-		cv::Point(lv_nMaxLoc.x + templ.cols, lv_nMaxLoc.y + templ.rows),
+		cv::Point(lv_nMaxLoc.x + templImg.cols, lv_nMaxLoc.y + templImg.rows),
 		cv::Scalar(rand() % 255, rand() % 255, rand() % 255), 
 		2, 
 		cv::LINE_AA, 
 		0);
 	// 输出到文件中;
 	imwrite("showImg.png", showImg);
+	// 释放对象;
+	showImg.release();
 #endif
 
+	// 释放对象;
+	templImg.release();
+	resultImg.release();
+
 	return true;
 }
 
 CV_API bool MatchSingleImage(std::string strImg, std::string strTmpImg, double& matchVal, cv::Rect& matchRect)
 {
 	cv::Mat img;
-	cv::Mat templ;
-	if (!ReadImage(strImg.c_str(), img) || !ReadImage(strTmpImg.c_str(), templ))
+	cv::Mat templImg;
+	if (!ReadImage(strImg.c_str(), img) )
 		return false;
 
+	if (!ReadImage(strTmpImg.c_str(), templImg)) {
+		img.release();
+		return false;
+	}
+
 	cv::Mat resultImg;
-	cv::matchTemplate(img, templ, resultImg, cv::TM_CCOEFF_NORMED);
-	if (resultImg.empty())
+	cv::matchTemplate(img, templImg, resultImg, cv::TM_CCOEFF_NORMED);
+	if (resultImg.empty()) {
+		img.release();
+		templImg.release();
 		return false;
+	}
 
 	// 最小和最大匹配值;
 	double lv_nMinVal = 0.0;
@@ -545,8 +567,8 @@ CV_API bool MatchSingleImage(std::string strImg, std::string strTmpImg, double&
 	// 匹配区域,使用最大区域;
 	matchRect.x = lv_nMaxLoc.x;
 	matchRect.y = lv_nMaxLoc.y;
-	matchRect.width = templ.cols;
-	matchRect.height = templ.rows;
+	matchRect.width = templImg.cols;
+	matchRect.height = templImg.rows;
 
 #ifdef _DEBUG
 	// 在原图上框出模板位置;
@@ -554,15 +576,22 @@ CV_API bool MatchSingleImage(std::string strImg, std::string strTmpImg, double&
 	img.copyTo(showImg);
 	cv::rectangle(showImg,
 		cv::Point(lv_nMaxLoc.x, lv_nMaxLoc.y),
-		cv::Point(lv_nMaxLoc.x + templ.cols, lv_nMaxLoc.y + templ.rows),
+		cv::Point(lv_nMaxLoc.x + templImg.cols, lv_nMaxLoc.y + templImg.rows),
 		cv::Scalar(rand() % 255, rand() % 255, rand() % 255),
 		2,
 		cv::LINE_AA,
 		0);
 	// 输出到文件中;
 	imwrite("showImg.png", showImg);
+	// 释放对象;
+	showImg.release();
 #endif
 
+	// 释放对象;
+	img.release();
+	templImg.release();
+	resultImg.release();
+
 	return true;
 }
 
@@ -571,7 +600,31 @@ CV_API bool Mat2Bitmap(cv::Mat& img, CBitmap& bitmap)
 	return CV_API bool();
 }
 
+CV_API bool Mat2HBitmap(cv::Mat& img, HBITMAP& bitmap)
+{
+	//MAT类的TYPE=(nChannels-1+ CV_8U)<<3
+	int nChannels = (img.type() >> 3) - CV_8U + 1;
+	int iSize = img.cols * img.rows * nChannels;
+	bitmap = CreateBitmap(img.cols, img.rows, 1, nChannels * 8, img.data);
+	return true;
+}
+
 CV_API bool Mat2GdiplusBitmap(cv::Mat& img, Gdiplus::Bitmap& bitmap)
 {
 	return CV_API bool();
 }
+
+CV_API bool Bitmap2Mat(CBitmap& bitmap, cv::Mat& img)
+{
+	return CV_API bool();
+}
+
+CV_API bool HBitmap2Mat(HBITMAP& bitmap, cv::Mat& img)
+{
+	return CV_API bool();
+}
+
+CV_API bool GdiplusBitmap2Mat(Gdiplus::Bitmap& bitmap, cv::Mat& img)
+{
+	return CV_API bool();
+}

+ 4 - 1
cv/cv/cv.h

@@ -68,8 +68,11 @@ CV_API bool MatchSingleImage(std::string strImg, std::string strTmpImg, double&
 
 // 13、将opencv Mat对象转为GDI Bitmap对象和GDI+ Bitmap对象;
 CV_API bool Mat2Bitmap(cv::Mat &img, CBitmap &bitmap);
+CV_API bool Mat2HBitmap(cv::Mat& img, HBITMAP& bitmap);
 CV_API bool Mat2GdiplusBitmap(cv::Mat& img, Gdiplus::Bitmap& bitmap);
-
+CV_API bool Bitmap2Mat(CBitmap& bitmap, cv::Mat& img);
+CV_API bool HBitmap2Mat(HBITMAP& bitmap, cv::Mat& img);
+CV_API bool GdiplusBitmap2Mat(Gdiplus::Bitmap& bitmap, cv::Mat& img);
 
 // 待开发
 // 11、获取图片指定RGB颜色域的轮廓图像结果