public string LoadImage() { string res = ""; evtMultiStart(); System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog(); ofd.Filter = "Image Files (*.bmp,*.png,*.jpg,*.jpeg) | *.bmp;*.png;*.jpg;*.jpeg"; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { res = Task <string> .Run(() => { var tempimg = new Img(ofd.FileName); RatioW = tempimg.Width / 800.0; RatioH = tempimg.Height / 600.0; BaseImg = tempimg; //.Resize(800, 600, Inter.Area); ClrOriginalImg = new ColorImg(ofd.FileName); //.Resize(800, 600, Inter.Area); return(ofd.FileName); }).Result; } else { res = "NG"; } evtMultiEnd(); return(res); }
public ColorImg CenterDiffDraw(List <System.Drawing.Point> points, ColorImg img) { var line = new LineSegment2D(points[0], points[1]); img.Draw(line, new Bgr(153, 51, 153), 1); return(img); }
public string LoadImageFromDrop(string path) { var ext = Path.GetExtension(path); if (ext == ".bmp" || ext == ".png" || ext == ".jpg" || ext == ".jpeg") { BaseImg = null; var tempimg = new Img(path); RatioW = tempimg.Width / 800.0; RatioH = tempimg.Height / 600.0; BaseImg = tempimg; //.Resize(800, 600, Inter.Area); ClrOriginalImg = new ColorImg(path); //.Resize(800, 600, Inter.Area); return(path); } return("NG"); }
public void StartProcessing(Config cfg) { try { ClrImg = ClrOriginalImg.Copy(); if (BaseImg == null) { return; } var img = BaseImg.ThresholdBinary(new Gray(cfg.Threshold), new Gray(255)); //.SmoothMedian( (int)cfg.Resolution * 10 + 1 ); var contours = new VectorOfVectorOfPoint(); CvInvoke.FindContours(img, contours, null, Emgu.CV.CvEnum.RetrType.List, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxNone); var cntrColorSet = CreateContour_ColorSet(Outercolor, Innercolor, cfg, contours); List <VectorOfPoint> cntrlist = cntrColorSet.Item1; List <MCvScalar> colorlist = cntrColorSet.Item2; var centers = FindCenter(cntrlist); var centerlist = new List <double[]>(); double textRatio = Math.Pow(Math.E, RatioW); List <double[]> xys = new List <double[]>(); for (int i = 0; i < centers.Count(); i++) { CvInvoke.Circle(ClrImg, centers[i], 5, colorlist[i]); CvInvoke.Circle(ClrImg, centers[i], (int)(5 * RatioW), colorlist[i], thickness: RatioW > 1 ? (int)RatioW : 1); var realx = (centers[i].X * cfg.Resolution); var realy = (centers[i].Y * cfg.Resolution); centerlist.Add(new double[] { realx, realy }); } double xerror; double yerror; if (cfg.UseLine) { var slope1 = (cfg.HY2 - cfg.HY1) / (cfg.HX2 - cfg.HX1); var bias1 = cfg.HY1 - slope1 * cfg.HX1; var slope2 = (cfg.WY2 - cfg.WY1) / (cfg.WX2 - cfg.WX1); var bias2 = cfg.WY1 - slope2 * cfg.WX1; var crossx = (bias2 - bias1) / (slope1 - slope2); var crossy = slope1 * ((bias2 - bias1) / (slope1 - slope2)) + bias1; var posx = (int)(crossx * RatioW); var posy = (int)(crossy * RatioH); centers.Add(new System.Drawing.Point(posx, posy)); var crossx_cvs = crossx * RatioW * cfg.Resolution; var crossy_cvs = crossy * RatioH * cfg.Resolution; centerlist.Add(new double[] { crossx_cvs, crossy_cvs }); CvInvoke.Circle(ClrImg, centers.Last(), 5, colorlist.Last()); CvInvoke.Circle(ClrImg, centers.Last(), (int)(5 * RatioW), colorlist.Last(), thickness: RatioW > 1 ? (int)RatioW : 1); } xerror = Math.Abs(centerlist[0][0] - centerlist[1][0]); yerror = Math.Abs(centerlist[0][1] - centerlist[1][1]); string xyerror = string.Format("X Error : {0} , Y Error : {1}", xerror.ToString("F4"), yerror.ToString("F4")); System.Drawing.Point textposXY = new System.Drawing.Point(centers[0].X - (int)(40 * RatioW), centers[0].Y - (int)(10 * RatioH)); CvInvoke.PutText(ClrImg, xyerror, textposXY, FontFace.HersheySimplex, RatioW / 2.0, new MCvScalar(53, 251, 32), thickness: (int)(2 * RatioW)); double errorDistance = CalcDistance(centerlist); ClrImg = CenterDiffDraw(centers, ClrImg); System.Drawing.Point textdifpos = new System.Drawing.Point(centers[0].X + (int)(40 * RatioW), centers[0].Y + (int)(10 * RatioH)); CvInvoke.PutText(ClrImg, "Error : " + errorDistance.ToString("F4") + " (um)", textdifpos, FontFace.HersheySimplex, RatioW / 2.0, new MCvScalar(153, 51, 153), thickness: (int)(2 * RatioW)); var res = ToBitmapSource(ClrImg); evtProcessedImg(res); evtDistance(errorDistance); } catch (Exception er) { er.ToString().Print(); } }
// 각 이미지마다 해상도에 맞춰서 프로세싱 하자. public async void StartMultiProcessing(List <MultiAnalysisDatacs> srclist, Config cfg) { if (cfg == null) { System.Windows.MessageBox.Show(" Please Set Config First. "); } if (srclist.Count < 1) { return; } SaveFileDialog sfd = new SaveFileDialog(); sfd.Title = "Choose folder and header name of Result"; if (sfd.ShowDialog() == DialogResult.OK) { string basepath = Path.GetDirectoryName(sfd.FileName); string header = Path.GetFileName(sfd.FileName); Console.WriteLine(header); evtMultiStart(); await Task.Run(() => { for (int k = 0; k < srclist.Count; k++) { try { var path = srclist[k].fullname; BaseImg = new Img(path); RatioW = BaseImg.Width / 800.0; RatioH = BaseImg.Height / 600.0; //BaseImg = tempimg.Resize(800, 600, Inter.Area); ClrOriginalImg = new ColorImg(path);//.Resize(800, 600, Inter.Area); ClrImg = ClrOriginalImg.Copy(); // Processing var img = BaseImg.ThresholdBinary(new Gray(cfg.Threshold), new Gray(255)) .SmoothMedian((int)cfg.Resolution * 10 + 1); var contours = new VectorOfVectorOfPoint(); CvInvoke.FindContours(img, contours, null, Emgu.CV.CvEnum.RetrType.List, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxNone); var cntrColorSet = CreateContour_ColorSet(Outercolor, Innercolor, cfg, contours); List <VectorOfPoint> cntrlist = cntrColorSet.Item1; List <MCvScalar> colorlist = cntrColorSet.Item2; // Center var centers = FindCenter(cntrlist); var centerlist = new List <double[]>(); //Error for (int i = 0; i < centers.Count(); i++) { CvInvoke.Circle(ClrImg, centers[i], 5, colorlist[i]); var realx = (centers[i].X *RatioW *cfg.Resolution); var realy = (centers[i].Y *RatioH *cfg.Resolution); var x = realx.ToString(); var y = realy.ToString(); string xy = x + " , " + y + " (um)"; System.Drawing.Point textpos = new System.Drawing.Point(centers[i].X - 40 - (i * 25), centers[i].Y - 10 - (i * 25)); CvInvoke.PutText(ClrImg, xy, textpos, FontFace.HersheySimplex, 0.4, colorlist[i]); centerlist.Add(new double[] { realx, realy }); } double errorDistance = CalcDistance(centerlist); ClrImg = CenterDiffDraw(centers, ClrImg); System.Drawing.Point textdifpos = new System.Drawing.Point(centers[0].X + 40, centers[0].Y + 10); CvInvoke.PutText(ClrImg, "Error : " + errorDistance.ToString("F4") + " (um)", textdifpos, FontFace.HersheySimplex, 0.4, new MCvScalar(153, 51, 153)); var res = ToBitmapSource(ClrImg); var error = errorDistance.ToString("F4"); evtNumError(k, error); srclist[k].error = error; //Save Result ClrImg.Save(basepath + "\\" + k.ToString() + "_" + header + "_" + srclist[k].name); } catch (Exception er) { er.ToString().Print(); } } try { StringBuilder stv = new StringBuilder(); stv.AppendLine("Nnmber,FileName,Error"); foreach (var item in srclist) { stv.AppendLine(item.no.ToString() + "," + item.name + "," + item.error); } File.WriteAllText(sfd.FileName + "_Result.csv", stv.ToString()); } catch (Exception) { System.Windows.MessageBox.Show("Please check result file path or file is opened or not"); } }); evtMultiEnd(); } }