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.ServiceInterface { using OTSCLRINTERFACE; using OTSIMGPROC; using System.Drawing.Imaging; using System.Windows; public class CImageHandler:IImageProcess { #region 通过byte数组生成BMP图像文件 /// /// 将一个byte的数组转换为8bit灰度位图 /// /// 数组 /// 图像宽度 /// 图像高度 /// 位图 public static Bitmap ToGrayBitmap(byte[] data, int width, int height) { if (width == 0 || height == 0) return null; //// 申请目标位图的变量,并将其内存区域锁定 Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed); //// BitmapData这部分内容 需要 using System.Drawing.Imaging; BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); //// 获取图像参数 // 扫描线的宽度 int stride = bmpData.Stride; // 显示宽度与扫描线宽度的间隙 int offset = stride - width; // 获取bmpData的内存起始位置 IntPtr iptr = bmpData.Scan0; // 用stride宽度,表示这是内存区域的大小 int scanBytes = stride * height; //// 下面把原始的显示大小字节数组转换为内存中实际存放的字节数组 int posScan = 0; int posReal = 0;// 分别设置两个位置指针,指向源数组和目标数组 byte[] pixelValues = new byte[scanBytes]; //为目标数组分配内存 //for (int x = height-1;x>=0 ; x--) data[startIndex+ y];// for (int x = 0; x < height; x++) { int startIndex = x * width; //// 下面的循环节是模拟行扫描 for (int y = 0; y < width; y++) { pixelValues[posScan++] = data[posReal++]; } posScan += offset; //行扫描结束,要将目标位置指针移过那段“间隙” } //// 用Marshal的Copy方法,将刚才得到的内存字节数组复制到BitmapData中 System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, iptr, scanBytes); bmp.UnlockBits(bmpData); // 解锁内存区域 //// 下面的代码是为了修改生成位图的索引表,从伪彩修改为灰度 ColorPalette tempPalette; using (Bitmap tempBmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed)) { tempPalette = tempBmp.Palette; } for (int i = 0; i < 256; i++) { tempPalette.Entries[i] = Color.FromArgb(i, i, i); } bmp.Palette = tempPalette; return bmp; } #endregion //获取测量的BSE图 //COTSSample WSample: 工作样品测量 //Byte[] BSEImage: 带背景图数据 //int iHeight: 图像高度 //int iWidth: 图像宽度 //Byte[]BSEImageNoBG : 去背景图数据 public bool GetBSEImage(COTSImageProcParam ImgProcPrm, double pixelSize, byte[] BSEImage, int iHeight, int iWidth, ref byte[] BSEImageNoBG) { Rectangle rect = new Rectangle(); rect.Height = iHeight; rect.Width = iWidth; CBSEImgClr pBSEImageIn = new CBSEImgClr(rect); CBSEImgClr pBSEImageOut = new CBSEImgClr(rect); //pBSEImageIn.SetImageRect(rect); pBSEImageIn.SetImageData(BSEImage,iWidth, iHeight ); //pBSEImageOut.SetImageRect(rect); if (null == ImgProcPrm) { return false; } RemoveBackGround(pBSEImageIn, ImgProcPrm, pixelSize ,ref pBSEImageOut); BSEImageNoBG = pBSEImageOut.GetImageDataPtr(); return true; } /// /// 获取测量的BSE图 /// /// BSE原数据 /// 图像高度 /// 图像宽度 /// /// /// 去背景图数据 /// public bool GetBSEImage(byte[] BSEImage, int iHeight, int iWidth, int grayStart, int grayEnd, ref byte[] BSEImageNoBG) { Rectangle rect = new Rectangle(); rect.Height = iHeight; rect.Width = iWidth; CBSEImgClr pBSEImageIn = new CBSEImgClr(rect); CBSEImgClr pBSEImageOut = new CBSEImgClr(rect); //pBSEImageIn.SetImageRect(rect); pBSEImageIn.SetImageData(BSEImage, iWidth, iHeight ); CIntRangeClr cIntRangeClr = new CIntRangeClr(); cIntRangeClr.SetStart(grayStart); cIntRangeClr.SetEnd(grayEnd); OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr(); int num=0; imgpro.GetSpecialGrayRangeImage(pBSEImageIn, cIntRangeClr, pBSEImageOut,ref num); for (int i = 0; i < iWidth; i++) { for (int j = 0; j < iHeight; j++) { var bse = pBSEImageOut.GetBSEValue(i, j); if (bse == 255) { var originalBse = pBSEImageIn.GetBSEValue(i, j); pBSEImageOut.SetBSEValue(i, j, originalBse); } else { pBSEImageOut.SetBSEValue(i, j, 255); } } } BSEImageNoBG = pBSEImageOut.GetImageDataPtr(); return true; } // remove background public void RemoveBackGround(CBSEImgClr a_pImgIn, COTSImageProcParam a_pImgProcessParam, double a_pixelSize, ref CBSEImgClr a_pImgOut) { List parts = new List(); List specialGreyparts = new List(); if (!RemoveBGAndGetParts(a_pImgIn, a_pImgProcessParam, a_pixelSize, ref parts)) { return; } if (a_pImgProcessParam.GetSpecialGreyRangeParam().GetIsToRun()) { var param = a_pImgProcessParam.GetSpecialGreyRangeParam(); var ranges = param.GetSpecialGreyRanges(); foreach (var r in ranges) { CIntRangeClr r1 = new CIntRangeClr(); r1.SetStart(r.range.GetStart()); r1.SetEnd(r.range.GetEnd()); CDoubleRangeClr r2 = new CDoubleRangeClr(); r2.SetStart(r.diameterRange.GetStart()); r2.SetEnd(r.diameterRange.GetEnd()); GetParticlesBySpecialGray(a_pImgIn, r1, r2, a_pixelSize, ref specialGreyparts); } } for (int i = 0; i < a_pImgOut.GetWidth(); i++) { for (int j = 0; j < a_pImgOut.GetHeight(); j++) { a_pImgOut.SetBSEValue(i, j, 255); } } if (specialGreyparts.Count > 0) { foreach (var p in specialGreyparts) { foreach (var s in p.GetFeature().GetSegmentsList()) { for (int i = s.GetStart(); i < s.GetStart() + s.GetLength(); i++) { var bseValue = a_pImgIn.GetBSEValue(i, s.GetHeight()); a_pImgOut.SetBSEValue(i, s.GetHeight(), bseValue); } } } } 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 = a_pImgIn.GetBSEValue(i, s.GetHeight()); a_pImgOut.SetBSEValue(i, s.GetHeight(), bseValue); } } } return; } public void BDilate3(string source, string target, int wDegree, int rows, int columns) { throw new NotImplementedException(); } public void BErode3(string source, string target, int wDegree, int rows, int columns) { throw new NotImplementedException(); } public bool CalParticleImageProp( COTSParticleClr part, double a_pixelSize) { OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr(); imgpro.CalcuParticleImagePropertes(part,a_pixelSize); return true; } public bool RemoveBGAndGetParts(CBSEImgClr img, COTSImageProcParam a_pImgProcessParam,double a_pixelSize,ref List parts) { OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr(); OTSCLRINTERFACE.COTSImgProcPrmClr prm = GetImageProcPrmClr(a_pImgProcessParam); OTSCLRINTERFACE.COTSFieldDataClr flddataclr = new OTSCLRINTERFACE.COTSFieldDataClr(); if (!imgpro.GetFieldDataFromImage(img, prm, a_pixelSize, flddataclr)) { return false; } parts = flddataclr.GetParticleList(); return true; } public bool GetParticlesBySpecialGray(CBSEImgClr img, CIntRangeClr grayrange, CDoubleRangeClr diameterRange,double a_pixelSize, ref List parts) { OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr(); //OTSCLRINTERFACE.COTSImgProcPrmClr prm = GetImageProcPrmClr(a_pImgProcessParam); OTSCLRINTERFACE.COTSFieldDataClr flddataclr = new OTSCLRINTERFACE.COTSFieldDataClr(); imgpro.GetParticlesBySpecialPartGrayRange(img, grayrange,diameterRange,a_pixelSize, flddataclr); parts = flddataclr.GetParticleList(); return true; } private COTSImgProcPrmClr GetImageProcPrmClr(COTSImageProcParam a_oSource) { COTSImgProcPrmClr prmclr = new COTSImgProcPrmClr(); OTSCLRINTERFACE.CDoubleRangeClr r1 = new OTSCLRINTERFACE.CDoubleRangeClr(a_oSource.GetIncAreaRange().GetStart(), a_oSource.GetIncAreaRange().GetEnd()); prmclr.SetIncArea(r1); OTSCLRINTERFACE.CIntRangeClr r2= new OTSCLRINTERFACE.CIntRangeClr(a_oSource.GetBGGray().GetStart(), a_oSource.GetBGGray().GetEnd()); prmclr.SetBGGray(r2); OTSCLRINTERFACE.CIntRangeClr r3= new OTSCLRINTERFACE.CIntRangeClr(a_oSource.GetParticleGray().GetStart(), a_oSource.GetParticleGray().GetEnd()); prmclr.SetParticleGray(r3); prmclr.SetBGRemoveType((int)a_oSource.GetBGRemoveType()); prmclr.SetAutoBGRemoveType((int)a_oSource.GetAutoBGRemoveType()); prmclr.SetErrodDilateParam(a_oSource.GetErrodDilateParam()); return prmclr; } public bool MergeBigBoundaryParticles(List allFields, double pixelSize, int scanFieldSize, Size ResolutionSize, ref List mergedParts) { List fldclrs = new List(); foreach (var f in allFields) { COTSFieldDataClr fldclr = new COTSFieldDataClr(); PointF p1 = f.GetOTSPosition(); Point p2 = new Point((int)p1.X, (int)p1.Y); fldclr.SetPosition(p2); fldclr.SetImageWidth(f.Width); fldclr.SetImageHeight(f.Height); var parts = f.GetListAnalysisParticles(); foreach (var p in parts) { fldclr.AddParticle(p); } fldclrs.Add(fldclr); } OTSCLRINTERFACE.ImageProForClr imgpro = new OTSCLRINTERFACE.ImageProForClr(); imgpro.MergeBigBoundaryParticles(fldclrs, pixelSize, scanFieldSize, ResolutionSize, mergedParts); return true; } } }