Sfoglia il codice sorgente

improve the image process procedure.

gsp 4 anni fa
parent
commit
16cbeea36a

+ 1 - 1
OTSCPP/OTSClrInterface/ImageProClr/ImageProForClr.h

@@ -13,7 +13,7 @@ namespace OTSIMGPROC
 		  bool GetFieldDataFromImage(CBSEImgClr^ bseImg, COTSImgProcPrmClr^ perameter, COTSFieldDataClr^ fieldData)
 		{
 			 
-			bool ret = COTSImageProcess::RemoveBSEImageBG(bseImg->GetBSEImgPtr(), perameter->GetImgPrcPrmPtr(), fieldData->GetOTSFieldDataPtr());
+			bool ret = COTSImageProcess::RemoveBGByFindContour(bseImg->GetBSEImgPtr(), perameter->GetImgPrcPrmPtr(), fieldData->GetOTSFieldDataPtr());
 			return ret;
 
 		}

+ 1 - 1
OTSCPP/OTSData/OTSParticle.cpp

@@ -201,7 +201,7 @@ namespace OTSDATA {
 			}
 		}
 
-		if (nHmin > nHmax || nWmin > nWmax)
+		if (nHmin >= nHmax || nWmin >= nWmax)
 		{
 			return FALSE;
 		}

+ 182 - 36
OTSCPP/OTSImagePro/OTSImageProcess.cpp

@@ -1028,14 +1028,14 @@ namespace OTSIMGPROC
 			COTSParticleList listParticleEmpty;
 			listParticleEmpty.clear();
 			m_pFieldData->SetParticleList(listParticleEmpty);
-			//memset(pPixel, 0, nImgSize);
+		
 
-			LogInfoTrace(__FILE__, __LINE__, _T("RemoveBSEImageBG: no particle is found."));
+			//LogInfoTrace(__FILE__, __LINE__, _T("RemoveBSEImageBG: no particle is found."));
 		}
 		else
 		{
 
-			//memcpy(pTempImg, pPixel, nImgSize);
+			
 
 			// get the area image
 			COTSImageProcess::BErode3(pPixel, pTempImg, 5, nHeightImg, nWidthImg);
@@ -1044,12 +1044,12 @@ namespace OTSIMGPROC
 
 
 			COTSParticleList listParticleOut;
-			if (!GetParticles(nWidthImg, nHeightImg, pPixel, listParticleOut))
+			if (!GetParticles(0,0,nWidthImg, nHeightImg, pPixel, listParticleOut))
 			{
 				COTSParticleList listParticleEmpty;
 				listParticleEmpty.clear();
 				m_pFieldData->SetParticleList(listParticleEmpty);
-				//memset(pPixel, 0, nImgSize);
+				
 
 			}
 			
@@ -1119,12 +1119,171 @@ namespace OTSIMGPROC
 
 		
 		delete[]pTempImg;
-		//delete[]pTempImg;
 
 
 		return TRUE;
 	
 	
+	}
+	BOOL COTSImageProcess::RemoveBGByFindContour(CBSEImgPtr m_pBSEImg, COTSImageProcessParamPtr a_pImageProcessParam, COTSFieldDataPtr m_pFieldData)
+	{
+
+		ASSERT(m_pFieldData);
+
+		ASSERT(m_pBSEImg);	
+
+		ASSERT(a_pImageProcessParam);
+	
+
+		int nWidthImg = m_pBSEImg->GetWidth();
+		int nHeightImg = m_pBSEImg->GetHeight();
+		m_pFieldData->Width = nWidthImg;
+		m_pFieldData->Height = nHeightImg;
+		long nImgSize = nWidthImg * nHeightImg;
+
+
+		BYTE* pSrcImg = m_pBSEImg->GetImageDataPointer();
+
+		BYTE* pTempImg = new BYTE[nImgSize];
+
+
+		CBSEImgPtr imgNoBGBinary = CBSEImgPtr(new CBSEImg());
+		long nNumParticle = 0;
+		RemoveBackGround(m_pBSEImg, a_pImageProcessParam, imgNoBGBinary, nNumParticle);
+
+
+
+
+		BYTE* pPixel = imgNoBGBinary->GetImageDataPointer();
+
+
+
+
+		long nPtStart = a_pImageProcessParam->GetParticleGray().GetStart();
+		long nPtEnd = a_pImageProcessParam->GetParticleGray().GetEnd();
+
+
+		if (nNumParticle == 0)
+		{
+
+			COTSParticleList listParticleEmpty;
+			listParticleEmpty.clear();
+			m_pFieldData->SetParticleList(listParticleEmpty);
+			
+
+			//LogInfoTrace(__FILE__, __LINE__, _T("RemoveBSEImageBG: no particle is found."));
+		}
+		else
+		{
+
+			// get the area image
+			COTSImageProcess::BErode3(pPixel, pTempImg, 5, nHeightImg, nWidthImg);
+			COTSImageProcess::BDilate3(pTempImg, pPixel, 5, nHeightImg, nWidthImg);
+
+			Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);
+			Mat blurImg;
+			medianBlur(cvcopyImg, blurImg, 5);//smooth the edge
+
+			vector<vector<Point>>contours;
+			
+			findContours(cvcopyImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
+
+			COTSParticleList listParticleOut;
+			for (size_t i = 0; i < contours.size(); i++)
+			{
+				
+				Rect rectMax = boundingRect(contours[i]);
+				Mat  rectROI = cvcopyImg(rectMax).clone();
+
+
+				//exclude the point which  intersect into this bounding box but is not in this contour.
+				for (int nX = 0; nX < rectROI.rows; nX++)
+				{
+					for (int nY = 0; nY < rectROI.cols; nY++)
+					{
+						double localPos = pointPolygonTest(contours[i], Point2f(nX + rectMax.x, nY + rectMax.y), false);
+						if (localPos == -1)
+						{
+							rectROI.data[nX, nY] = 0;//set the value to 0,so we won't consider it when we find segment and feature in this ROI.
+						}
+
+					}
+
+				}
+
+				GetParticles(rectMax.x, rectMax.y, rectMax.width, rectMax.height, rectROI.data, listParticleOut);
+				
+			}
+
+
+
+			// form a image only have particles on
+			COTSSegmentsList listImage;
+			for (auto pParticle : listParticleOut)
+			{
+				COTSFeaturePtr pFeature = pParticle->GetFeature();
+				COTSSegmentsList listSegment = pFeature->GetSegmentsList();
+
+				long nPixelNum = 0;
+				long nPixelAll = 0;
+				int nStartS = 0;
+				int nHeightS = 0;
+				int nLengthS = 0;
+				for (auto pSegment : listSegment)
+				{
+					// update image list
+					COTSSegmentPtr pSegNew = COTSSegmentPtr(new COTSSegment(*pSegment.get()));
+					listImage.push_back(pSegNew);
+
+
+					// get particle average gray
+					nStartS = pSegment->GetStart();
+					nHeightS = pSegment->GetHeight();
+					nLengthS = pSegment->GetLength();
+					nPixelNum += (long)nLengthS;
+
+
+					if (nHeightS > nHeightImg)
+					{
+						//LogErrorTrace(__FILE__, __LINE__, _T("seg height is wrong."));
+						return FALSE;
+					}
+					if ((nStartS + nLengthS - 1) > nWidthImg)
+					{
+						//LogErrorTrace(__FILE__, __LINE__, _T("seg starst and length is wrong."));
+						return FALSE;
+					}
+
+					for (unsigned int i = 0; i < nLengthS; i++)
+					{
+						if ((nStartS + i) > nWidthImg)
+						{
+							//LogErrorTrace(__FILE__, __LINE__, _T("seg start is wrong."));
+							return FALSE;
+						}
+						else if (nHeightS > nHeightImg)
+						{
+							//LogErrorTrace(__FILE__, __LINE__, _T("seg height is wrong."));
+							return FALSE;
+						}
+
+						long nValueTemp = (long)*(pSrcImg + nHeightS * nWidthImg + nStartS + i);
+						nPixelAll += nValueTemp;
+					}
+				}
+				BYTE nAveGray = (BYTE)(nPixelAll / nPixelNum);
+				pParticle->SetAveGray(nAveGray);
+				pParticle->SetArea(nPixelNum);
+
+			}
+			m_pFieldData->SetParticleList(listParticleOut);
+		}
+
+
+		delete[]pTempImg;
+
+
+		return TRUE;
 	}
 	CIntRangePtr COTSImageProcess::CalBackground(CBSEImgPtr m_pBSEImg)
 	{
@@ -1223,9 +1382,7 @@ namespace OTSIMGPROC
 		BYTE* pTempImg2 = new BYTE[nImgSize];
 
 		BYTE* pSrcImg = a_pImgIn->GetImageDataPointer();
-		//memcpy(pTempImg2, pSrcImg, nImgSize);
-
-		//cv::blur(pSrcImg, pTempImg2, nImgSize);
+	
 
 	
 
@@ -1328,33 +1485,27 @@ namespace OTSIMGPROC
 		delete[] pTempImg2;
 		return ;
 	}
-	BOOL COTSImageProcess::GetParticles(long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSParticleList& a_listParticles)
+	BOOL COTSImageProcess::GetParticles(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSParticleList& a_listParticles)
 	{
-
 		ASSERT(a_pPixel);
 		if (!a_pPixel)
 		{
-			LogErrorTrace(__FILE__, __LINE__, _T("GetParticles: there is no image data"));
 			return FALSE;
 		}
 
-		a_listParticles.clear();
-
+		//a_listParticles.clear();
+		COTSParticleList findedParts;
 		COTSSegmentsList listSegment;
 		listSegment.clear();
 
 
 		//1. get segment line by line	
-		if (!GetSegmentList(a_nWidth, a_nHeight, a_pPixel, listSegment))
+		if (!GetSegmentList(left, top, a_nWidth, a_nHeight, a_pPixel, listSegment))
 		{
-			LogErrorTrace(__FILE__, __LINE__, _T("GetParticles:failed to get segments."));
 			return FALSE;
 		}
-
-
 		if ((int)listSegment.size() == 0)
 		{
-			LogErrorTrace(__FILE__, __LINE__, _T("no particle is found."));
 
 			return FALSE;
 		}
@@ -1363,38 +1514,36 @@ namespace OTSIMGPROC
 		//2. save the temp feature
 		COTSFeatureList listFeature;
 		listFeature.clear();
-		if (!GetFeatureList1(listSegment, listFeature))
+		if (!GetFeatureList(listSegment, listFeature))//get every feature for all the particle,the complete feature.
 		{
-			LogErrorTrace(__FILE__, __LINE__, _T("GetParticles:failed to get up down segment list."));
 			return FALSE;
 		}
 
 
 		if ((int)listFeature.size() == 0)
 		{
-			LogErrorTrace(__FILE__, __LINE__, _T("no particle is found."));
 			return FALSE;
 		}
 
 		COTSParticleList listParticles;
 		listParticles.clear();
-		if (!ChangeFeaturelist(listFeature, a_listParticles))
+		if (!ChangeFeaturelist(listFeature, findedParts))
 		{
-			LogErrorTrace(__FILE__, __LINE__, _T("can't change feature to particle."));
 			return FALSE;
 		}
-
+		for (auto f : findedParts)
+		{
+			a_listParticles.push_back(f);
+		}
 
 		return TRUE;
-
-
 	}
-	BOOL COTSImageProcess::GetSegmentList(long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSSegmentsList& a_listSegments)
+	BOOL COTSImageProcess::GetSegmentList(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSSegmentsList& a_listSegments)
 	{
 		ASSERT(a_pPixel);
 		if (!a_pPixel)
 		{
-			LogErrorTrace(__FILE__, __LINE__, _T("GetSegments: there is no image data"));
+			//LogErrorTrace(__FILE__, __LINE__, _T("GetSegments: there is no image data"));
 			return FALSE;
 		}
 
@@ -1404,7 +1553,7 @@ namespace OTSIMGPROC
 
 		//1. get segment line by line
 		long nLine, nm, nn;
-		long nStart, nLength;
+		long nStart = 0, nLength = 0;
 		for (nLine = 0; nLine < a_nHeight; nLine++)
 		{
 			for (nm = 0; nm < a_nWidth; nm += (nLength + 1))
@@ -1432,7 +1581,7 @@ namespace OTSIMGPROC
 					}
 
 					// generate segment
-					COTSSegmentPtr pSegment = COTSSegmentPtr(new COTSSegment(nLine, nStart, nLength));
+					COTSSegmentPtr pSegment = COTSSegmentPtr(new COTSSegment(nLine + top, nStart + left, nLength));
 					a_listSegments.push_back(pSegment);
 
 				}
@@ -1445,13 +1594,13 @@ namespace OTSIMGPROC
 
 		if ((int)a_listSegments.size() == 0)
 		{
-			LogErrorTrace(__FILE__, __LINE__, _T("no particle is found."));
+			//LogErrorTrace(__FILE__, __LINE__, _T("no particle is found."));
 			return FALSE;
 		}
 
 		return TRUE;
 	}
-	BOOL COTSImageProcess::GetFeatureList1(COTSSegmentsList& a_listSegments, COTSFeatureList& a_listFeatures)
+	BOOL COTSImageProcess::GetFeatureList(COTSSegmentsList& a_listSegments, COTSFeatureList& a_listFeatures)
 	{
 		COTSSegmentsList listSegmentNew;
 		std::map<long, COTSSegmentsList > mapOneLineSegments;
@@ -1553,7 +1702,6 @@ namespace OTSIMGPROC
 	{
 		if (a_listFeatures.size() == 0)
 		{
-			LogErrorTrace(__FILE__, __LINE__, _T("ChangeFeaturelist: there is no feature in the list."));
 			return FALSE;
 		}
 		// compute Rect
@@ -1563,7 +1711,6 @@ namespace OTSIMGPROC
 			pParticle->SetFeature(pFeature);
 			if (!pParticle->CalCoverRect())
 			{
-				LogErrorTrace(__FILE__, __LINE__, _T("ChangeFeaturelist: failed to get particle rect."));
 				return FALSE;
 			}
 			a_listParticle.push_back(pParticle);
@@ -1571,7 +1718,6 @@ namespace OTSIMGPROC
 
 		if ((int)a_listParticle.size() == 0)
 		{
-			LogErrorTrace(__FILE__, __LINE__, _T("Can't get particle."));
 			return FALSE;
 		}
 

+ 5 - 3
OTSCPP/OTSImagePro/OTSImageProcess.h

@@ -62,6 +62,8 @@ namespace OTSIMGPROC {
 		static BOOL ReZoom(CString InPutPath, CString OutPutPath);
 		static BOOL RemoveBSEImageBG(CBSEImgPtr m_pBSEImg, COTSImageProcessParamPtr a_pImageProcessParam, COTSFieldDataPtr m_pFieldData);
 
+		static BOOL RemoveBGByFindContour(CBSEImgPtr m_pBSEImg, COTSImageProcessParamPtr a_pImageProcessParam, COTSFieldDataPtr m_pFieldData);
+
 		static CIntRangePtr CalBackground(CBSEImgPtr m_pBSEImg);
 		static void RemoveBackGround(CBSEImgPtr a_pImgIn, COTSImageProcessParamPtr a_pImageProcessParam, CBSEImgPtr a_pImgOut,long& foundedPixelNum);
 		static BOOL CalcuParticleImagePropertes(COTSParticlePtr part, double a_PixelSize);
@@ -69,9 +71,9 @@ namespace OTSIMGPROC {
 		static BOOL MergeBigBoundaryParticles(COTSFieldDataList allFields, double pixelSize, int scanFieldSize, CSize ResolutionSize, COTSParticleList& mergedParts);
 
 	protected:
-		static BOOL GetParticles(long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSParticleList& a_listParticles);
-		static BOOL GetSegmentList(long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSSegmentsList& a_listSegments);
-		static BOOL GetFeatureList1(COTSSegmentsList& a_listSegments, COTSFeatureList& a_listFeatures);
+		static BOOL GetParticles(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSParticleList& a_listParticles);
+		static BOOL GetSegmentList(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSSegmentsList& a_listSegments);
+		static BOOL GetFeatureList(COTSSegmentsList& a_listSegments, COTSFeatureList& a_listFeatures);
 		static BOOL ChangeFeaturelist(COTSFeatureList& a_listFeatures, COTSParticleList& a_listParticle);
 		
 		

+ 2 - 4
OTSIncAMeasureApp/0-OTSModel/Measure/GetBSEPic/CSmplMeasure.cs

@@ -619,13 +619,11 @@ namespace OTSModelSharp
                  var rst = FieldImageProcess(poiFieldCentre, pBSEImg);
                     if (rst == true)
                     {
-                        // is the field data empty
-                        //if (!curFldData.IsEmpty())
-                        //{
+              
                             // add the field into the field
                             m_pSampleRstFile.AddAField(curFldData);
 
-                        //}
+                       
                     }