فهرست منبع

update removebackground algorithm

gsp 1 سال پیش
والد
کامیت
bc57b25bc0
3فایلهای تغییر یافته به همراه99 افزوده شده و 68 حذف شده
  1. 75 0
      OTSCPP/OTSImagePro/BaseFunction.cpp
  2. 4 0
      OTSCPP/OTSImagePro/BaseFunction.h
  3. 20 68
      OTSCPP/OTSImagePro/OTSImageProcess.cpp

+ 75 - 0
OTSCPP/OTSImagePro/BaseFunction.cpp

@@ -6,6 +6,7 @@
 #include "OTSParticle.h"
 #include "OTSImageProcessParam.h"
 #include <OTSFieldData.h>
+#include "OTSMorphology.h"
 using namespace cv;
 using namespace std;
 using namespace OTSDATA;
@@ -255,4 +256,78 @@ Mat AdjustContrastY(const Mat& img)
 	ImageStretchByHistogram(workImg, out);
 
 	return Mat(out);
+}
+
+
+void CVRemoveBG(const cv::Mat& img, cv::Mat& dst,int bgstart,int bgend, long& nNumParticle)
+{
+	int min_gray = bgstart;
+	int max_gray = bgend;
+	if (img.empty())
+	{
+		std::cout << "图像为空";
+		return;
+	}
+	Mat image = img.clone();
+	if (image.channels() != 1)
+	{
+		cv::cvtColor(image, image, cv::COLOR_BGR2GRAY);
+	}
+	//lut 查找表 取规定范围的灰度图 排除拼图时四周灰度为255区域 以及 灰度值较低的区域
+	uchar lutvalues[256];
+	for (int i = 0; i < 256; i++)
+	{
+		if (i < min_gray || i > max_gray)
+		{
+			lutvalues[i] = i;
+			nNumParticle++;
+		}
+		else
+		{
+			lutvalues[i] = 0;
+		}
+	}
+	cv::Mat lutpara(1, 256, CV_8UC1, lutvalues);
+	cv::LUT(image, lutpara, image);
+	cv::Mat out_fill0, out_fill;
+	//开运算 获得x>5 的元素
+	cv::morphologyEx(image, out_fill0, cv::MorphTypes::MORPH_OPEN, cv::getStructuringElement(0, cv::Size(5, 1)), cv::Point(-1, -1), 1);
+	cv::morphologyEx(image, out_fill, cv::MorphTypes::MORPH_OPEN, cv::getStructuringElement(0, cv::Size(1, 5)), cv::Point(-1, -1), 1);
+	out_fill = out_fill + out_fill0;
+	//闭运算 
+	//cv::morphologyEx(out_fill, out_fill, cv::MorphTypes::MORPH_CLOSE, cv::getStructuringElement(0, cv::Size(5, 3)), cv::Point(-1, -1), 1);
+	//二值
+	cv::threshold(out_fill, out_fill, 1, 255, cv::ThresholdTypes::THRESH_BINARY);
+	dst = out_fill.clone();
+}
+void RemoveBG_old(const cv::Mat& img, cv::Mat& dst, int nBGStart, int nBGEnd,long& nNumParticle)
+{
+	int w, h;
+	w = img.cols;
+	h = img.rows;
+	BYTE* pSrcImg = img.data;
+	BYTE* pPixel = new BYTE[w * h];
+	BYTE* pTempImg = new BYTE[w * h];
+	for (unsigned int i = 0; i < w*h; i++)
+	{
+		if (pSrcImg[i] < nBGStart || pSrcImg[i] > nBGEnd)
+		{
+			pPixel[i] = 255;
+			nNumParticle++;
+		}
+		else
+		{
+			pPixel[i] = 0;
+			
+		}
+		
+	}
+	int errodDilateParam =5;
+	if (errodDilateParam > 0)
+	{
+
+		BErode3(pPixel, pTempImg, errodDilateParam, h, w);
+		BDilate3(pTempImg, pPixel, errodDilateParam, h, w);
+	}
+	dst.data = pPixel;
 }

+ 4 - 0
OTSCPP/OTSImagePro/BaseFunction.h

@@ -29,3 +29,7 @@ CBSEImgPtr GetBSEImgFromMat(Mat inImg);
 void ImageStretchByHistogram(const Mat& src, Mat& dst);
 
 Mat AdjustContrastY(const Mat& img);
+
+void CVRemoveBG(const cv::Mat& img, cv::Mat& dst, int bgstart, int bgend, long& nNumParticle);
+
+void RemoveBG_old(const cv::Mat& img, cv::Mat& dst, int nBGStart, int nBGEnd, long& nNumParticle);

+ 20 - 68
OTSCPP/OTSImagePro/OTSImageProcess.cpp

@@ -581,8 +581,9 @@ namespace OTSIMGPROC
 		BYTE* pSrcImg = a_pImgIn->GetImageDataPointer();
 	
 
-	    BYTE* pPixel = new byte[nImgSize];
-	
+	    BYTE* pPixel= new BYTE[nImgSize];
+		Mat srcImgMat = GetMatDataFromBseImg(a_pImgIn);
+		Mat rstMat;
 		
 
 		long nBGStart;
@@ -597,31 +598,13 @@ namespace OTSIMGPROC
 			nPartStart = a_pImageProcessParam->GetParticleGray().GetStart();
 			nPartEnd = a_pImageProcessParam->GetParticleGray().GetEnd();
 
-			// delete background 
-			for (unsigned int i = 0; i < nImgSize; i++)
-			{
-				if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
-				{
-					pPixel[i] = 0;
-				}
-				else
-				{
-					pPixel[i] = 255;
-					nNumParticle++;
-				}
-				if (pSrcImg[i]<nPartStart || pSrcImg[i]>nPartEnd)
-				{
-					pPixel[i] = 0;
-				}
-			}
-			int errodDilateParam =5;
-			if (errodDilateParam > 0)
-			{
-				
-				BErode3(pPixel, pTempImg, errodDilateParam, nHeightImg, nWidthImg);
-				BDilate3(pTempImg, pPixel, errodDilateParam, nHeightImg, nWidthImg);
-			}
 			
+			
+			
+			CVRemoveBG(srcImgMat, rstMat, nBGStart, nBGEnd,nNumParticle);
+			//RemoveBG_old(srcImgMat, rstMat, nBGStart, nBGEnd, nNumParticle);
+
+			pPixel = rstMat.data;
 		
 
 			
@@ -638,56 +621,25 @@ namespace OTSIMGPROC
 			switch (a_pImageProcessParam->GetAutoBGRemoveType())
 			{
 			case OTS_AUTOBGREMOVE_TYPE::DOWNWARD:
-				for (unsigned int i = 0; i < nImgSize; i++)
-				{
-					if (pSrcImg[i] <= nBGEnd)
-					{
-						pPixel[i] = 0;
-					}
-					else
-					{
-						pPixel[i] = 255;
-						nNumParticle++;
-					}
-				}
+				
+				//RemoveBG_old(srcImgMat, rstMat, 0, nBGEnd, nNumParticle);
+				CVRemoveBG(srcImgMat, rstMat, 0, nBGEnd, nNumParticle);
 				break;
 			case OTS_AUTOBGREMOVE_TYPE::UPWARD:
-				for (unsigned int i = 0; i < nImgSize; i++)
-				{
-					if (pSrcImg[i] >= nBGStart)
-					{
-						pPixel[i] = 0;
-					}
-					else
-					{
-						pPixel[i] = 255;
-						nNumParticle++;
-					}
-				}
+				
+				//RemoveBG_old(srcImgMat, rstMat, nBGStart, 255, nNumParticle);
+				CVRemoveBG(srcImgMat, rstMat, nBGStart, 255, nNumParticle);
 				break;
 			case OTS_AUTOBGREMOVE_TYPE::MIDDLE:
-				for (unsigned int i = 0; i < nImgSize; i++)
-				{
-					if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
-					{
-						pPixel[i] = 0;
-					}
-					else
-					{
-						pPixel[i] = 255;
-						nNumParticle++;
-					}
-				}
+				
+				//RemoveBG_old(srcImgMat, rstMat, nBGStart, nBGEnd, nNumParticle);
+				CVRemoveBG(srcImgMat, rstMat, nBGStart, nBGEnd, nNumParticle);
+
 				break;
 			default:
 				break;
 			}
-			int errodDilateParam = 5;
-			if (errodDilateParam > 0)
-			{
-				BErode3(pPixel, pTempImg, errodDilateParam, nHeightImg, nWidthImg);
-				BDilate3(pTempImg, pPixel, errodDilateParam, nHeightImg, nWidthImg);
-			}
+			
 		
 		}
 		a_pBinImgOut->SetImageData(pPixel,nWidthImg,nHeightImg);