|
|
@@ -1,21 +1,16 @@
|
|
|
using OTSDataType;
|
|
|
-using OTSModelSharp.ImageProcess;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Drawing;
|
|
|
using System.Linq;
|
|
|
-using System.Text;
|
|
|
-using System.Threading.Tasks;
|
|
|
using static OTSDataType.otsdataconst;
|
|
|
|
|
|
namespace OTSModelSharp.ServiceCenter
|
|
|
{
|
|
|
using OpenCvSharp;
|
|
|
using OTSCLRINTERFACE;
|
|
|
- using OTSIMGPROC;
|
|
|
using System.Drawing.Imaging;
|
|
|
using System.Runtime.InteropServices;
|
|
|
- using System.Windows;
|
|
|
|
|
|
public class CImageHandler
|
|
|
{
|
|
|
@@ -36,7 +31,7 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
/// <returns>位图</returns>
|
|
|
public static Bitmap ToGrayBitmap(byte[] data, int width, int height)
|
|
|
{
|
|
|
- if (width == 0 || height == 0)
|
|
|
+ if (width == 0 || height == 0)
|
|
|
return null;
|
|
|
//// 申请目标位图的变量,并将其内存区域锁定
|
|
|
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
|
|
|
@@ -106,31 +101,31 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
}
|
|
|
|
|
|
//获取测量的BSE图
|
|
|
-
|
|
|
+
|
|
|
public bool GetBSEImage(COTSField tempFld, COTSImageProcParam ImgProcPrm, double pixelSize, int iWidth, int iHeight, ref byte[] BSEImageNoBG)
|
|
|
{
|
|
|
Rectangle rect = new Rectangle();
|
|
|
rect.Height = iHeight;
|
|
|
rect.Width = iWidth;
|
|
|
-
|
|
|
+
|
|
|
CBSEImgClr pBSEImageOut = new CBSEImgClr(rect);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
if (null == ImgProcPrm)
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
|
|
|
- RemoveBackGround(tempFld, ImgProcPrm, pixelSize ,ref pBSEImageOut);
|
|
|
+ RemoveBackGround(tempFld, ImgProcPrm, pixelSize, ref pBSEImageOut);
|
|
|
|
|
|
|
|
|
BSEImageNoBG = pBSEImageOut.GetImageDataPtr();
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取测量的BSE图
|
|
|
@@ -150,20 +145,20 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
CBSEImgClr pBSEImageIn = new CBSEImgClr(rect);
|
|
|
CBSEImgClr pBSEImageOut = new CBSEImgClr(rect);
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- pBSEImageIn.SetImageData(BSEImage, iWidth, iHeight );
|
|
|
+
|
|
|
+
|
|
|
+ pBSEImageIn.SetImageData(BSEImage, iWidth, iHeight);
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
CIntRangeClr cIntRangeClr = new CIntRangeClr();
|
|
|
cIntRangeClr.SetStart(grayStart);
|
|
|
cIntRangeClr.SetEnd(grayEnd);
|
|
|
|
|
|
//ImageProForClr imgpro = new ImageProForClr();
|
|
|
- int num=0;
|
|
|
- imgProEngine.GetSpecialGrayRangeImage(pBSEImageIn, cIntRangeClr, pBSEImageOut,ref num);
|
|
|
+ int num = 0;
|
|
|
+ imgProEngine.GetSpecialGrayRangeImage(pBSEImageIn, cIntRangeClr, pBSEImageOut, ref num);
|
|
|
|
|
|
for (int i = 0; i < iWidth; i++)
|
|
|
{
|
|
|
@@ -175,24 +170,24 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
var originalBse = pBSEImageIn.GetBSEValue(i, j);
|
|
|
pBSEImageOut.SetBSEValue(i, j, originalBse);
|
|
|
}
|
|
|
- else
|
|
|
+ else
|
|
|
{
|
|
|
pBSEImageOut.SetBSEValue(i, j, 255);
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
|
|
|
BSEImageNoBG = pBSEImageOut.GetImageDataPtr();
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
- public bool GetColoredImage(COTSField tempFld, COTSImageProcParam ImgProcPrm, double pixelSize, int iWidth, int iHeight, ref CBSEImgClr a_pImgOut, ref Bitmap BSEImageNoBG)
|
|
|
+ public bool GetColoredImage(COTSField tempFld, COTSImageProcParam ImgProcPrm, double pixelSize, int iWidth, int iHeight, ref CBSEImgClr a_pImgOut, ref Bitmap BSEImageNoBG)
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
|
|
|
if (null == ImgProcPrm)
|
|
|
{
|
|
|
@@ -205,15 +200,15 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
return true;
|
|
|
}
|
|
|
// remove background
|
|
|
- private void RemoveBackGround(COTSField tempFld, COTSImageProcParam a_pImgProcessParam, double a_pixelSize, ref CBSEImgClr a_pImgOut)
|
|
|
+ private void RemoveBackGround(COTSField tempFld, COTSImageProcParam a_pImgProcessParam, double a_pixelSize, ref CBSEImgClr a_pImgOut)
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
|
|
|
List<COTSParticleClr> parts = new List<COTSParticleClr>();
|
|
|
List<COTSParticleClr> specialGreyparts = new List<COTSParticleClr>();
|
|
|
var ecdrange = a_pImgProcessParam.GetIncAreaRange();
|
|
|
var originalBse = tempFld.GetBSEImage();
|
|
|
-
|
|
|
+
|
|
|
if (!RemoveBGAndGetParts(tempFld, a_pImgProcessParam, ref parts))
|
|
|
{
|
|
|
return;
|
|
|
@@ -225,8 +220,8 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
{
|
|
|
var param = a_pImgProcessParam.GetSpecialGreyRangeParam();
|
|
|
var ranges = param.GetSpecialGreyRanges();
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
foreach (var r in ranges)
|
|
|
{
|
|
|
CIntRangeClr r1 = new CIntRangeClr();
|
|
|
@@ -239,7 +234,7 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
|
|
|
GetParticlesBySpecialGray(originalBse, r1, r2, a_pixelSize, ref specialGreyparts);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -270,10 +265,10 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
|
|
|
foreach (var p in parts)
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
foreach (var s in p.GetFeature().GetSegmentsList())
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
for (int i = s.GetStart(); i < s.GetStart() + s.GetLength(); i++)
|
|
|
{
|
|
|
var bseValue = originalBse.GetBSEValue(i, s.GetHeight());
|
|
|
@@ -283,9 +278,9 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
|
|
|
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
return;
|
|
|
}
|
|
|
private void RemoveBGAndGetColoredParts(COTSField tempFld, COTSImageProcParam a_pImgProcessParam, double a_pixelSize, ref CBSEImgClr a_pImgOut, ref Bitmap a_pBmpOut)
|
|
|
@@ -295,17 +290,44 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
List<COTSParticleClr> parts = new List<COTSParticleClr>();
|
|
|
List<COTSParticleClr> specialGreyparts = new List<COTSParticleClr>();
|
|
|
var ecdrange = a_pImgProcessParam.GetIncAreaRange();
|
|
|
-
|
|
|
- if (!RemoveBGAndGetParts(tempFld, a_pImgProcessParam, ref parts))
|
|
|
+
|
|
|
+ if (!RemoveBGAndGetParts(tempFld, a_pImgProcessParam, ref parts))
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
+ if (a_pImgProcessParam.GetBGRemoveType() == OTS_BGREMOVE_TYPE.WaterShed)
|
|
|
+ {
|
|
|
+ List<COTSParticleClr> allsubRegions = new List<COTSParticleClr>();
|
|
|
+
|
|
|
+
|
|
|
+ foreach (var p in parts)
|
|
|
+ {
|
|
|
+ if (p.GetSubParticles().Count > 0)
|
|
|
+ {
|
|
|
+ var subparts = p.GetSubParticles();
|
|
|
+ allsubRegions.AddRange(subparts);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ allsubRegions.Add(p);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ tempFld.SetListAnalysisParticles(allsubRegions);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ tempFld.SetListAnalysisParticles(parts);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
tempFld.CalParticleImageProp(parts);
|
|
|
+
|
|
|
tempFld.SelectParticlesAccordingImgProp(a_pImgProcessParam);
|
|
|
- parts = tempFld.GetListAnalysisParticles();
|
|
|
+ var Analysisparts = tempFld.GetListAnalysisParticles();
|
|
|
var a_pImgIn = tempFld.GetBSEImage();
|
|
|
Random c = new Random();
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
for (int i = 0; i < a_pBmpOut.Width; i++)
|
|
|
@@ -314,26 +336,26 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
{
|
|
|
a_pBmpOut.SetPixel(i, j, Color.White);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
|
|
|
|
|
|
- foreach (var p in parts)
|
|
|
+
|
|
|
+ foreach (var p in Analysisparts)
|
|
|
{
|
|
|
|
|
|
Color cValue = GetRandomColor30(c);
|
|
|
|
|
|
//Color cValue = p.GetTypeColor();
|
|
|
foreach (var s in p.GetFeature().GetSegmentsList())
|
|
|
- {
|
|
|
+ {
|
|
|
|
|
|
- for (int i = s.GetStart(); i < s.GetStart() + s.GetLength(); i++)
|
|
|
- {
|
|
|
+ for (int i = s.GetStart(); i < s.GetStart() + s.GetLength(); i++)
|
|
|
+ {
|
|
|
var y = s.GetHeight();
|
|
|
a_pBmpOut.SetPixel(i, y, cValue);
|
|
|
- }
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
@@ -373,22 +395,22 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
|
|
|
}
|
|
|
tempFld.CalParticleImageProp(specialGreyparts);
|
|
|
- parts.AddRange(specialGreyparts);
|
|
|
- tempFld.SetListAnalysisParticles(parts);
|
|
|
+ Analysisparts.AddRange(specialGreyparts);
|
|
|
+ tempFld.SetListAnalysisParticles(Analysisparts);
|
|
|
//tempFld.SelectParticlesAccordingImgProp(a_pImgProcessParam);
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
|
|
|
Graphics graphics = Graphics.FromImage(a_pBmpOut);
|
|
|
|
|
|
// 绘制一个红色矩形
|
|
|
Pen pen = new Pen(Color.Red, 1);
|
|
|
- var overlap=a_pImgProcessParam.GetOverlapParam();
|
|
|
+ var overlap = a_pImgProcessParam.GetOverlapParam();
|
|
|
if (overlap > 0)
|
|
|
{
|
|
|
int overlapPixels = (int)(overlap / a_pixelSize);
|
|
|
@@ -400,11 +422,11 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
|
|
|
return;
|
|
|
}
|
|
|
- public void GetImageWithBlackColoredParts(List<COTSParticleClr> analysitParts,COTSImageProcParam a_pImgProcessParam,double a_pixelSize, ref Bitmap a_pBmpOut)
|
|
|
+ public void GetImageWithBlackColoredParts(List<COTSParticleClr> analysitParts, COTSImageProcParam a_pImgProcessParam, double a_pixelSize, ref Bitmap a_pBmpOut)
|
|
|
{
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
for (int i = 0; i < a_pBmpOut.Width; i++)
|
|
|
@@ -470,7 +492,7 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
foreach (var p in analysitParts)
|
|
|
{
|
|
|
|
|
|
- Color cValue = ColorTranslator.FromHtml(p.GetTypeColor()) ;
|
|
|
+ Color cValue = ColorTranslator.FromHtml(p.GetTypeColor());
|
|
|
foreach (var s in p.GetFeature().GetSegmentsList())
|
|
|
{
|
|
|
|
|
|
@@ -499,7 +521,7 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
|
|
|
public Color GetRandomColor30(Random r)
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
int n = r.Next(0, 31);
|
|
|
|
|
|
Color ret_color;
|
|
|
@@ -606,21 +628,21 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
|
|
|
return ret_color;
|
|
|
}
|
|
|
-
|
|
|
|
|
|
- public bool CalParticleImageProp( COTSParticleClr part, double a_pixelSize)
|
|
|
+
|
|
|
+ public bool CalParticleImageProp(COTSParticleClr part, double a_pixelSize)
|
|
|
{
|
|
|
-
|
|
|
- imgProEngine.CalcuParticleImagePropertes(part,a_pixelSize);
|
|
|
-
|
|
|
+
|
|
|
+ imgProEngine.CalcuParticleImagePropertes(part, a_pixelSize);
|
|
|
+
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
|
|
|
|
|
|
- public bool RemoveBGAndGetParts(COTSField currentField, COTSImageProcParam a_pImgProcessParam,ref List<COTSParticleClr> parts)
|
|
|
+
|
|
|
+ public bool RemoveBGAndGetParts(COTSField currentField, COTSImageProcParam a_pImgProcessParam, ref List<COTSParticleClr> parts)
|
|
|
{
|
|
|
- var a_pixelSize = currentField.GetPixelSize();
|
|
|
+ var a_pixelSize = currentField.GetPixelSize();
|
|
|
COTSImgProcPrmClr prm = GetImageProcPrmClr(a_pImgProcessParam);
|
|
|
COTSFieldDataClr flddataclr = new COTSFieldDataClr();
|
|
|
PointF fldpos = currentField.GetOTSPosition();
|
|
|
@@ -632,46 +654,28 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
double right = fldpos.X + currentField.ImgWidth * a_pixelSize / 2;
|
|
|
double bottom = fldpos.Y - currentField.ImgHeight * a_pixelSize / 2;
|
|
|
flddataclr.SetOTSRect(left, top, right, bottom);
|
|
|
-
|
|
|
+
|
|
|
if (!imgProEngine.GetFieldDataFromImage(currentField.GetBSEImage(), prm, a_pixelSize, flddataclr))
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
List<COTSParticleClr> originalParts = flddataclr.GetParticleList();
|
|
|
-
|
|
|
- if(a_pImgProcessParam.GetBGRemoveType() == OTS_BGREMOVE_TYPE.WaterShed)
|
|
|
- {
|
|
|
- foreach(var p in originalParts)
|
|
|
- {
|
|
|
- if (p.GetSubParticles().Count > 0)
|
|
|
- {
|
|
|
- parts.AddRange(p.GetSubParticles());
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- parts.Add(p);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- currentField.SetListAnalysisParticles(parts);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- parts = originalParts;
|
|
|
- }
|
|
|
+ parts = originalParts;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
- currentField.SetListAnalysisParticles(parts);
|
|
|
-
|
|
|
-
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
- public bool GetParticlesBySpecialGray(CBSEImgClr img, CIntRangeClr grayrange, CDoubleRangeClr diameterRange,double a_pixelSize, ref List<COTSParticleClr> parts)
|
|
|
+ public bool GetParticlesBySpecialGray(CBSEImgClr img, CIntRangeClr grayrange, CDoubleRangeClr diameterRange, double a_pixelSize, ref List<COTSParticleClr> parts)
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
|
|
|
COTSFieldDataClr flddataclr = new COTSFieldDataClr();
|
|
|
- imgProEngine.GetParticlesBySpecialPartGrayRange(img, grayrange,diameterRange,a_pixelSize, flddataclr);
|
|
|
+ imgProEngine.GetParticlesBySpecialPartGrayRange(img, grayrange, diameterRange, a_pixelSize, flddataclr);
|
|
|
|
|
|
parts = flddataclr.GetParticleList();
|
|
|
return true;
|
|
|
@@ -689,14 +693,14 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
prmclr.SetParticleGray(r3);
|
|
|
prmclr.SetBGRemoveType((int)a_oSource.GetBGRemoveType());
|
|
|
prmclr.SetAutoBGRemoveType((int)a_oSource.GetAutoBGRemoveType());
|
|
|
-
|
|
|
+
|
|
|
prmclr.SetOverlapParam(a_oSource.GetOverlapParam());
|
|
|
prmclr.SetMatrixStep(a_oSource.MatrixStep);
|
|
|
-
|
|
|
+
|
|
|
|
|
|
return prmclr;
|
|
|
}
|
|
|
- public bool CalculateBrightnessDarkGrayByOtsu(CBSEImgClr bseimg, ref int bright, ref int dark)
|
|
|
+ public bool CalculateBrightnessDarkGrayByOtsu(CBSEImgClr bseimg, ref int bright, ref int dark)
|
|
|
{
|
|
|
if (bseimg == null || bseimg.GetImageDataPtr() == null)
|
|
|
{
|
|
|
@@ -704,10 +708,10 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
dark = 0;
|
|
|
return false;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
return imgProEngine.CalcuBrightPhaseDarkPhaseGrayByOtsu(bseimg, ref bright, ref dark);
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
public bool MergeBigBoundaryParticles(List<COTSField> allFields, double pixelSize, int scanFieldSize, System.Drawing.Size ResolutionSize, ref List<COTSParticleClr> mergedParts)
|
|
|
{
|
|
|
@@ -717,7 +721,7 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
COTSFieldDataClr fldclr = new COTSFieldDataClr();
|
|
|
PointF p1 = f.GetOTSPosition();
|
|
|
System.Drawing.Point p2 = new System.Drawing.Point((int)p1.X, (int)p1.Y);
|
|
|
- fldclr.SetPosition((int)p1.X,(int)p1.Y);
|
|
|
+ fldclr.SetPosition((int)p1.X, (int)p1.Y);
|
|
|
fldclr.SetImageWidth(f.ImgWidth);
|
|
|
fldclr.SetImageHeight(f.ImgHeight);
|
|
|
var parts = f.GetListAnalysisParticles();
|
|
|
@@ -727,13 +731,13 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
}
|
|
|
fldclrs.Add(fldclr);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
|
|
|
imgProEngine.MergeBigBoundaryParticles(fldclrs, pixelSize, scanFieldSize, ResolutionSize, mergedParts);
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 根据Segment寻找边缘坐标点
|
|
|
/// </summary>
|
|
|
@@ -743,7 +747,7 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
/// <returns></returns>
|
|
|
public static List<System.Drawing.Point> FindContoursBySegment(int width, int height, List<COTSSegmentClr> segmentClrs)
|
|
|
{
|
|
|
- List<System.Drawing.Point> points = new List<System.Drawing.Point>();
|
|
|
+ List<System.Drawing.Point> points = new List<System.Drawing.Point>();
|
|
|
using (Mat mat = new Mat(height, width, MatType.CV_8UC1, new Scalar(0)))
|
|
|
{
|
|
|
for (int i = 0; i < segmentClrs.Count; i++)
|
|
|
@@ -768,6 +772,6 @@ namespace OTSModelSharp.ServiceCenter
|
|
|
return points;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
}
|