private void task() { MotorControler mc = MotorControler.GetInstance(parameterManager); Surface sur = Surface.GetInstance(parameterManager); Camera camera = Camera.GetInstance(); Led led = Led.GetInstance(); CoordManager cm = new CoordManager(parameterManager); Vector3 InitPoint = mc.GetPoint(); Vector3 p = new Vector3(); List <Mat> image_set = new List <Mat>(); Vector3 ggg = mc.GetPoint(); string txtfileName = string.Format(@"c:\img\aaaaaa.txt"); StreamWriter twriter = File.CreateText(txtfileName); Vector3 initialpoint = mc.GetPoint(); camera.Start(); for (int r = 0; r < 10; r++) { for (int i = 0; i < 10; i++) { mc.MovePoint(initialpoint.X + 10 * i, initialpoint.Y + 10 * r, initialpoint.Z); mc.Join(); int ledbrightness = led.AdjustLight(parameterManager); int viewcounter = 0; bool flag = true; while (flag) { mc.MoveDistance(-0.003, VectorId.Z); mc.Join(); byte[] b = camera.ArrayImage; Mat src = new Mat(440, 512, MatType.CV_8U, b); Mat mat = src.Clone(); Cv2.GaussianBlur(mat, mat, Cv.Size(3, 3), -1); Mat gau = mat.Clone(); Cv2.GaussianBlur(gau, gau, Cv.Size(7, 7), -1); Cv2.Subtract(gau, mat, mat); Cv2.Threshold(mat, mat, 4, 1, ThresholdType.Binary); int brightness = Cv2.CountNonZero(mat); viewcounter++; if (brightness > 5000 || viewcounter > 100) { flag = false; } } Vector3 surfacepoint = mc.GetPoint(); for (int q = 0; q < 20; q++) { mc.MovePointZ(surfacepoint.Z - 0.03 + (0.003 * q)); mc.Join(); Vector3 nowpoint = mc.GetPoint(); byte[] b = camera.ArrayImage; Mat image = new Mat(440, 512, MatType.CV_8U, b); Mat clone_image = image.Clone(); //image_set.Add(clone_image); //char[] filename = new char[64]; //String.Format(filename,"gtrd%d.png" , i ); clone_image.ImWrite(String.Format(@"c:\img\gtrd_{0}_{1}_{2}.bmp", (int)(nowpoint.X), (int)(nowpoint.Y), q) ); // mc.MovePointY(currentpoint.Y - (0.180 - 0.01)); // mc.Join(); string stlog = ""; stlog += String.Format("{0} {1} {2} {3}\n", ledbrightness, nowpoint.X, nowpoint.Y, nowpoint.Z); twriter.Write(stlog); } } } camera.Stop(); twriter.Close(); /* * int viewcounter = 0; * * * string txtfileName = string.Format(@"{0}\{1}.txt", * direcotryPath, System.DateTime.Now.ToString("yyyyMMdd_HHmmss_ffff")); * StreamWriter twriter = File.CreateText(txtfileName); * * List<Vector3> PointList = new List<Vector3>(); * for (int xx = 0; xx < 5; xx++) { * for (int yy = 0; yy < 5; yy++) { * GridMark nearestMark = cm.GetTheNearestGridMark(new Vector3(InitPoint.X + xx * 10, InitPoint.Y + yy * 10, InitPoint.Z)); * PointList.Add(new Vector3(nearestMark.x, nearestMark.y, InitPoint.Z)); * } * } * * camera.Stop(); * * for (int pp = 0; pp < PointList.Count(); pp++) { * string stlog = ""; * int nshot = 20; * byte[] bb = new byte[440 * 512 * nshot]; * * mc.MovePoint(PointList[pp]); * mc.Join(); * * p = mc.GetPoint(); * double prev_z = p.Z; * DateTime starttime = System.DateTime.Now; * string datfileName = string.Format(@"{0}\{1}_x{2}_y{3}.dat", * direcotryPath, * starttime.ToString("yyyyMMdd_HHmmss_fff"), * (int)(p.X * 1000), * (int)(p.Y * 1000)); * BinaryWriter writer = new BinaryWriter(File.Open(datfileName, FileMode.Create)); * * * * while (viewcounter < nshot) { * mc.MoveDistance(-0.001, VectorId.Z); * mc.Join(); * byte[] b = Ipt.CaptureMain(); * p = mc.GetPoint(); * TimeSpan ts = System.DateTime.Now - starttime; * stlog += String.Format("{0} {1} {2} {3} {4} {5} {6} {7}\n", * pp, * System.DateTime.Now.ToString("HHmmss\\.fff"), * ts.ToString("s\\.fff"), * (p.X * 1000).ToString("0.0"), * (p.Y * 1000).ToString("0.0"), * (p.Z * 1000).ToString("0.0"), * (prev_z * 1000 - p.Z * 1000).ToString("0.0"), * viewcounter); * b.CopyTo(bb, 440 * 512 * viewcounter); * viewcounter++; * } * * viewcounter = 0; * twriter.Write(stlog); * writer.Write(bb); * writer.Flush(); * writer.Close(); * * } * * twriter.Close(); * camera.Start(); */ }
private void recogThread_Task() { Camera camera = Camera.GetInstance(); camera.Start(); MotorControler mc = MotorControler.GetInstance(parameterManager); surfaces[0] = UpTop; surfaces[1] = UpBottom; surfaces[2] = LowTop; surfaces[3] = LowBottom; // 現在位置が,前回の表面認識結果に基づいて下ゲル以下にいる場合は下ゲル下部から上方向に順に表面認識を行う. // 一方,ベース及び上ゲル以上にいる場合は上ゲル上部から下方向に順に表面認識を行う. // そのためにforループの条件式を設定する GelLayer layerNow = getWhichLayerNow(); int i, end; Action fomula; if (layerNow == GelLayer.Under || layerNow == GelLayer.UnderOut) { i = surfaces.Length; end = 0; fomula = (() => -- i); } else { i = 0; end = surfaces.Length; fomula = (() => ++ i); } for (i = 0; i < end; fomula()) { mc.MovePointZ(surfaces[i]); mc.Join(); bool presentResult = false, previousResult = IsInGel(); bool[] votes = new bool[parent.NumOfVoting]; int counter = 1; while (presentResult == previousResult) { previousResult = presentResult; mc.MovePointZ(surfaces[i] + everyRange * counter); while (mc.Moving) { for (int j = 0; j < votes.Length; ++j) { votes[i] = IsInGel(); Thread.Sleep(parent.DelayTime); } presentResult = Surface.Vote(votes); if (presentResult != previousResult) { mc.AbortMoving(); } } counter *= -1; counter += (counter > 0 ? 1 : 0); if (Math.Abs(everyRange * counter) > searchRange) { throw new SurfaceFailedException("Distance limit out."); } } surfaces[i] = mc.GetPoint().Z; // 境界面認識時のイベントを発生させる. ActivityEventArgs eventArgs = new ActivityEventArgs(); eventArgs.ZValue = surfaces[i]; switch (i) { case 3: if (LowBottomRecognized != null) { LowBottomRecognized(this, eventArgs); } break; case 2: if (LowTopRecognized != null) { LowTopRecognized(this, eventArgs); } break; case 1: if (UpBottomRecognized != null) { UpBottomRecognized(this, eventArgs); } break; case 0: if (UpTopRecognized != null) { UpTopRecognized(this, eventArgs); } break; } } dateTime = DateTime.Now; }
private void task() { System.Diagnostics.Stopwatch worktime = new System.Diagnostics.Stopwatch();//Stopwatch worktime.Start(); MotorControler mc = MotorControler.GetInstance(parameterManager); Camera camera = Camera.GetInstance(); TracksManager tm = parameterManager.TracksManager; Track myTrack = tm.GetTrack(tm.TrackingIndex); Surface surface = Surface.GetInstance(parameterManager); //for up layer double Sh = 0.5 / (surface.UpTop - surface.UpBottom); double tansi = Math.Sqrt(myTrack.MsDX * myTrack.MsDX + myTrack.MsDY * myTrack.MsDY); double theta = Math.Atan(tansi); double dz; double dz_price_img = (6 * Math.Cos(theta) / Sh) / 1000; double dz_img = dz_price_img * (-1); string datarootdirpath = string.Format(@"C:\MKS_test\{0}", myTrack.IdString);//Open forder to store track information System.IO.DirectoryInfo mydir = System.IO.Directory.CreateDirectory(datarootdirpath); List <OpenCvSharp.CPlusPlus.Point2d> Msdxdy = new List <OpenCvSharp.CPlusPlus.Point2d>(); Msdxdy.Add(new OpenCvSharp.CPlusPlus.Point2d(myTrack.MsDX, myTrack.MsDY)); List <OpenCvSharp.CPlusPlus.Point3d> LStage = new List <OpenCvSharp.CPlusPlus.Point3d>(); List <OpenCvSharp.CPlusPlus.Point> LPeak = new List <OpenCvSharp.CPlusPlus.Point>(); List <Point3d> LTrack = new List <Point3d>(); List <List <ImageTaking> > UpTrackInfo = new List <List <ImageTaking> >(); //for down layer................................................................ double Sh_low; Sh_low = 0.5 / (surface.LowTop - surface.LowBottom); List <OpenCvSharp.CPlusPlus.Point2d> Msdxdy_Low = new List <OpenCvSharp.CPlusPlus.Point2d>(); List <OpenCvSharp.CPlusPlus.Point3d> LStage_Low = new List <OpenCvSharp.CPlusPlus.Point3d>(); List <OpenCvSharp.CPlusPlus.Point> LPeak_Low = new List <OpenCvSharp.CPlusPlus.Point>(); List <Point3d> LTrack_Low = new List <Point3d>(); List <List <ImageTaking> > LowTrackInfo = new List <List <ImageTaking> >(); dz_price_img = (6 * Math.Cos(theta) / Sh) / 1000; dz_img = dz_price_img * (-1); dz = dz_img; int gotobase = 0; int not_detect = 0; for (int i = 0; gotobase < 1; i++) { Vector3 initialpos = mc.GetPoint(); double moverange = 9 * dz_img; double predpoint = moverange + initialpos.Z; if (predpoint < surface.UpBottom) { gotobase = 1; dz = surface.UpBottom - initialpos.Z + 9 * dz_price_img; } //gotobase = 1のときは、移動して画像を撮影するようにする。 if (i != 0) { Vector3 dstpoint = new Vector3( LTrack[i - 1].X + Msdxdy[i].X * dz * Sh, LTrack[i - 1].Y + Msdxdy[i].Y * dz * Sh, LTrack[i - 1].Z + dz ); mc.MovePoint(dstpoint); mc.Join(); } List <ImageTaking> LiITUpMid = TakeSequentialImage( //image taking Msdxdy[i].X * Sh, //Dx Msdxdy[i].Y * Sh, //Dy dz_img, //Dz 10); //number of images LStage.Add(new OpenCvSharp.CPlusPlus.Point3d(LiITUpMid[9].StageCoord.X, LiITUpMid[9].StageCoord.Y, LiITUpMid[9].StageCoord.Z)); LiITUpMid[9].img.ImWrite(datarootdirpath + string.Format(@"\img_l_up_{0}.bmp", i)); UpTrackInfo.Add(LiITUpMid); List <Mat> binimages = new List <Mat>(); for (int t = 0; t <= 9; t++) { Mat bin = (Mat)DogContrastBinalize(LiITUpMid[t].img); double xx = myTrack.MsDX * myTrack.MsDX; double yy = myTrack.MsDY * myTrack.MsDY; if (Math.Sqrt(xx + yy) >= 0.4) {//////////////////???????????????????? Cv2.Dilate(bin, bin, new Mat()); } Cv2.Dilate(bin, bin, new Mat()); binimages.Add(bin); } //trackを重ねる処理を入れる。 Point2d pixel_cen = TrackDetection(binimages, 256, 220, 3, 3, 4, 90, 6);//, true); //Point2d pixel_cen = TrackDetection(binimages, 256, 220, 3, 3, 4, 90, true); if (pixel_cen.X == -1 & pixel_cen.Y == -1) { mc.Join(); not_detect = 1; goto not_detect_track; } LPeak.Add(new OpenCvSharp.CPlusPlus.Point(pixel_cen.X - 256, pixel_cen.Y - 220)); double firstx = LStage[i].X - LPeak[i].X * 0.000267; double firsty = LStage[i].Y + LPeak[i].Y * 0.000267; double firstz = LStage[i].Z; LTrack.Add(new Point3d(firstx, firsty, firstz)); // //上側乳剤層上面の1回目のtrack検出によって、次のtrackの位置を検出する角度を求める。 //その角度が、1回目のtrack検出の結果によって大きな角度にならないように調整をする。 if (i == 0) { Msdxdy.Add(new OpenCvSharp.CPlusPlus.Point2d(myTrack.MsDX, myTrack.MsDY)); } else if (i == 1) { List <Point3d> LTrack_ghost = new List <Point3d>(); double dzPrev = (LStage[0].Z - surface.UpTop) * Sh; double Lghost_x = LTrack[0].X - Msdxdy[i].X * dzPrev; double Lghost_y = LTrack[0].Y - Msdxdy[i].Y * dzPrev; LTrack_ghost.Add(new Point3d(Lghost_x, Lghost_y, surface.UpTop));//上側乳剤層上面にtrackがあるならどの位置にあるかを算出する。 string txtfileName_ltrackghost = datarootdirpath + string.Format(@"\LTrack_ghost.txt"); StreamWriter twriter_ltrackghost = File.CreateText(txtfileName_ltrackghost); twriter_ltrackghost.WriteLine("{0} {1} {2}", LTrack_ghost[0].X, LTrack_ghost[0].Y, LTrack_ghost[0].Z); twriter_ltrackghost.Close(); OpenCvSharp.CPlusPlus.Point2d Tangle = ApproximateStraight(Sh, LTrack_ghost[0], LTrack[0], LTrack[1]); Msdxdy.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle.X, Tangle.Y)); } else { OpenCvSharp.CPlusPlus.Point2d Tangle = ApproximateStraight(Sh, LTrack[i - 2], LTrack[i - 1], LTrack[i]); Msdxdy.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle.X, Tangle.Y)); } }//for i-loop //baseまたぎ int ltrack_counter = LTrack.Count(); int msdxdy_counter = Msdxdy.Count(); mc.MovePoint( LTrack[ltrack_counter - 1].X + Msdxdy[msdxdy_counter - 1].X * (surface.LowTop - surface.UpBottom), LTrack[ltrack_counter - 1].Y + Msdxdy[msdxdy_counter - 1].Y * (surface.LowTop - surface.UpBottom), surface.LowTop ); mc.Join(); //////ここから下gelの処理 Msdxdy_Low.Add(new OpenCvSharp.CPlusPlus.Point2d(Msdxdy[msdxdy_counter - 1].X, Msdxdy[msdxdy_counter - 1].Y)); //今までのtrack追跡プログラムとは異なる角度等の使い方をする。 dz_price_img = (6 * Math.Cos(theta) / Sh_low) / 1000; dz_img = dz_price_img * (-1); dz = dz_img; int goto_dgel = 0; for (int i = 0; goto_dgel < 1; i++) { ///////移動して画像処理をしたときに、下gelの下に入らないようにする。 Vector3 initialpos = mc.GetPoint(); double moverange = 9 * dz_img; double predpoint = moverange + initialpos.Z; if (predpoint < surface.LowBottom)//もしもbaseに入りそうなら、8枚目の画像がちょうど下gelを撮影するようにdzを調整する。 { goto_dgel = 1; dz = surface.LowBottom - initialpos.Z + 9 * dz_price_img; } //////// //goto_dgel == 1のときは、移動して画像を撮影するようにする。 if (i != 0) { Vector3 dstpoint = new Vector3( LTrack_Low[i - 1].X + Msdxdy_Low[i].X * dz * Sh_low, LTrack_Low[i - 1].Y + Msdxdy_Low[i].Y * dz * Sh_low, LTrack_Low[i - 1].Z + dz ); mc.MovePoint(dstpoint); mc.Join(); } //今までのtrack追跡プログラムとは異なる角度等の使い方をする。 List <ImageTaking> LiITLowMid = TakeSequentialImage( Msdxdy[i].X * Sh_low, Msdxdy[i].Y * Sh_low, dz_img, 10); //画像・座標の記録 LStage_Low.Add(new OpenCvSharp.CPlusPlus.Point3d(LiITLowMid[9].StageCoord.X, LiITLowMid[9].StageCoord.Y, LiITLowMid[9].StageCoord.Z)); LiITLowMid[9].img.ImWrite(datarootdirpath + string.Format(@"\img_l_low_{0}.png", i)); LowTrackInfo.Add(LiITLowMid);//撮影した8枚の画像と、撮影した位置を記録する。 //撮影した画像をここで処理する。 List <Mat> binimages = new List <Mat>(); for (int t = 0; t <= 9; t++) { Mat bin = (Mat)DogContrastBinalize(LiITLowMid[t].img); double xx = myTrack.MsDX * myTrack.MsDX; double yy = myTrack.MsDY * myTrack.MsDY; if (Math.Sqrt(xx + yy) >= 0.4) { Cv2.Dilate(bin, bin, new Mat()); } Cv2.Dilate(bin, bin, new Mat()); binimages.Add(bin); } //trackを重ねる処理を入れる。 Point2d pixel_cen = TrackDetection(binimages, 256, 220, 3, 3, 4, 90, 6);//, true);//画像の8枚目におけるtrackのpixel座標を算出する。 //もし検出に失敗した場合はループを抜ける。 if (pixel_cen.X == -1 & pixel_cen.Y == -1) { //mc.MovePoint(LTrack_Low[i - 1].X, LTrack_Low[i - 1].Y, LTrack_Low[i - 1].Z); mc.Join(); not_detect = 1; goto not_detect_track; } // //検出したpixel座標をstage座標に変換するなどlistに追加する。 LPeak_Low.Add(new OpenCvSharp.CPlusPlus.Point(pixel_cen.X - 256, pixel_cen.Y - 220)); double firstx = LStage_Low[i].X - LPeak_Low[i].X * 0.000267; double firsty = LStage_Low[i].Y + LPeak_Low[i].Y * 0.000267; double firstz = LStage_Low[i].Z; LTrack_Low.Add(new Point3d(firstx, firsty, firstz)); // //ここからは、最小二乗法で角度を算出するプログラムである。 //上側乳剤層上面の1回目のtrack検出によって、次のtrackの位置を検出する角度を求める。 //その角度が、1回目のtrack検出の結果によって大きな角度にならないように調整をする。 if (i == 0) { OpenCvSharp.CPlusPlus.Point2d Tangle_l = ApproximateStraightBase(Sh, Sh_low, LTrack[ltrack_counter - 2], LTrack[ltrack_counter - 1], LTrack_Low[i], surface); Msdxdy_Low.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle_l.X, Tangle_l.Y)); } else if (i == 1) { OpenCvSharp.CPlusPlus.Point2d Tangle_l = ApproximateStraightBase(Sh, Sh_low, LTrack[ltrack_counter - 1], LTrack_Low[i - 1], LTrack_Low[i], surface); Msdxdy_Low.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle_l.X, Tangle_l.Y)); } else { OpenCvSharp.CPlusPlus.Point2d Tangle_l = ApproximateStraight(Sh_low, LTrack_Low[i - 2], LTrack_Low[i - 1], LTrack_Low[i]); Msdxdy_Low.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle_l.X, Tangle_l.Y)); } }//i_loop // int ltrack_low_count = LTrack_Low.Count(); mc.MovePointXY(LTrack_Low[ltrack_low_count - 1].X, LTrack_Low[ltrack_low_count - 1].Y); mc.Join(); //検出に失敗した場合は、ループを抜けてここに来る。 not_detect_track :;//検出に失敗したと考えられる地点で画像を取得し、下ゲル下面まで移動する。(現在は下ゲル下面とするが、今後変更する可能性有。) if (not_detect != 0) { //写真撮影 List <ImageTaking> NotDetect = TakeSequentialImage( Msdxdy[0].X * Sh, Msdxdy[0].Y * Sh, 0, 1); string txtfileName_t_not_detect = datarootdirpath + string.Format(@"\not_detect.txt"); StreamWriter twriter_t_not_detect = File.CreateText(txtfileName_t_not_detect); for (int i = 0; i < NotDetect.Count; i++) { NotDetect[i].img.ImWrite(datarootdirpath + string.Format(@"\img_t_not_detect.bmp")); Vector3 p = NotDetect[i].StageCoord; twriter_t_not_detect.WriteLine("{0} {1} {2} {3}", i, p.X, p.Y, p.Z); } twriter_t_not_detect.Close(); mc.MovePointZ(surface.LowBottom); mc.Join(); } //file write out up_gel string txtfileName_surface = datarootdirpath + string.Format(@"\surface.txt"); StreamWriter twriter_surface = File.CreateText(txtfileName_surface); twriter_surface.WriteLine("{0} {1} {2} {3}", surface.UpTop, surface.UpBottom, surface.LowTop, surface.LowBottom); twriter_surface.Close(); string txtfileName_sh_up = datarootdirpath + string.Format(@"\Sh_up.txt"); StreamWriter twriter_sh_up = File.CreateText(txtfileName_sh_up); twriter_sh_up.WriteLine("{0}", Sh); twriter_sh_up.Close(); //file write out string txtfileName_t_info_up = datarootdirpath + string.Format(@"\location_up.txt"); StreamWriter twriter_t_info_up = File.CreateText(txtfileName_t_info_up); for (int i = 0; i < UpTrackInfo.Count; i++) { for (int t = 0; t < UpTrackInfo[i].Count; t++) { UpTrackInfo[i][t].img.ImWrite(datarootdirpath + string.Format(@"\img_t_info_up_{0}-{1}.bmp", i, t)); Vector3 p = UpTrackInfo[i][t].StageCoord; twriter_t_info_up.WriteLine("{0} {1} {2} {3} {4}", i, t, p.X, p.Y, p.Z); } } twriter_t_info_up.Close(); string txtfileName_lpeak = datarootdirpath + string.Format(@"\lpeak_up.txt"); StreamWriter twriter_lpeak = File.CreateText(txtfileName_lpeak); for (int i = 0; i < LPeak.Count(); i++) { OpenCvSharp.CPlusPlus.Point p = LPeak[i]; twriter_lpeak.WriteLine("{0} {1} {2}", i, p.X, p.Y); } twriter_lpeak.Close(); string txtfileName_ltrack = datarootdirpath + string.Format(@"\ltrack_up.txt"); StreamWriter twriter_ltrack = File.CreateText(txtfileName_ltrack); for (int i = 0; i < LTrack.Count(); i++) { OpenCvSharp.CPlusPlus.Point3d p = LTrack[i]; twriter_ltrack.WriteLine("{0} {1} {2} {3}", i, p.X, p.Y, p.Z); } twriter_ltrack.Close(); string txtfileName_msdxdy = datarootdirpath + string.Format(@"\msdxdy.txt"); StreamWriter twriter_msdxdy = File.CreateText(txtfileName_msdxdy); for (int i = 0; i < Msdxdy.Count(); i++) { OpenCvSharp.CPlusPlus.Point2d p = Msdxdy[i]; twriter_msdxdy.WriteLine("{0} {1} {2}", i, p.X, p.Y); } twriter_msdxdy.Close(); //file write out low_gel string txtfileName_sh_low = datarootdirpath + string.Format(@"\Sh_low.txt"); StreamWriter twriter_sh_low = File.CreateText(txtfileName_sh_low); twriter_sh_low.WriteLine("{0}", Sh_low); twriter_sh_low.Close(); string txtfileName_t_info_low = datarootdirpath + string.Format(@"\location_low.txt"); StreamWriter twriter_t_info_low = File.CreateText(txtfileName_t_info_low); for (int i = 0; i < LowTrackInfo.Count; i++) { for (int t = 0; t < LowTrackInfo[i].Count; t++) { LowTrackInfo[i][t].img.ImWrite(datarootdirpath + string.Format(@"\img_t_info_low_{0}-{1}.bmp", i, t)); Vector3 p = LowTrackInfo[i][t].StageCoord; twriter_t_info_low.WriteLine("{0} {1} {2} {3} {4}", i, t, p.X, p.Y, p.Z); } } twriter_t_info_low.Close(); string txtfileName_lpeak_low = datarootdirpath + string.Format(@"\lpeak_low.txt"); StreamWriter twriter_lpeak_low = File.CreateText(txtfileName_lpeak_low); for (int i = 0; i < LPeak_Low.Count(); i++) { OpenCvSharp.CPlusPlus.Point p = LPeak_Low[i]; twriter_lpeak_low.WriteLine("{0} {1} {2}", i, p.X, p.Y); } twriter_lpeak_low.Close(); string txtfileName_ltrack_low = datarootdirpath + string.Format(@"\ltrack_low.txt"); StreamWriter twriter_ltrack_low = File.CreateText(txtfileName_ltrack_low); for (int i = 0; i < LTrack_Low.Count(); i++) { OpenCvSharp.CPlusPlus.Point3d p = LTrack_Low[i]; twriter_ltrack_low.WriteLine("{0} {1} {2} {3}", i, p.X, p.Y, p.Z); } twriter_ltrack_low.Close(); string txtfileName_msdxdy_low = datarootdirpath + string.Format(@"\msdxdy_low.txt"); StreamWriter twriter_msdxdy_low = File.CreateText(txtfileName_msdxdy_low); for (int i = 0; i < Msdxdy_Low.Count(); i++) { OpenCvSharp.CPlusPlus.Point2d p = Msdxdy_Low[i]; twriter_msdxdy_low.WriteLine("{0} {1} {2}", i, p.X, p.Y); } twriter_msdxdy_low.Close(); worktime.Stop(); string txtfileName_time_log = datarootdirpath + string.Format(@"\time_log.txt"); StreamWriter twriter_time_log = File.CreateText(txtfileName_time_log); twriter_time_log.WriteLine("{0} {1} {2}", myTrack.MsDX, myTrack.MsDY, worktime.Elapsed); twriter_time_log.Close(); GoTopUp(); mc.Join(); }
private void BeamDetection(string outputfilename, bool isup) {// beam Detection int BinarizeThreshold = 60; int BrightnessThreshold = 4; int nop = 7; double dz = 0; if (isup == true) { dz = -0.003; } else { dz = 0.003; } Camera camera = Camera.GetInstance(); MotorControler mc = MotorControler.GetInstance(parameterManager); Vector3 InitPoint = mc.GetPoint(); Vector3 p = new Vector3(); TracksManager tm = parameterManager.TracksManager; int mod = parameterManager.ModuleNo; int pl = parameterManager.PlateNo; Track myTrack = tm.GetTrack(tm.TrackingIndex); string[] sp = myTrack.IdString.Split('-'); //string datfileName = string.Format("{0}.dat", System.DateTime.Now.ToString("yyyyMMdd_HHmmss")); string datfileName = string.Format(@"c:\test\bpm\{0}\{1}-{2}-{3}-{4}-{5}.dat", mod, mod, pl, sp[0], sp[1], System.DateTime.Now.ToString("ddHHmmss")); BinaryWriter writer = new BinaryWriter(File.Open(datfileName, FileMode.Create)); byte[] bb = new byte[440 * 512 * nop]; string fileName = string.Format("{0}", outputfilename); StreamWriter twriter = File.CreateText(fileName); string stlog = ""; List <ImageTaking> LiIT = TakeSequentialImage(0.0, 0.0, dz, nop); Mat sum = Mat.Zeros(440, 512, MatType.CV_8UC1); for (int i = 0; i < LiIT.Count; i++) { Mat bin = (Mat)DogContrastBinalize(LiIT[i].img, 31, BinarizeThreshold); Cv2.Add(sum, bin, sum); //byte[] b = LiIT[i].img.ToBytes();//format is .png MatOfByte mob = new MatOfByte(LiIT[i].img); byte[] b = mob.ToArray(); b.CopyTo(bb, 440 * 512 * i); } mc.MovePointZ(InitPoint.Z); mc.Join(); Cv2.Threshold(sum, sum, BrightnessThreshold, 1, ThresholdType.Binary); //Cv2.FindContoursをつかうとAccessViolationExceptionになる(Release/Debug両方)ので、C-API風に書く using (CvMemStorage storage = new CvMemStorage()) { using (CvContourScanner scanner = new CvContourScanner(sum.ToIplImage(), storage, CvContour.SizeOf, ContourRetrieval.Tree, ContourChain.ApproxSimple)) { //string fileName = string.Format(@"c:\img\{0}.txt", // System.DateTime.Now.ToString("yyyyMMdd_HHmmss_fff")); foreach (CvSeq <CvPoint> c in scanner) { CvMoments mom = new CvMoments(c, false); if (c.ElemSize < 2) { continue; } if (mom.M00 == 0.0) { continue; } double mx = mom.M10 / mom.M00; double my = mom.M01 / mom.M00; stlog += string.Format("{0:F} {1:F}\n", mx, my); } } } twriter.Write(stlog); twriter.Close(); writer.Write(bb); writer.Flush(); writer.Close(); sum *= 255; sum.ImWrite(String.Format(@"c:\img\{0}_{1}_{2}.bmp", System.DateTime.Now.ToString("yyyyMMdd_HHmmss"), (int)(p.X * 1000), (int)(p.Y * 1000))); }//BeamDetection
/// <summary> /// 撮影処理を行います. /// <para>別スレッドのタスクとして実行してください.直接呼び出さないでください.</para> /// </summary> private void shootingThreadTask() { if (Started != null) { Started(this, new EventArgs()); } imagesUri = new List <string>(); shotPoint = new List <double>(); MotorControler mc = MotorControler.GetInstance(parameterManager); Camera camera = Camera.GetInstance(); camera.Start(); numOfShot = 0; // 撮影開始地点へ移動する mc.MovePointZ(startPoint); mc.Join(); StreamWriter writer = new StreamWriter(string.Format(@"{0}\{1}.txt", filepath, filenameprefix)); // 撮影終了地点に移動しながら画像を確保する double pnInterval = (startPoint > endPoint ? -interval : interval); double presentPoint = mc.GetPoint().Z; while (isShootContinue(presentPoint)) { mc.MoveDistance(pnInterval, VectorId.Z); mc.Join(); presentPoint = mc.GetPoint().Z; byte[] b = camera.ArrayImage; Mat src = new Mat(440, 512, MatType.CV_8U, b); string pngfileName = string.Format(@"{0}\{1}{2:000}.png", filepath, filenameprefix, numOfShot); src.ImWrite(pngfileName); shotPoint.Add(presentPoint); writer.WriteLine(presentPoint.ToString()); ++numOfShot; if (OnShort != null) { ActivityEventArgs e = new ActivityEventArgs(); e.ZValue = presentPoint; OnShort(this, e); } #if false//20140225 yokoyama,jyoshida mc.MoveDistance(pnInterval, VectorId.Z, delegate { presentPoint = mc.GetPoint().Z; Camera c = Camera.GetInstance(); saveTemp(c.Image); shotPoint.Add(presentPoint); ++numOfShot; if (OnShort != null) { ActivityEventArgs e = new ActivityEventArgs(); e.ZValue = presentPoint; OnShort(this, e); } }); #endif }//while writer.Close(); mc.SlowDownStop(VectorId.Z); }
}//BeamDetection private void task() { MotorControler mc = MotorControler.GetInstance(parameterManager); Camera camera = Camera.GetInstance(); TracksManager tm = parameterManager.TracksManager; Track myTrack = tm.GetTrack(tm.TrackingIndex); Surface surface = Surface.GetInstance(parameterManager); Stopwatch sw = new Stopwatch(); CoordManager coordManager = new CoordManager(parameterManager); //CoordManager coordManager; int mod = parameterManager.ModuleNo; int pl = parameterManager.PlateNo; sw.Start(); try { //MotorControler mc = MotorControler.GetInstance(parameterManager); GridMark nearestMark = coordManager.GetTheNearestGridMark(mc.GetPoint()); System.Diagnostics.Debug.WriteLine(String.Format("{0}, {1}", nearestMark.x, nearestMark.y)); mc.MovePointXY(nearestMark.x, nearestMark.y); mc.Join(); } catch (GridMarkNotFoundException ex) { System.Diagnostics.Debug.WriteLine(String.Format("{0}", ex.ToString())); } try { // MotorControler mc = MotorControler.GetInstance(parameterManager); IGridMarkRecognizer GridMarkRecognizer = coordManager; mc.SetSpiralCenterPoint(); Led led = Led.GetInstance(); Vector2 encoderPoint = new Vector2(-1, -1); encoderPoint.X = mc.GetPoint().X; encoderPoint.Y = mc.GetPoint().Y;//おこられたのでしかたなくこうする 吉田20150427 Vector2 viewerPoint = new Vector2(-1, -1); bool continueFlag = true; while (continueFlag) { led.AdjustLight(parameterManager); viewerPoint = GridMarkRecognizer.SearchGridMarkx50(); if (viewerPoint.X < 0 || viewerPoint.Y < 0) { System.Diagnostics.Debug.WriteLine(String.Format("grid mark not found")); mc.MoveInSpiral(true); mc.Join(); continueFlag = (mc.SpiralIndex < 30); } else { System.Diagnostics.Debug.WriteLine(String.Format("******** {0} {1}", viewerPoint.X, viewerPoint.Y)); encoderPoint = coordManager.TransToEmulsionCoord(viewerPoint); mc.MovePointXY(encoderPoint); mc.Join(); continueFlag = false; } } // while //重心検出と移動を2回繰り返して、グリッドマークを視野中心にもっていく mc.MovePointXY(encoderPoint); mc.Join(); viewerPoint = GridMarkRecognizer.SearchGridMarkx50(); encoderPoint = coordManager.TransToEmulsionCoord(viewerPoint); mc.MovePointXY(encoderPoint); mc.Join(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("exception"); } //.........Get value of HFDX and HFDY................// try { Vector3 CurrentCenterPoint = mc.GetPoint(); GridMark nearestMark = coordManager.GetTheNearestGridMark(CurrentCenterPoint); System.Diagnostics.Debug.WriteLine(String.Format("{0}, {1}", CurrentCenterPoint.X, CurrentCenterPoint.Y)); coordManager.HFDX = CurrentCenterPoint.X - nearestMark.x; coordManager.HFDY = CurrentCenterPoint.Y - nearestMark.y; } catch (EntryPointNotFoundException ex) { MessageBox.Show("エントリポイントが見当たりません。 " + ex.Message); System.Diagnostics.Debug.WriteLine("エントリポイントが見当たりません。 " + ex.Message); } //....... Move to track place after revised with shift value.....// double dstx = myTrack.MsX + coordManager.HFDX; double dsty = myTrack.MsY + coordManager.HFDY; mc.MovePointXY(dstx, dsty, delegate { //stage.WriteLine(Properties.Strings.MovingComplete); }); mc.Join(); Led led_ = Led.GetInstance(); led_.AdjustLight(parameterManager); Thread.Sleep(500); //Wait for 5s //////////////////////////////Surfacerecog/////////////////////////////////////...................................///////// try { if (mc.IsMoving) { MessageBoxResult r = MessageBox.Show( Properties.Strings.SurfaceException01, Properties.Strings.Abort + "?", MessageBoxButton.YesNo); if (r == MessageBoxResult.Yes) { mc.AbortMoving(); } else { return; } } //Surface surface = Surface.GetInstance(parameterManager); if (surface.IsActive) { MessageBoxResult r = MessageBox.Show( Properties.Strings.SurfaceException02, Properties.Strings.Abort + "?", MessageBoxButton.YesNo); if (r == MessageBoxResult.Yes) { surface.Abort(); } else { return; } } try { surface.Start(true); } catch (Exception ex) { MessageBox.Show(ex.Message, Properties.Strings.Error); } } catch (Exception ex) { MessageBox.Show(ex.Message, Properties.Strings.Error); } mc.Join(); //...............Beam Pattern Matching..................//////////////// try { string datarootdirpath = string.Format(@"C:\test\bpm\{0}", mod); System.IO.DirectoryInfo mydir = System.IO.Directory.CreateDirectory(datarootdirpath); string[] sp = myTrack.IdString.Split('-'); string uptxt = string.Format(@"c:\test\bpm\{0}\{1}-{2}-{3}-{4}_up.txt", mod, mod, pl, sp[0], sp[1]); string dwtxt = string.Format(@"c:\test\bpm\{0}\{1}-{2}-{3}-{4}_dw.txt", mod, mod, pl - 1, sp[0], sp[1]); BeamDetection(uptxt, true); BeamPatternMatch bpm = new BeamPatternMatch(8, 200); bpm.ReadTrackDataTxtFile(dwtxt, false); bpm.ReadTrackDataTxtFile(uptxt, true); bpm.DoPatternMatch(); //stage.WriteLine(String.Format("pattern match dx,dy = {0}, {1}", bpm.GetPeakX() * 0.2625 * 0.001, bpm.GetPeakY() * 0.2625 * 0.001)); Vector3 BfPoint = mc.GetPoint(); mc.MoveDistance(bpm.GetPeakX() * 0.2625 * 0.001, VectorId.X); mc.Join(); mc.MoveDistance(-bpm.GetPeakY() * 0.2625 * 0.001, VectorId.Y); mc.Join(); Led led = Led.GetInstance(); led.AdjustLight(parameterManager); Vector3 AfPoint = mc.GetPoint(); //stage.WriteLine(String.Format("Move dx,dy = {0}, {1}", BfPoint.X - AfPoint.X, BfPoint.Y - AfPoint.Y)); } catch (ArgumentOutOfRangeException) { MessageBox.Show("ID numver is not existed。 "); } catch (System.Exception) { MessageBox.Show("No beam battern。 "); } // } /////////////////////////////SurfaceRecog//////////////////////////////////////////////////////////////////////////// Vector3 initial = mc.GetPoint();///////initialpoint double Sh = 0.5 / (surface.UpTop - surface.UpBottom); //ここから角度によって撮影間隔を変更するように書き換える。 double tansi = Math.Sqrt(myTrack.MsDX * myTrack.MsDX + myTrack.MsDY * myTrack.MsDY); double theta = Math.Atan(tansi); //絶対値の大きさを入れる。dzはマイナスの値になるようにする。 double dz; double dz_price_img = (6 * Math.Cos(theta) / Sh) / 1000; double dz_img = dz_price_img * (-1); // string datarootdirpathw = string.Format(@"C:\test\{0}", myTrack.IdString); System.IO.DirectoryInfo mydir_ = System.IO.Directory.CreateDirectory(datarootdirpathw); // //必要なlistをまとめる //List<ImageTaking> LiITUpTrack = new List<ImageTaking>(); List <OpenCvSharp.CPlusPlus.Point2d> Msdxdy = new List <OpenCvSharp.CPlusPlus.Point2d>(); Msdxdy.Add(new OpenCvSharp.CPlusPlus.Point2d(myTrack.MsDX, myTrack.MsDY)); List <OpenCvSharp.CPlusPlus.Point3d> LStage = new List <OpenCvSharp.CPlusPlus.Point3d>(); List <OpenCvSharp.CPlusPlus.Point> LPeak = new List <OpenCvSharp.CPlusPlus.Point>(); //i番目の画像で実際に見つかったトラックの座標。pixel座標で視野中心からの差分。 List <Point3d> LTrack = new List <Point3d>(); //i番目の画像で実際に見つかったトラックの座標のステージ座標 List <List <ImageTaking> > UpTrackInfo = new List <List <ImageTaking> >(); //エラー防止のために下ゲルの処理の際に必要なListをここに移動した。 double Sh_low; Sh_low = 0.5 / (surface.LowTop - surface.LowBottom); List <OpenCvSharp.CPlusPlus.Point2d> Msdxdy_Low = new List <OpenCvSharp.CPlusPlus.Point2d>(); //List<ImageTaking> LiITLowMid = new List<ImageTaking>(); List <OpenCvSharp.CPlusPlus.Point3d> LStage_Low = new List <OpenCvSharp.CPlusPlus.Point3d>(); List <OpenCvSharp.CPlusPlus.Point> LPeak_Low = new List <OpenCvSharp.CPlusPlus.Point>(); //i番目の画像で実際に見つかったトラックの座標。pixel座標で視野中心からの差分。 List <Point3d> LTrack_Low = new List <Point3d>(); //i番目の画像で実際に見つかったトラックの座標のステージ座標 List <List <ImageTaking> > LowTrackInfo = new List <List <ImageTaking> >(); //今までのtrack追跡プログラムとは異なる角度等の使い方をする。 dz_price_img = (6 * Math.Cos(theta) / Sh) / 1000; dz_img = dz_price_img * (-1); dz = dz_img; int gotobase = 0; int not_detect = 0; for (int i = 0; gotobase < 1; i++) { ///////移動して画像処理をしたときに、baseの中に入らないようにする。 Vector3 initialpos = mc.GetPoint(); double moverange = 7 * dz_img; double predpoint = moverange + initialpos.Z; if (predpoint < surface.UpBottom)//もしもbaseに入りそうなら、8枚目の画像がちょうどbaseを撮影するようにdzを調整する。 { gotobase = 1; dz = surface.UpBottom - initialpos.Z + 7 * dz_price_img; } //////// //gotobase = 1のときは、移動して画像を撮影するようにする。 if (i != 0) { Vector3 dstpoint = new Vector3( LTrack[i - 1].X + Msdxdy[i].X * dz * Sh, LTrack[i - 1].Y + Msdxdy[i].Y * dz * Sh, LTrack[i - 1].Z + dz ); mc.MovePoint(dstpoint); mc.Join(); } List <ImageTaking> LiITUpMid = TakeSequentialImage( Msdxdy[i].X * Sh, Msdxdy[i].Y * Sh, dz_img, 8); ////画像の保存、座標の保存。 LStage.Add(new OpenCvSharp.CPlusPlus.Point3d(LiITUpMid[7].StageCoord.X, LiITUpMid[7].StageCoord.Y, LiITUpMid[7].StageCoord.Z)); LiITUpMid[7].img.ImWrite(datarootdirpathw + string.Format(@"\img_l_up_{0}.bmp", i)); UpTrackInfo.Add(LiITUpMid);//撮影した8枚の画像と、撮影した位置を記録する。 //撮影した画像をここで処理する。 List <Mat> binimages = new List <Mat>(); for (int t = 0; t <= 7; t++) { Mat bin = (Mat)DogContrastBinalize(LiITUpMid[t].img); double xx = myTrack.MsDX * myTrack.MsDX; double yy = myTrack.MsDY * myTrack.MsDY; if (Math.Sqrt(xx + yy) >= 0.4) { Cv2.Dilate(bin, bin, new Mat()); } Cv2.Dilate(bin, bin, new Mat()); binimages.Add(bin); } //trackを重ねる処理を入れる。 Point2d pixel_cen = TrackDetection(binimages, 256, 220, 3, 3, 4, 90, 3);//画像の8枚目におけるtrackのpixel座標を算出する。 if (pixel_cen.X == -1 & pixel_cen.Y == -1) { //追跡に失敗した時に最後に検出したtrack座標に移動してから、追跡に失敗した地点の画像を撮影するようにする。 //mc.MovePoint(LTrack[i - 1].X, LTrack[i - 1].Y, LTrack[i - 1].Z); mc.Join(); not_detect = 1; goto not_detect_track; } //検出したpixel座標をstage座標に変換するなどlistに追加する。 LPeak.Add(new OpenCvSharp.CPlusPlus.Point(pixel_cen.X - 256, pixel_cen.Y - 220)); double firstx = LStage[i].X - LPeak[i].X * 0.000267; double firsty = LStage[i].Y + LPeak[i].Y * 0.000267; double firstz = LStage[i].Z; LTrack.Add(new Point3d(firstx, firsty, firstz)); // //上側乳剤層上面の1回目のtrack検出によって、次のtrackの位置を検出する角度を求める。 //その角度が、1回目のtrack検出の結果によって大きな角度にならないように調整をする。 if (i == 0) { Msdxdy.Add(new OpenCvSharp.CPlusPlus.Point2d(myTrack.MsDX, myTrack.MsDY)); } else if (i == 1) { List <Point3d> LTrack_ghost = new List <Point3d>(); double dzPrev = (LStage[0].Z - surface.UpTop) * Sh; double Lghost_x = LTrack[0].X - Msdxdy[i].X * dzPrev; double Lghost_y = LTrack[0].Y - Msdxdy[i].Y * dzPrev; LTrack_ghost.Add(new Point3d(Lghost_x, Lghost_y, surface.UpTop));//上側乳剤層上面にtrackがあるならどの位置にあるかを算出する。 string txtfileName_ltrackghost = datarootdirpathw + string.Format(@"\LTrack_ghost.txt"); StreamWriter twriter_ltrackghost = File.CreateText(txtfileName_ltrackghost); twriter_ltrackghost.WriteLine("{0} {1} {2}", LTrack_ghost[0].X, LTrack_ghost[0].Y, LTrack_ghost[0].Z); twriter_ltrackghost.Close(); OpenCvSharp.CPlusPlus.Point2d Tangle = ApproximateStraight(Sh, LTrack_ghost[0], LTrack[0], LTrack[1]); Msdxdy.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle.X, Tangle.Y)); } else { OpenCvSharp.CPlusPlus.Point2d Tangle = ApproximateStraight(Sh, LTrack[i - 2], LTrack[i - 1], LTrack[i]); Msdxdy.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle.X, Tangle.Y)); } }//for i-loop //baseまたぎ int ltrack_counter = LTrack.Count(); int msdxdy_counter = Msdxdy.Count(); mc.MovePoint( LTrack[ltrack_counter - 1].X + Msdxdy[msdxdy_counter - 1].X * (surface.LowTop - surface.UpBottom), LTrack[ltrack_counter - 1].Y + Msdxdy[msdxdy_counter - 1].Y * (surface.LowTop - surface.UpBottom), surface.LowTop ); mc.Join(); //////ここから下gelの処理 Msdxdy_Low.Add(new OpenCvSharp.CPlusPlus.Point2d(Msdxdy[msdxdy_counter - 1].X, Msdxdy[msdxdy_counter - 1].Y)); //今までのtrack追跡プログラムとは異なる角度等の使い方をする。 dz_price_img = (6 * Math.Cos(theta) / Sh_low) / 1000; dz_img = dz_price_img * (-1); dz = dz_img; int goto_dgel = 0; for (int i = 0; goto_dgel < 1; i++) { ///////移動して画像処理をしたときに、下gelの下に入らないようにする。 Vector3 initialpos = mc.GetPoint(); double moverange = 7 * dz_img; double predpoint = moverange + initialpos.Z; if (predpoint < surface.LowBottom)//もしもbaseに入りそうなら、8枚目の画像がちょうど下gelを撮影するようにdzを調整する。 { goto_dgel = 1; dz = surface.LowBottom - initialpos.Z + 7 * dz_price_img; } //////// //goto_dgel == 1のときは、移動して画像を撮影するようにする。 if (i != 0) { Vector3 dstpoint = new Vector3( LTrack_Low[i - 1].X + Msdxdy_Low[i].X * dz * Sh_low, LTrack_Low[i - 1].Y + Msdxdy_Low[i].Y * dz * Sh_low, LTrack_Low[i - 1].Z + dz ); mc.MovePoint(dstpoint); mc.Join(); } //今までのtrack追跡プログラムとは異なる角度等の使い方をする。 List <ImageTaking> LiITLowMid = TakeSequentialImage( Msdxdy[i].X * Sh_low, Msdxdy[i].Y * Sh_low, dz_img, 8); //画像・座標の記録 LStage_Low.Add(new OpenCvSharp.CPlusPlus.Point3d(LiITLowMid[7].StageCoord.X, LiITLowMid[7].StageCoord.Y, LiITLowMid[7].StageCoord.Z)); LiITLowMid[7].img.ImWrite(datarootdirpathw + string.Format(@"\img_l_low_{0}.bmp", i)); LowTrackInfo.Add(LiITLowMid);//撮影した8枚の画像と、撮影した位置を記録する。 //撮影した画像をここで処理する。 List <Mat> binimages = new List <Mat>(); for (int t = 0; t <= 7; t++) { Mat bin = (Mat)DogContrastBinalize(LiITLowMid[t].img); double xx = myTrack.MsDX * myTrack.MsDX; double yy = myTrack.MsDY * myTrack.MsDY; if (Math.Sqrt(xx + yy) >= 0.4) { Cv2.Dilate(bin, bin, new Mat()); } Cv2.Dilate(bin, bin, new Mat()); binimages.Add(bin); } //trackを重ねる処理を入れる。 Point2d pixel_cen = TrackDetection(binimages, 256, 220, 3, 3, 4, 90, 3);//画像の8枚目におけるtrackのpixel座標を算出する。 //もし検出に失敗した場合はループを抜ける。 if (pixel_cen.X == -1 & pixel_cen.Y == -1) { //mc.MovePoint(LTrack_Low[i - 1].X, LTrack_Low[i - 1].Y, LTrack_Low[i - 1].Z); mc.Join(); not_detect = 1; goto not_detect_track; } // //検出したpixel座標をstage座標に変換するなどlistに追加する。 LPeak_Low.Add(new OpenCvSharp.CPlusPlus.Point(pixel_cen.X - 256, pixel_cen.Y - 220)); double firstx = LStage_Low[i].X - LPeak_Low[i].X * 0.000267; double firsty = LStage_Low[i].Y + LPeak_Low[i].Y * 0.000267; double firstz = LStage_Low[i].Z; LTrack_Low.Add(new Point3d(firstx, firsty, firstz)); // //ここからは、最小二乗法で角度を算出するプログラムである。 //上側乳剤層上面の1回目のtrack検出によって、次のtrackの位置を検出する角度を求める。 //その角度が、1回目のtrack検出の結果によって大きな角度にならないように調整をする。 if (i == 0) { OpenCvSharp.CPlusPlus.Point2d Tangle_l = ApproximateStraightBase(Sh, Sh_low, LTrack[ltrack_counter - 2], LTrack[ltrack_counter - 1], LTrack_Low[i], surface); Msdxdy_Low.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle_l.X, Tangle_l.Y)); } else if (i == 1) { OpenCvSharp.CPlusPlus.Point2d Tangle_l = ApproximateStraightBase(Sh, Sh_low, LTrack[ltrack_counter - 1], LTrack_Low[i - 1], LTrack_Low[i], surface); Msdxdy_Low.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle_l.X, Tangle_l.Y)); } else { OpenCvSharp.CPlusPlus.Point2d Tangle_l = ApproximateStraight(Sh_low, LTrack_Low[i - 2], LTrack_Low[i - 1], LTrack_Low[i]); Msdxdy_Low.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle_l.X, Tangle_l.Y)); } }//i_loop // int ltrack_low_count = LTrack_Low.Count(); mc.MovePointXY(LTrack_Low[ltrack_low_count - 1].X, LTrack_Low[ltrack_low_count - 1].Y); mc.Join(); //検出に失敗した場合は、ループを抜けてここに来る。 not_detect_track :;//検出に失敗したと考えられる地点で画像を取得し、下ゲル下面まで移動する。(現在は下ゲル下面とするが、今後変更する可能性有。) if (not_detect != 0) { //写真撮影 List <ImageTaking> NotDetect = TakeSequentialImage( Msdxdy[0].X * Sh, Msdxdy[0].Y * Sh, 0, 1); string txtfileName_t_not_detect = datarootdirpathw + string.Format(@"\not_detect.txt"); StreamWriter twriter_t_not_detect = File.CreateText(txtfileName_t_not_detect); for (int i = 0; i < NotDetect.Count; i++) { NotDetect[i].img.ImWrite(datarootdirpathw + string.Format(@"\img_t_not_detect.bmp")); Vector3 p = NotDetect[i].StageCoord; twriter_t_not_detect.WriteLine("{0} {1} {2} {3}", i, p.X, p.Y, p.Z); } twriter_t_not_detect.Close(); mc.MovePointZ(surface.LowBottom); mc.Join(); } //file write out up_gel string txtfileName_sh_up = datarootdirpathw + string.Format(@"\Sh_up.txt"); StreamWriter twriter_sh_up = File.CreateText(txtfileName_sh_up); twriter_sh_up.WriteLine("{0}", Sh); twriter_sh_up.Close(); //file write out string txtfileName_t_info_up = datarootdirpathw + string.Format(@"\location_up.txt"); StreamWriter twriter_t_info_up = File.CreateText(txtfileName_t_info_up); for (int i = 0; i < UpTrackInfo.Count; i++) { for (int t = 0; t < UpTrackInfo[i].Count; t++) { UpTrackInfo[i][t].img.ImWrite(datarootdirpathw + string.Format(@"\img_t_info_up_{0}-{1}.bmp", i, t)); Vector3 p = UpTrackInfo[i][t].StageCoord; twriter_t_info_up.WriteLine("{0} {1} {2} {3} {4}", i, t, p.X, p.Y, p.Z); } } twriter_t_info_up.Close(); string txtfileName_lpeak = datarootdirpathw + string.Format(@"\lpeak_up.txt"); StreamWriter twriter_lpeak = File.CreateText(txtfileName_lpeak); for (int i = 0; i < LPeak.Count(); i++) { OpenCvSharp.CPlusPlus.Point p = LPeak[i]; twriter_lpeak.WriteLine("{0} {1} {2}", i, p.X, p.Y); } twriter_lpeak.Close(); string txtfileName_ltrack = datarootdirpathw + string.Format(@"\ltrack_up.txt"); StreamWriter twriter_ltrack = File.CreateText(txtfileName_ltrack); for (int i = 0; i < LTrack.Count(); i++) { OpenCvSharp.CPlusPlus.Point3d p = LTrack[i]; twriter_ltrack.WriteLine("{0} {1} {2} {3}", i, p.X, p.Y, p.Z); } twriter_ltrack.Close(); string txtfileName_msdxdy = datarootdirpathw + string.Format(@"\msdxdy.txt"); StreamWriter twriter_msdxdy = File.CreateText(txtfileName_msdxdy); for (int i = 0; i < Msdxdy.Count(); i++) { OpenCvSharp.CPlusPlus.Point2d p = Msdxdy[i]; twriter_msdxdy.WriteLine("{0} {1} {2}", i, p.X, p.Y); } twriter_msdxdy.Close(); //file write out low_gel string txtfileName_sh_low = datarootdirpathw + string.Format(@"\Sh_low.txt"); StreamWriter twriter_sh_low = File.CreateText(txtfileName_sh_low); twriter_sh_low.WriteLine("{0}", Sh_low); twriter_sh_low.Close(); string txtfileName_t_info_low = datarootdirpathw + string.Format(@"\location_low.txt"); StreamWriter twriter_t_info_low = File.CreateText(txtfileName_t_info_low); for (int i = 0; i < LowTrackInfo.Count; i++) { for (int t = 0; t < LowTrackInfo[i].Count; t++) { LowTrackInfo[i][t].img.ImWrite(datarootdirpathw + string.Format(@"\img_t_info_low_{0}-{1}.bmp", i, t)); Vector3 p = LowTrackInfo[i][t].StageCoord; twriter_t_info_low.WriteLine("{0} {1} {2} {3} {4}", i, t, p.X, p.Y, p.Z); } } twriter_t_info_low.Close(); string txtfileName_lpeak_low = datarootdirpathw + string.Format(@"\lpeak_low.txt"); StreamWriter twriter_lpeak_low = File.CreateText(txtfileName_lpeak_low); for (int i = 0; i < LPeak_Low.Count(); i++) { OpenCvSharp.CPlusPlus.Point p = LPeak_Low[i]; twriter_lpeak_low.WriteLine("{0} {1} {2}", i, p.X, p.Y); } twriter_lpeak_low.Close(); string txtfileName_ltrack_low = datarootdirpathw + string.Format(@"\ltrack_low.txt"); StreamWriter twriter_ltrack_low = File.CreateText(txtfileName_ltrack_low); for (int i = 0; i < LTrack_Low.Count(); i++) { OpenCvSharp.CPlusPlus.Point3d p = LTrack_Low[i]; twriter_ltrack_low.WriteLine("{0} {1} {2} {3}", i, p.X, p.Y, p.Z); } twriter_ltrack_low.Close(); string txtfileName_msdxdy_low = datarootdirpathw + string.Format(@"\msdxdy_low.txt"); StreamWriter twriter_msdxdy_low = File.CreateText(txtfileName_msdxdy_low); for (int i = 0; i < Msdxdy_Low.Count(); i++) { OpenCvSharp.CPlusPlus.Point2d p = Msdxdy_Low[i]; twriter_msdxdy_low.WriteLine("{0} {1} {2}", i, p.X, p.Y); } twriter_msdxdy_low.Close();//*/ //.................Taking Photo in buttom of down layer.......................// try { string[] sp = myTrack.IdString.Split('-'); string dwtxt = string.Format(@"c:\test\bpm\{0}\{1}-{2}-{3}-{4}_dw.txt", mod, mod, pl, sp[0], sp[1]); string datarootdirpath = string.Format(@"C:\test\bpm\{0}", mod); System.IO.DirectoryInfo mydir = System.IO.Directory.CreateDirectory(datarootdirpath); System.IO.DirectoryInfo mydir2 = System.IO.Directory.CreateDirectory(datarootdirpath); BeamDetection(dwtxt, false); } catch (ArgumentOutOfRangeException) { MessageBox.Show("ID is not exist "); } //...............Move to top surfacr of upper layer........................// try { Vector3 cc = mc.GetPoint(); double Zp = surface.UpTop; mc.MoveTo(new Vector3(cc.X, cc.Y, Zp)); mc.Join(); } catch (ArgumentOutOfRangeException) { MessageBox.Show("Cannot move to top surface of upperlayer "); } // .......................................................................// sw.Stop(); string[] sp1 = myTrack.IdString.Split('-'); string logtxt_ = string.Format(@"c:\test\bpm\{0}\{1}-{2}_log_.txt", mod, mod, pl); //string log_ = string.Format("{0} \n", sw.Elapsed); string log_ = string.Format("{0} {1} {2} \n", sp1[0], sp1[1], sw.Elapsed); StreamWriter swr = new StreamWriter(logtxt_, true, Encoding.ASCII); swr.Write(log_); swr.Close(); }
private void task() { MotorControler mc = MotorControler.GetInstance(parameterManager); Surface sur = Surface.GetInstance(parameterManager); Camera camera = Camera.GetInstance(); Led led = Led.GetInstance(); CoordManager cm = new CoordManager(parameterManager); Vector3 InitPoint = mc.GetPoint(); List <Mat> image_set = new List <Mat>(); Vector3 ggg = mc.GetPoint(); string txtfileName = string.Format(@"c:\img\bbbbbb.txt"); StreamWriter twriter = File.CreateText(txtfileName); // twriter.Write("line95\n"); Vector3 initialpoint = mc.GetPoint(); List <Vector2> list_lateralmark43 = new List <Vector2>();//in PL#43 list_lateralmark43.Add(new Vector2(81.706, 89.124)); list_lateralmark43.Add(new Vector2(-89.715, 83.350)); list_lateralmark43.Add(new Vector2(-86.969, -87.964)); list_lateralmark43.Add(new Vector2(83.521, -86.169)); List <Vector2> list_scanregion43 = new List <Vector2>(); list_scanregion43.Add(list_lateralmark43[0] - new Vector2(-5, -5)); list_scanregion43.Add(list_lateralmark43[1] - new Vector2(+5, -5)); list_scanregion43.Add(list_lateralmark43[2] - new Vector2(+5, +5)); list_scanregion43.Add(list_lateralmark43[3] - new Vector2(-5, +5)); List <Vector2> list_lateralmark42 = new List <Vector2>();//in PL#42 list_lateralmark42.Add(new Vector2(81.750, 89.099)); list_lateralmark42.Add(new Vector2(-89.526, 83.417)); list_lateralmark42.Add(new Vector2(-86.754, -87.727)); list_lateralmark42.Add(new Vector2(83.612, -85.831)); List <Vector2> list_scanregion42 = new List <Vector2>(); list_scanregion42.Add(list_lateralmark42[0] - new Vector2(-5, -5)); list_scanregion42.Add(list_lateralmark42[1] - new Vector2(+5, -5)); list_scanregion42.Add(list_lateralmark42[2] - new Vector2(+5, +5)); list_scanregion42.Add(list_lateralmark42[3] - new Vector2(-5, +5)); Affine ap_s43_s42 = Affine.CreateAffineBy(list_lateralmark43, list_lateralmark42); List <Vector2> list_scanpoint_g = new List <Vector2>(); for (int x = 0; x < 11; x++) { for (int y = 0; y < 11; y++) { list_scanpoint_g.Add(new Vector2(x * 1.0, y * 1.0)); } } List <Vector2> list_scancorner_g = new List <Vector2>(); list_scancorner_g.Add(new Vector2(10.0, 10.0)); list_scancorner_g.Add(new Vector2(0.0, 10.0)); list_scancorner_g.Add(new Vector2(0.0, 0.0)); list_scancorner_g.Add(new Vector2(10.0, 0.0)); Affine ap_g43_s43 = Affine.CreateAffineBy(list_scancorner_g, list_scanregion43); camera.Start(); for (int d = 0; d < list_scanpoint_g.Count; d++) { // Vector2 predpoint41 = ap_g41_s41.Trance(list_scanpoint_g[d]); Vector2 predpoint43 = ap_g43_s43.Trance(list_scanpoint_g[d]); Vector2 predpoint42 = ap_s43_s42.Trance(predpoint43); mc.MoveTo(new Vector3(predpoint42.X, predpoint42.Y, initialpoint.Z - 0.100)); mc.Join(); Thread.Sleep(4000); int ledbrightness = led.AdjustLight(parameterManager); int viewcounter = 0; bool flag = true; while (flag) { mc.MoveDistance(+0.003, VectorId.Z); mc.Join(); byte[] b = camera.ArrayImage; Mat src = new Mat(440, 512, MatType.CV_8U, b); Mat mat = src.Clone(); Cv2.GaussianBlur(mat, mat, Cv.Size(3, 3), -1); Mat gau = mat.Clone(); Cv2.GaussianBlur(gau, gau, Cv.Size(7, 7), -1); Cv2.Subtract(gau, mat, mat); Cv2.Threshold(mat, mat, 4, 1, ThresholdType.Binary); int brightness = Cv2.CountNonZero(mat); viewcounter++; if (brightness > 2000 || viewcounter > 100) { flag = false; } }//while-loop surface detection Vector3 surfacepoint = mc.GetPoint(); for (int vy = 0; vy < 2; vy++) { for (int vx = 0; vx < 2; vx++) { mc.MoveTo (new Vector3( predpoint42.X - 0.125 / 2.0 + 0.125 * vx, predpoint42.Y - 0.105 / 2.0 + 0.105 * vy, surfacepoint.Z)); mc.Join(); for (int q = 0; q < 8; q++) { mc.MovePointZ(surfacepoint.Z + 0.003 + (0.003 * q)); mc.Join(); Vector3 currentpoint = mc.GetPoint(); byte[] b = camera.ArrayImage; Mat image = new Mat(440, 512, MatType.CV_8U, b); Mat clone_image = image.Clone(); //clone_image.ImWrite(String.Format(@"c:\img\{0}_{1:00}.bmp", d, q)); clone_image.ImWrite(String.Format(@"c:\img\{0}_{1}_{2}_{3:00}.bmp", d, vx, vy, q)); string stlog = ""; stlog += String.Format("{0} {1} {2} {3}\n", ledbrightness, currentpoint.X, currentpoint.Y, currentpoint.Z); twriter.Write(stlog); // twriter.Write("line198\n"); twriter.Flush(); } //for-loop q pictureID } //for-loop vx view0,1 } //for-loop vy view0,1 } //for-loop d-th scanning point camera.Stop(); twriter.Close(); }