List <ImageTaking> TakeSequentialImage(double ax, double ay, double dz, int nimage) { MotorControler mc = MotorControler.GetInstance(parameterManager); Camera camera = Camera.GetInstance(); Vector3 initialpos = mc.GetPoint(); List <ImageTaking> lit = new List <ImageTaking>(); for (int i = 0; i < nimage; i++) { Vector3 dstpoint = new Vector3( initialpos.X + ax * dz * i, initialpos.Y + ay * dz * i, initialpos.Z + dz * i ); mc.MovePoint(dstpoint); mc.Join(); byte[] b = camera.ArrayImage; Mat image = new Mat(440, 512, MatType.CV_8U, b); ImageTaking it = new ImageTaking(); it.img = image.Clone(); it.StageCoord = mc.GetPoint(); lit.Add(it); //image.Release(); //imagec.Release(); } return(lit); }
private void task() { // レンズが50倍に設定されていない場合は例外を返すようにしたいがやり方が分からん(20140724) //現在地からスパイラルサーチ30視野でグリッドマークを検出する //検出したら視野の真ん中に持ってくる 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 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"); } }
public void ShowReFollowTrackDifinitionUi(double x, double y) //H26 9/26 by GTR { MotorControler mc = MotorControler.GetInstance(parameterManager); Vector3 initialpos = mc.GetPoint(); Line[] lines = DrawCrossMark(x, y); // Grid markを定義する位置を確認するダイアログボックスを表示する //GridMarkDefinitionUtil util = new GridMarkDefinitionUtil(coordManager); //SelectGridMarkWindow sgWindow = new SelectGridMarkWindow(); //sgWindow.GridMarkPoint = util.NextPoint; // ユーザーがGrid markを定義する位置を選択したならば,その箇所でGridf markを定義する. Camera camera = Camera.GetInstance(); Vector2 emulsionSystemPoint = coordManager.TransToEmulsionCoord((int)x, (int)y); //coordManager.SetGridMark(emulsionSystemPoint, sgWindow.GridMarkPoint, camera.ArrayImage); //WriteLine(string.Format(Properties.Strings.DefineGridMark, // sgWindow.GridMarkPoint, emulsionSystemPoint.X, emulsionSystemPoint.Y)); string datarootdirpathw = string.Format(@"C:\test"); string txtfileName_sh_up = datarootdirpathw + string.Format(@"\ReTrackFollowPosi.txt"); StreamWriter twriter_sh_up = File.CreateText(txtfileName_sh_up); twriter_sh_up.WriteLine("{0} {1} {2}", emulsionSystemPoint.X, emulsionSystemPoint.Y, initialpos.Z); twriter_sh_up.Close(); // バツ印を消去 emulsionViewerCanvas.Children.Remove(lines[0]); emulsionViewerCanvas.Children.Remove(lines[1]); }
/// <summary> /// 現在,どこのゲルにいるのかを現在の座標値と,前回調べた境界面の座標値から算出します. /// </summary> /// <returns>現在いるゲル</returns> private GelLayer getWhichLayerNow() { MotorControler mc = MotorControler.GetInstance(parameterManager); double z = mc.GetPoint().Z; GelLayer gelLayer; if (z > UpTop) { gelLayer = GelLayer.OverOut; } else if (z <= UpTop && z >= UpBottom) { gelLayer = GelLayer.Over; } else if (z < UpBottom && z > LowTop) { gelLayer = GelLayer.Base; } else if (z <= LowTop && z >= LowBottom) { gelLayer = GelLayer.Under; } else if (z < LowBottom) { gelLayer = GelLayer.UnderOut; } else { gelLayer = GelLayer.Unknown; } return(gelLayer); }
}//BeamDetection private void BPMW(Track myTrack, int mod, int pl) { MotorControler mc = MotorControler.GetInstance(parameterManager); try { string datarootdirpath = string.Format(@"C:\MKS_test\bpm\{0}-{1}", mod, pl); System.IO.DirectoryInfo mydir = System.IO.Directory.CreateDirectory(datarootdirpath); string[] sp = myTrack.IdString.Split('-'); string logtxt = string.Format(@"C:\MKS_test\WorkingTime\{0}\{1}-{2}_BPMW.txt", mod, mod, pl); SimpleLogger SL1 = new SimpleLogger(logtxt, sp[0], sp[1]); string uptxt = string.Format(@"c:\MKS_test\bpm\{0}-{1}\{2}-{3}-{4}-{5}_up.txt", mod, pl, mod, pl, sp[0], sp[1]); string dwtxt = string.Format(@"c:\MKS_test\bpm\{0}-{1}\{2}-{3}-{4}-{5}_dw.txt", mod, pl - 1, mod, pl - 1, sp[0], sp[1]); BeamDetection(uptxt, true); BeamPatternMatch bpm = new BeamPatternMatch(8, 200); bpm.ReadTrackDataTxtFile(dwtxt, false); bpm.ReadTrackDataTxtFile(uptxt, true); SL1.Info("Tracking Start"); bpm.DoPatternMatch(); SL1.Info("Tracking end"); 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。 "); } }
private void viewTimer_Ticked(object sender, EventArgs e) { MotorControler mc = MotorControler.GetInstance(parameterManager); Vector3 p = mc.GetPoint(); coordinateLabel.Content = string.Format("X:{0:0.00000}, Y:{1:0.00000}, Z:{2:0.0000}", p.X, p.Y, p.Z); }
private void startButton_Click(object sender, RoutedEventArgs e) { if (!System.IO.Directory.Exists(destinationDir)) { MessageBox.Show(Properties.Strings.DirNotFound); return; } if (fileNameTextBox.Background == Brushes.Pink) { MessageBox.Show(Properties.Strings.FileNameInvalid); return; } AccumImage accumImage = AccumImage.GetInstance(parameterManager); if (accumImage.IsActive) { MessageBoxResult r = MessageBox.Show( Properties.Strings.ShootException01, Properties.Strings.Abort + "?", MessageBoxButton.YesNo); if (r == MessageBoxResult.Yes) { accumImage.Abort(); } else { return; } } try { accumImage.FilePath = destinationDir; accumImage.FilenamePrefix = fileName; double distance = double.Parse(distanceTextBox.Text); MotorControler mc = MotorControler.GetInstance(parameterManager); accumImage.IntervalUm = double.Parse(intervalTextBox.Text); accumImage.StartPoint = mc.GetPoint().Z; accumImage.EndPoint = accumImage.StartPoint + ((bool)minusRadioButton.IsChecked ? -distance : distance); progressPanel.Visibility = Visibility.Visible; progressBar.Minimum = 0; progressBar.Maximum = distance / accumImage.IntervalUm; accumImage.Start(); abortButton.IsEnabled = true; ledGroupBox.IsEnabled = false; Properties.Settings.Default.ShootDistance = double.Parse(distanceTextBox.Text); Properties.Settings.Default.ShootInterval = accumImage.IntervalUm; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void parametersTimer_Tick(object sender, EventArgs e) { // モータの座標値を更新する MotorControler mc = MotorControler.GetInstance(parameterManager); Vector3 presentPosition = mc.GetPoint(); drawPositionOnViewer(presentPosition); // モータ稼働中は速度の変更を禁止にする speedComboBox.IsEnabled = (!mc.IsMoving); }
/// <summary> /// コンピュータ画面上のピクセル値の座標からエマルションの座標値に変換します. /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns>エマルションでの座標値</returns> public Vector2 TransToEmulsionCoord(int x, int y) { MotorControler mc = MotorControler.GetInstance(); int emX = ParameterManager.ImageResolution.Width; int emY = ParameterManager.ImageResolution.Height; Vector3 motorPosition = mc.GetPoint(); Vector2 p = new Vector2(); p.X = motorPosition.X - (x - emX / 2) * parameterManager.CameraMainResolution; p.Y = motorPosition.Y + (y - emY / 2) * parameterManager.CameraSubResolution; return(p); }
private void task() { MotorControler mc = MotorControler.GetInstance(parameterManager); Vector3 CenterPoint = mc.GetPoint(); string fileName = string.Format(@"c:\test\coordinate.txt"); System.IO.StreamWriter sw = new System.IO.StreamWriter(fileName, true, System.Text.Encoding.GetEncoding("shift_jis")); string coordinate = string.Format("{0} {1} {2}\n", CenterPoint.X, CenterPoint.Y, CenterPoint.Z); sw.Write(coordinate); sw.Close(); }
private void GoTopUp() { MotorControler mc = MotorControler.GetInstance(parameterManager); Surface surface = Surface.GetInstance(parameterManager); try { Vector3 cc = mc.GetPoint(); double Zp = surface.UpTop + 0.015; mc.MoveTo(new Vector3(cc.X, cc.Y, Zp)); mc.Join(); } catch (ArgumentOutOfRangeException) { MessageBox.Show("Cannot move to top surface of upperlayer "); } }
/// <summary> /// 撮影終了地点に到着してるかどうかを判定します. /// </summary> /// <param name="direction">移動方向</param> /// <returns>true:到着してる, false:到着していない</returns> private bool isOnEndPoint(PlusMinus direction) { bool flag = false; MotorControler mc = MotorControler.GetInstance(parameterManager); double point = mc.GetPoint().Z; switch (direction) { case PlusMinus.Plus: flag = (startPoint <= endPoint); break; case PlusMinus.Minus: flag = (startPoint >= endPoint); break; } return(flag); }
/// <summary> /// recogThreadの行う処理です.このメソッドを直接呼び出さないでください. /// </summary> private void recogThread_Task() { App.logger.Info("Surface.recogThread_Task() start"); if (Started != null) { Started(this, new EventArgs()); } Camera camera = Camera.GetInstance(); camera.Start(); /* Z軸をSpeed1(低速)でマイナス方向に動かして,最下点もしくは下降距離分だけ移動させる.*/ MotorControler mc = MotorControler.GetInstance(parameterManager); speedBeforeStart = mc.Speed; bool flag; double temp_z; mc.Inch(PlusMinus.Minus, motorSpeed, VectorId.Z); flag = true; while (flag) { int brightness = CountHitPixels(); if (brightness > 8000) { flag = false; } //if (brightness < 10000) flag = false;// } /* * UpTopRecognized(this, new eventArgs()); * LowBottomRecognized(this, new eventArgs()); * LowTopRecognized(this, new eventArgs()); * UpBottomRecognized(this, new eventArgs()); */ mc.SlowDownStop(VectorId.Z); surfaces[0] = mc.GetPoint().Z; System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z)); Thread.Sleep(100); mc.Move(new Vector3(0.0, 0.0, -0.2)); mc.Join(); Thread.Sleep(100); mc.Inch(PlusMinus.Minus, motorSpeed, VectorId.Z); flag = true; while (flag) { int brightness = CountHitPixels(); if (brightness < 5000) { flag = false; // } // if (brightness < 4000) flag = false; } surfaces[1] = mc.GetPoint().Z;// // surfaces[2] = mc.GetPoint().Z; System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z)); flag = true; while (flag) { int brightness = CountHitPixels(); //if (brightness > 4000) flag = false; if (brightness > 5000) { flag = false; } } mc.SlowDownStop(VectorId.Z); // surfaces[1] = mc.GetPoint().Z; surfaces[2] = mc.GetPoint().Z;// System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z)); Thread.Sleep(100); // Thread.Sleep(150);// // mc.Move(new Vector3(0.0, 0.0, -0.2)); mc.Move(new Vector3(0.0, 0.0, -0.23));// mc.Join(); Thread.Sleep(100); //Thread.Sleep(150);// mc.Inch(PlusMinus.Minus, motorSpeed, VectorId.Z); flag = true; while (flag) { int brightness = CountHitPixels(); // if (brightness < 2000) flag = false; if (brightness < 3000) { flag = false; // } } mc.SlowDownStop(VectorId.Z); surfaces[3] = mc.GetPoint().Z; System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z)); Thread.Sleep(100); // Thread.Sleep(150);// // temp_z = (surfaces[0] - 0.05) - mc.GetPoint().Z; temp_z = (surfaces[0] + 0.02) - mc.GetPoint().Z;// mc.Move(new Vector3(0.0, 0.0, temp_z)); mc.Join(); Thread.Sleep(100); //Thread.Sleep(150);// /* * // モータZ軸をマイナス方向(下方向)に稼働させる * try { * // 下降の開始 * mc.SetMotorSpeed(IO.MotorSpeed.Speed3); * mc.Inch(MechaAxisAddress.ZAddress, PlusMinus.Plus); * } catch (MotorAxisException) { * // すでに最下点に位置していた場合は,catchのスコープが実行される. * isOnBottomLimit = true; * } * * // 最下点もしくは下降距離分に下降したかを監視する. * double startPoint = mc.GetPoint().Z; * while (!isOnBottomLimit) { * // 移動距離の確認 * if (loweringDistance != 0) { * isOnBottomLimit = (loweringDistance <= Math.Abs(mc.GetPoint().Z - startPoint)); * } * * // モータの移動可能最下点到達か否かの確認 * isOnBottomLimit |= * (mc.GetAbnomalState(MechaAxisAddress.ZAddress) * == MotorAbnomalState.AxisLimitPlus); * } * // 最下点に到達したため,モータの移動を止める. * // 最下点に到達時のイベントを発生させる * mc.StopInching(MechaAxisAddress.ZAddress); * if (OnMotorBottomLimited != null) { * OnMotorBottomLimited(this, new EventArgs()); * } * * // 遅延を行わないとモータドライバが動作不良を起こす場合がある. * Thread.Sleep(300); * * */ /* -------------------- 下降の完了 ---------------- */ /* 上昇しながら撮影中の画像を分析する */ // 最下点からZ軸をプラス方向に動かす. // 撮影中の画像がゲルの中か否かを判定する. // ベース及びその他の場所と,ゲルの中の境界である座標を記録する. /* * * * mc.Inch(PlusMinus.Plus, motorSpeed, VectorId.Z); * startPoint = mc.GetPoint().Z; * * * // bool flag = true; * * while (flag) { * //Thread.Sleep(delayTime); * * 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(31, 31), -1); * Cv2.Subtract(gau, mat, mat); * Cv2.Threshold(mat, mat, 10, 1, ThresholdType.Binary); * int brightness = Cv2.CountNonZero(mat); * * if (brightness > 3000) flag = false; * } * * mc.StopInching(MechaAxisAddress.ZAddress); * surfaces[3] = mc.GetPoint().Z; * System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z)); * * * * // Vector3 speed = new Vector3(30, 30, 0.4);//つかわないけど * // Vector3 tolerance = new Vector3(0.001, 0.001, 0.001);//つかわないけど * // Vector3 distance = new Vector3(0, 0, 0.3); * // mc.Move(distance, speed, tolerance); * * Thread.Sleep(100); * * */ /* * * * mc.Inch(PlusMinus.Plus, MotorSpeed, VectorId.Z); * double currentPoint = mc.GetPoint().Z; * * bool dice = true; * * while (dice) { * * double nowPoint = mc.GetPoint().Z; * * double renge = nowPoint - currentPoint; * * if (renge > 0.20) dice = false; * * * * * } * mc.StopInching(MechaAxisAddress.ZAddress); * * Thread.Sleep(100); * * * / * * * // mc.MoveDistance(0.2, VectorId.Z); * // mc.Join(); * // Thread.Sleep(2000); * // System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z)); * /* * * mc.Inch(PlusMinus.Plus, motorSpeed, VectorId.Z); * flag = true; * while (flag) { * * 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(31, 31), -1); * Cv2.Subtract(gau, mat, mat); * Cv2.Threshold(mat, mat, 10, 1, ThresholdType.Binary); * int brightness = Cv2.CountNonZero(mat); * * if (brightness < 4000) flag = false; * } * * * surfaces[2] = mc.GetPoint().Z; * System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z)); * */ /* * * * flag = true; * while (flag) { * * 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(31, 31), -1); * Cv2.Subtract(gau, mat, mat); * Cv2.Threshold(mat, mat, 10, 1, ThresholdType.Binary); * int brightness = Cv2.CountNonZero(mat); * * if (brightness > 4000) flag = false; * } * * mc.StopInching(MechaAxisAddress.ZAddress); * surfaces[1] = mc.GetPoint().Z; * System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z)); * * * //mc.Move(distance, speed, tolerance); * * * * * * * * // 遅延を行わないとモータドライバが動作不良を起こす場合がある. * Thread.Sleep(100); * * */ /* * * mc.Inch(PlusMinus.Plus, MotorSpeed, VectorId.Z); * double current2Point = mc.GetPoint().Z; * * bool dice2 = true; * * while (dice2) { * * double nowPoint = mc.GetPoint().Z; * * double renge = nowPoint - current2Point; * * if (renge > 0.20) dice2 = false; * * * * * } * * mc.StopInching(MechaAxisAddress.ZAddress); * * Thread.Sleep(100); * */ // mc.MoveDistance(0.2, VectorId.Z); // mc.Join(); // Thread.Sleep(2000); // System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z)); /* mc.Inch(PlusMinus.Plus, motorSpeed, VectorId.Z); * flag = true; * while (flag) { * * 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(31, 31), -1); * Cv2.Subtract(gau, mat, mat); * Cv2.Threshold(mat, mat, 10, 1, ThresholdType.Binary); * int brightness = Cv2.CountNonZero(mat); * * if (brightness < 12000) flag = false; * } * mc.StopInching(MechaAxisAddress.ZAddress); * surfaces[0] = mc.GetPoint().Z; * System.Diagnostics.Debug.WriteLine(string.Format("{0}", mc.GetPoint().Z)); * * * */ ////////////// // mc.AAAAAA(); /* * stopWatch = new System.Diagnostics.Stopwatch(); * stopWatch.Start(); * * int idNo = 0; * int index = 3; #if false * bool[] votes = new bool[numOfVoting]; * int[] values = new int[numOfVoting]; * double[] points = new double[numOfVoting]; * * Thread.Sleep(delayTime); * * // 今回の入力画像の演算結果を多数決配列の最後尾に設定する. * // 同時に今回の判定値やモータの座標もそれぞれ保持する. * votes[votes.Length - 1] = IsInGel(); * values[values.Length - 1] = brightness; * points[points.Length - 1] = mc.GetPoint().Z; * * // 今回と過去一定回数分の結果で多数決をとる. * presentResult = Vote(votes); * * // 多数決の結果配列を一つずつシフトする * for (int i = 0; i < values.Length; ++i) { * votes[i] = (i == votes.Length - 1 ? false : votes[i + 1]); * values[i] = (i == values.Length - 1 ? 0 : values[i + 1]); * points[i] = (i == points.Length - 1 ? 0 : points[i + 1]); * } #endif * * double previousZVal, presentZVal; * int previousBrightness , presentBrightness; * bool previousResult = false, presentResult = false; * while (index >= 0) { * Thread.Sleep(delayTime); * presentZVal = Math.Round(mc.GetPoint().Z, 5); * presentResult = IsInGel(); * bool isBorder = isBoudarySurface(previousResult, presentResult, index); * * // 撮像・入力画像の確認のイベントを発生させる. * SurfaceEventArgs eventArgs = new SurfaceEventArgs(); * eventArgs.Id = idNo; #if false * eventArgs.ZValue = points[0]; * eventArgs.Brightness = values[0]; * eventArgs.Distance = Math.Abs(points[0] - startPoint); #endif * * presentBrightness = brightness; * eventArgs.ZValue = presentZVal; * eventArgs.Brightness = presentBrightness; * eventArgs.Distance = Math.Abs(presentZVal - startPoint); * eventArgs.IsInGel = presentResult; * eventArgs.IsBoundary = isBorder; * eventArgs.Second = Time; ++idNo; * if (Shooting != null) { * Thread t = new Thread(new ThreadStart(delegate() { * Shooting(this, eventArgs); * })); * t.IsBackground = true; * t.Start(); * } * * if (isBorder) { * // 境界面であった場合,多数決に用いたZ座標の値の内,最も古い値を境界面の座標として採用する. * // ここでは境界面として,その座標値を保持している. * //surfaces[index] = points[0]; * surfaces[index] = presentZVal; * * // 境界面認識時のイベントを発生させる. * switch (index) { * 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; * } * * --index; * } * previousResult = presentResult; * previousBrightness = presentBrightness; * previousZVal = presentZVal; * * // 距離によるエラー終了 * if (mc.GetPoint().Z - startPoint > distanceOut) { * mc.StopInching(MechaAxisAddress.ZAddress); * throw new SurfaceFailedException("Distance limit out"); * } * } * * mc.StopInching(MechaAxisAddress.ZAddress); * stopWatch.Stop(); * dateTime = DateTime.Now; * latestCoord = new Vector2(mc.GetPoint().X, mc.GetPoint().Y); */ }
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 initialpoint = mc.GetPoint(); int pixthre = 500; for (int bx = -5; bx <= 5; bx++) { for (int by = -5; by <= 5; by++) { string txtfileName = string.Format(@"E:\img\{0}_{1}.txt", bx, by); StreamWriter twriter = File.CreateText(txtfileName); Vector3 blockstartpoint = new Vector3(); blockstartpoint.X = initialpoint.X + bx * 1.0; blockstartpoint.Y = initialpoint.Y + by * 1.0; blockstartpoint.Z = initialpoint.Z; mc.MoveTo(new Vector3(blockstartpoint.X + 0.5, blockstartpoint.Y + 0.5, initialpoint.Z - 0.020)); mc.Join(); int ledbrightness = led.AdjustLight(parameterManager); camera.Start(); surfrecog(pixthre, 0.003); camera.Stop(); double surfaceZup = mc.GetPoint().Z; //上面 ベース中からはじめ、ベース上側を表面認識 //ベース上側からはじめてZ方向正の向きにスキャン for (int vy = 0; vy < 10; vy++) { Vector3 linestartpoint = mc.GetPoint(); linestartpoint.X = blockstartpoint.X; linestartpoint.Y = blockstartpoint.Y + vy * parameterManager.SpiralShiftY; linestartpoint.Z = surfaceZup; for (int vx = 0; vx < 8;) { if (vx == 0) { Vector3 approachingpoint = mc.GetPoint(); approachingpoint.X = blockstartpoint.X + vx * parameterManager.SpiralShiftX - 0.05; approachingpoint.Y = blockstartpoint.Y + vy * parameterManager.SpiralShiftY - 0.05; approachingpoint.Z = linestartpoint.Z - 0.006; mc.MoveTo(approachingpoint); mc.Join(); } Vector3 viewstartpoint = mc.GetPoint(); viewstartpoint.X = blockstartpoint.X + vx * parameterManager.SpiralShiftX; viewstartpoint.Y = blockstartpoint.Y + vy * parameterManager.SpiralShiftY; viewstartpoint.Z = linestartpoint.Z - 0.006; mc.MoveTo(viewstartpoint); mc.Join(); Thread.Sleep(100); Vector3 viewpoint = mc.GetPoint(); List <ImageTaking> lit = new List <ImageTaking>(); mc.Inch(PlusMinus.Plus, 0.15, VectorId.Z); int viewcounter = 0; while (viewcounter < 16 + 3) { byte[] b = Ipt.CaptureMain(); Vector3 p = mc.GetPoint(); if (viewcounter >= 3) { ImageTaking it = new ImageTaking(p, b); string stlog = ""; stlog += String.Format("{0} {1} {2} {3}\n", ledbrightness, p.X, p.Y, p.Z); twriter.Write(stlog); } viewcounter++; }//view viewcounter = 0; double endz = mc.GetPoint().Z; mc.SlowDownStop(VectorId.Z); mc.Join(); if (endz - viewstartpoint.Z < 0.070) { tsparams tsp = new tsparams(); tsp.phthre = 10; List <microtrack> lm = TrackSelector.Select(lit, tsp); foreach (microtrack m in lm) { double viewx = viewpoint.X; double viewy = viewpoint.Y; double pixelx = 135.0 / 512.0; double pixely = 115.0 / 440.0; double x = viewx - (m.cx - 256) * pixelx; double y = viewy + (m.cy - 220) * pixely; Console.WriteLine(string.Format("{0:0.0} {1:0.0} {2:0.0} {3:0.0} {4:0.0} {5:0.0} {6:0.0} {7:0.0}", m.ph, m.pv, m.ax, m.ay, x, y, m.cx, m.cy)); } vx++; } } //vx } //vy //下面 ベース中からはじめ、ベース下側を表面認識 //ベース下側からはじめてZ方向負の向きにスキャン mc.MoveTo(new Vector3(blockstartpoint.X + 0.5, blockstartpoint.Y + 0.5, initialpoint.Z - 0.140)); mc.Join(); camera.Start(); surfrecog(pixthre, -0.003); camera.Stop(); double surfaceZdown = mc.GetPoint().Z; for (int vy = 0; vy < 10; vy++) { Vector3 linestartpoint = mc.GetPoint(); linestartpoint.X = blockstartpoint.X; linestartpoint.Y = blockstartpoint.Y + vy * parameterManager.SpiralShiftY; linestartpoint.Z = surfaceZdown; for (int vx = 0; vx < 8;) { if (vx == 0) { Vector3 approachingpoint = mc.GetPoint(); approachingpoint.X = blockstartpoint.X + vx * parameterManager.SpiralShiftX - 0.05; approachingpoint.Y = blockstartpoint.Y + vy * parameterManager.SpiralShiftY - 0.05; approachingpoint.Z = linestartpoint.Z + 0.006; mc.MoveTo(approachingpoint); mc.Join(); } Vector3 viewstartpoint = mc.GetPoint(); viewstartpoint.X = blockstartpoint.X + vx * parameterManager.SpiralShiftX; viewstartpoint.Y = blockstartpoint.Y + vy * parameterManager.SpiralShiftY; viewstartpoint.Z = linestartpoint.Z + 0.006; mc.MoveTo(viewstartpoint); mc.Join(); Thread.Sleep(100); Vector3 viewpoint = mc.GetPoint(); byte[] bb = new byte[440 * 512 * 16]; string datfileName = string.Format(@"E:\img\d_{0}_{1}_{2}_{3}.dat", (int)(viewpoint.X * 1000), (int)(viewpoint.Y * 1000), vx, vy ); BinaryWriter writer = new BinaryWriter(File.Open(datfileName, FileMode.Create)); mc.Inch(PlusMinus.Minus, 0.15, VectorId.Z); int viewcounter = 0; while (viewcounter < 16 + 3) { byte[] b = Ipt.CaptureMain(); Vector3 p = mc.GetPoint(); if (viewcounter >= 3) { b.CopyTo(bb, 440 * 512 * (viewcounter - 3)); string stlog = ""; stlog += String.Format("{0} {1} {2} {3}\n", ledbrightness, p.X, p.Y, p.Z); twriter.Write(stlog); } viewcounter++; }//view viewcounter = 0; double endz = mc.GetPoint().Z; mc.SlowDownStop(VectorId.Z); mc.Join(); Thread.Sleep(100); if (viewstartpoint.Z - endz < 0.070) { vx++; writer.Write(bb); writer.Flush(); writer.Close(); } } //vx } //vy camera.Stop(); twriter.Close(); } //blocky } //blockx } //task
private void task() { MotorControler mc = MotorControler.GetInstance(parameterManager); Surface sur = Surface.GetInstance(parameterManager); Camera camera = Camera.GetInstance(); Led led = Led.GetInstance(); Vector3 InitPoint = mc.GetPoint(); Vector3 p = new Vector3(); double emthickness = sur.UpTop - sur.UpBottom; int nshot = (int)(emthickness / 0.003); int blockXCounter = 0; int blockYCounter = 0; while (blockYCounter < nyView) { while (blockXCounter < nxView) { string txtfileName = string.Format(@"{0}\{1}.txt" , direcotryPath , System.DateTime.Now.ToString("yyyyMMdd_HHmmss_ffff") ); StreamWriter twriter = File.CreateText(txtfileName); //Vector3 InitPointofThisBlock = new Vector3( // InitPoint.X + blockXCounter * 4.350, // InitPoint.Y + blockYCounter * 4.390, // InitPoint.Z // ); //Vector3 SurfPointofThisBlock = new Vector3( // InitPointofThisBlock.X + 2.200, // InitPointofThisBlock.Y + 2.200, // InitPoint.Z // ); Vector3 InitPointofThisBlock = new Vector3( InitPoint.X + (double)(blockXCounter) * ((0.210 - 0.01) * 10 - 0.030), //if x40 -> 2.150, InitPoint.Y - (double)(blockYCounter) * ((0.180 - 0.01) * 10 - 0.030), //if x40 -> 2.170, InitPoint.Z ); Vector3 SurfPointofThisBlock = new Vector3( InitPointofThisBlock.X + 1.000, InitPointofThisBlock.Y - 1.000, InitPoint.Z ); //go to surface measurement mc.MovePoint(SurfPointofThisBlock.X, SurfPointofThisBlock.Y, sur.UpTop + 0.050);//above 50micron mc.Join(); //surface landing bool flag = true; int layercounter = 0; camera.Start(); 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(31, 31), -1); Cv2.Subtract(gau, mat, mat); Cv2.Threshold(mat, mat, 10, 1, ThresholdType.Binary); int brightness = Cv2.CountNonZero(mat); layercounter++; if (brightness > 10000 || layercounter > 30) { flag = false; } } led.AdjustLight(parameterManager); camera.Stop(); //surface double surfacetopz = mc.GetPoint().Z; double surfacebottomz = surfacetopz - emthickness; //data taking int rowcounter = 0; int colcounter = 0; //while (rowcounter < 24) { // while (colcounter < 20) { while (rowcounter < 12) { while (colcounter < 10) { string stlog = ""; byte[] bb = new byte[440 * 512 * nshot]; double startZ = 0.0; PlusMinus plusminus; if (colcounter % 2 == 0) { //camera.Start(); //led.AdjustLight(parameterManager); //camera.Stop(); startZ = surfacetopz + 0.012; plusminus = PlusMinus.Minus; } else { startZ = surfacebottomz - 0.009; plusminus = PlusMinus.Plus; } double prev_z = startZ; mc.MovePoint( InitPointofThisBlock.X + (0.210 - 0.01) * colcounter, //x40, 0.230-0.01 //parameterManager.SpiralShiftX InitPointofThisBlock.Y - (0.180 - 0.01) * rowcounter, //x40, 0.195-0.01 //parameterManager.SpiralShiftY startZ); mc.Join(); p = mc.GetPoint(); DateTime starttime = System.DateTime.Now; string datfileName = string.Format(@"{0}\{1}_x{2}_y{3}_xi{4}_yi{5}.dat", direcotryPath, starttime.ToString("yyyyMMdd_HHmmss"), (int)(p.X * 1000), (int)(p.Y * 1000), colcounter, rowcounter ); BinaryWriter writer = new BinaryWriter(File.Open(datfileName, FileMode.Create)); mc.Inch(plusminus, 0.15, VectorId.Z); int viewcounter = 0; while (viewcounter < nshot + 3) { 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", colcounter % 2, 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); prev_z = p.Z; if (viewcounter >= 3) { b.CopyTo(bb, 440 * 512 * (viewcounter - 3)); } viewcounter++; }//view viewcounter = 0; mc.SlowDownStop(VectorId.Z); twriter.Write(stlog); writer.Write(bb); writer.Flush(); writer.Close(); colcounter++; }//col colcounter = 0; rowcounter++; }//row rowcounter = 0; twriter.Close(); blockXCounter++; }//blockX blockXCounter = 0; blockYCounter++; }//blockY blockYCounter = 0; camera.Start(); }//end of task()
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
private void task() { try { 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(); int viewcounter = 0; //string txtfileName = string.Format(@"{0}\{1}.txt", // direcotryPath, System.DateTime.Now.ToString("yyyyMMdd_HHmmss_ffff")); //StreamWriter twriter = File.CreateText(txtfileName); //text読み込み // List<pointscan> PSList = new List<pointscan>(); List <Point2d> PSList = new List <Point2d>(); var reader = new StreamReader(File.OpenRead(@"C:\affine_position.txt")); //bool headerflag = true; while (!reader.EndOfStream) { var line = reader.ReadLine(); string[] delimiter = { " " }; var values = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); Point2d p2 = new Point2d( double.Parse(values[0]), double.Parse(values[1]) ); PSList.Add(p2); } int pixthre = 500; for (int pp = 0; pp < PSList.Count(); pp++) { viewcounter = 0; Vector3 movepoint = new Vector3(PSList[pp].X, PSList[pp].Y, InitPoint.Z); mc.MoveTo(movepoint); //camera.Start(); //surfrecog(pixthre, 0.003); //camera.Stop(); //double surfaceZup = mc.GetPoint().Z; //movepoint.Z = surfaceZup; //mc.MoveTo(movepoint); mc.Join(); //SurfPoint = mc.GetPoint(); camera.Start(); led.AdjustLight(parameterManager); camera.Stop(); Vector3 p = mc.GetPoint(); byte[] b = Ipt.CaptureMain(); Mat src = new Mat(440, 512, MatType.CV_8U, b); String filename = String.Format(@"C:\img\grid\{0}_{1}_.png", (p.X * 1000).ToString("0.0"), (p.Y * 1000).ToString("0.0")); Cv2.ImWrite(filename, src); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); } camera.Start(); } catch (SystemException) { } }
private void task() { // レンズが50倍に設定されていない場合は例外を返すようにしたいがやり方が分からん(20140724) 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"); } try { MotorControler mc = MotorControler.GetInstance(parameterManager); 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); } }
private void BeamFollower(Track myTrack, int mod, int pl, bool dubflag) { MotorControler mc = MotorControler.GetInstance(parameterManager); Camera camera = Camera.GetInstance(); Surface surface = Surface.GetInstance(parameterManager); Vector3 initial = mc.GetPoint(); string uptxt = string.Format(@"c:\GTR_test\bpm\{0}_{1}_up.txt", 10, 10); BeamDetection(uptxt, true); List <Point2d> BPM_pix = new List <Point2d>();//beamのピクセル座標を格納するListである。 string line; System.IO.StreamReader file = new System.IO.StreamReader(uptxt); while ((line = file.ReadLine()) != null) { string[] data = line.Split(' '); double sx = double.Parse(data[0]); double sy = double.Parse(data[1]); BPM_pix.Add(new Point2d(sx, sy)); } file.Close(); //ここからは、画像の中心(256,220)に近いいくつかのbeamの座標を選択し、それを追跡するように修正する。 List <Point2d> some_bpm = new List <Point2d>(); List <Point3d> point = new List <Point3d>(); List <Point2d> point_10 = new List <Point2d>(); for (int i = 0; i < BPM_pix.Count(); i++) { List <Point3d> point2 = new List <Point3d>(); for (int r = 0; r < BPM_pix.Count(); r++) { Point3d p = new Point3d(); double dx = BPM_pix[i].X - BPM_pix[r].X; double dy = BPM_pix[i].Y - BPM_pix[r].Y; double dr = Math.Sqrt(dx * dx + dy * dy); if (dr < 7) {//この7という数字は、windowsizeが一辺10ピクセルのため、全体で見た時に7ピクセル離れていれば良いだろうと判断し、このようにした。 p.X = 10; p.Y = 10; p.Z = 10; point2.Add(p); } }//for r if (point2.Count() == 1) { Point2d bem = new Point2d(); bem.X = BPM_pix[i].X; bem.Y = BPM_pix[i].Y; point_10.Add(bem); } }//for i //ここまで int bemcount = 0; if (point_10.Count() >= 5) { bemcount = 5; } else { bemcount = point_10.Count(); } for (int i = 0; i < bemcount; i++)//ここで、領域における分け方も含めてbeamを選択できるようにする。 { some_bpm.Add(new Point2d(point_10[i].X, point_10[i].Y)); } //ただ、とりあえずこれで作動はするであろう形になった。 List <List <pozi> > LBeam = new List <List <pozi> >(); List <List <pozi> > LBeam_Low = new List <List <pozi> >(); List <pozi> c2 = new List <pozi>(); List <Point2d> PM_result = some_bpm;//パターンマッチの結果から取得したbeamのpixel座標 for (int a = 0; a < PM_result.Count(); a++) { pozi c3 = new pozi(); c3.img.X = initial.X; c3.img.Y = initial.Y; c3.img.Z = initial.Z; c3.peak = PM_result[a]; double firstx = c3.img.X - (c3.peak.X - 256) * 0.000267; double firsty = c3.img.Y + (c3.peak.Y - 220) * 0.000267; double firstz = c3.img.Z; c3.stage = new Point3d(firstx, firsty, firstz); c2.Add(c3); } LBeam.Add(c2);//第一層目でのbeamの情報をぶち込む。 //for up layer int number_of_images = 7; int hits = 4; double Sh = 0.5 / (surface.UpTop - surface.UpBottom); double theta = Math.Atan(0); double dz; double dz_price_img = (6 * Math.Cos(theta) / Sh) / 1000; double dz_img = dz_price_img * (-1); string datarootdirpath = string.Format(@"C:\GTR_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>(); //stage移動のための角度を入れるList。 Msdxdy.Add(new OpenCvSharp.CPlusPlus.Point2d(0.0, 0.0)); //最初はbeamが垂直に原子核乾板に照射されていると考えて(0.0,0.0)を入れた。 List <OpenCvSharp.CPlusPlus.Point3d> LStage = new List <OpenCvSharp.CPlusPlus.Point3d>(); //ImageTakeingで撮影した画像の最後の画像の座標を入れるList。 List <OpenCvSharp.CPlusPlus.Point3d> LCenter = new List <OpenCvSharp.CPlusPlus.Point3d>(); //検出したbemaのずれから算出した、本来の画像の中心点のstage座標。 List <List <ImageTaking> > UpTrackInfo = new List <List <ImageTaking> >(); //for down layer................................................................ // tl.Rec("down"); 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.Point3d> LCenter_Low = new List <OpenCvSharp.CPlusPlus.Point3d>();//検出したbemaのずれから算出した、本来の画像の中心点のstage座標。 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 = (number_of_images - 1) * dz_img; double predpoint = moverange + initialpos.Z; if (predpoint < surface.UpBottom) { gotobase = 1; dz = surface.UpBottom - initialpos.Z + (number_of_images - 1) * dz_price_img; } //gotobase = 1のときは、移動して画像を撮影するようにする。 if (i != 0)//このままでOK { Vector3 dstpoint = new Vector3( LCenter[i - 1].X + Msdxdy[i].X * dz * Sh, LCenter[i - 1].Y + Msdxdy[i].Y * dz * Sh, LCenter[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 number_of_images); //number of images LStage.Add(new OpenCvSharp.CPlusPlus.Point3d( LiITUpMid[number_of_images - 1].StageCoord.X, LiITUpMid[number_of_images - 1].StageCoord.Y, LiITUpMid[number_of_images - 1].StageCoord.Z )); //撮影した画像の最後の画像の座標を LStage に代入する。 LiITUpMid[number_of_images - 1].img.ImWrite(datarootdirpath + string.Format(@"\img_l_up_{0}.png", i)); //最後の画像だけ別で保存。 UpTrackInfo.Add(LiITUpMid); //撮影した画像すべてを UpTrackInfo に代入。 List <Mat> binimages = new List <Mat>(); //撮影した画像に対して画像処理をかける。 for (int t = 0; t <= number_of_images - 1; t++) { Mat bin = (Mat)DogContrastBinalize(LiITUpMid[t].img, 31, 60); Cv2.Dilate(bin, bin, new Mat()); binimages.Add(bin); } List <pozi> beam_data = new List <pozi>(); //各stepごとで検出したbeamそれぞれのデータを格納する。 List <Point2d> MSDXDY_BEAM = new List <Point2d>(); //それぞれのbeamから算出した角度を格納するList。 for (int r = 0; r < LBeam[0].Count(); r++) //検出したbeamの数だけ処理を行うようにする。 { pozi beam_pozi = new pozi(); beam_pozi.img = LStage[i];//画像の撮影場所を格納する。 //trackを重ねる処理を入れる。 Point2d beam_peak = TrackDetection_verold(//シフトしないようにして、処理を行うようにしよう。 binimages, (int)LBeam[i][r].peak.X, (int)LBeam[i][r].peak.Y, 0, //shiftx もともと 3 0, //shifty もともと 3 4, 10, //windowsize もともと 90 hits, true); // true); if (beam_peak.X == -1 & beam_peak.Y == -1) {//検出できなかった時にどのような処理を行うのかを考えたほうがいいだろうな。 mc.Join(); not_detect = 1; //goto not_detect_track; とりあえずコメントアウトしておく } beam_pozi.peak.X = beam_peak.X; beam_pozi.peak.Y = beam_peak.Y; double firstx = beam_pozi.img.X - (beam_pozi.peak.X - 256) * 0.000267; double firsty = beam_pozi.img.Y + (beam_pozi.peak.Y - 220) * 0.000267; double firstz = beam_pozi.img.Z; beam_pozi.stage = new Point3d(firstx, firsty, firstz); beam_data.Add(beam_pozi); }//r_loop Point2d Ms_esti = new Point2d(); double Ms_x = new double(); double Ms_y = new double(); int pix_dX = new int(); int pix_dY = new int(); LBeam.Add(beam_data); for (int k = 0; k < LBeam[i].Count(); k++) { if (i == 0) { MSDXDY_BEAM.Add(new OpenCvSharp.CPlusPlus.Point2d(Msdxdy[i].X, Msdxdy[i].Y)); } else { if (i == 1) { Point3d LTrack_ghost = new Point3d(); double dzPrev = (LBeam[i][k].stage.Z - surface.UpTop) * Sh; double Lghost_x = LBeam[i][k].stage.X - Msdxdy[i].X * dzPrev; double Lghost_y = LBeam[i][k].stage.Y - Msdxdy[i].Y * dzPrev; LTrack_ghost = new Point3d(Lghost_x, Lghost_y, surface.UpTop); //上側乳剤層上面にtrackがあるならどの位置にあるかを算出する。 OpenCvSharp.CPlusPlus.Point2d Tangle = ApproximateStraight(Sh, LTrack_ghost, LBeam[i][k].stage, LBeam[i + 1][k].stage); //ここを2点で行うようにする。 MSDXDY_BEAM.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle.X, Tangle.Y)); } else { OpenCvSharp.CPlusPlus.Point2d Tangle = ApproximateStraight(Sh, LBeam[i - 1][k].stage, LBeam[i][k].stage, LBeam[i + 1][k].stage); MSDXDY_BEAM.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle.X, Tangle.Y)); } } } for (int q = 0; q < MSDXDY_BEAM.Count(); q++) //ここで個々のbeamの角度を平均してstageの移動角度を算出する。 { Ms_x += MSDXDY_BEAM[q].X; Ms_y += MSDXDY_BEAM[q].Y; //pix_dX += (int)LBeam[i][q - 1].peak.X - (int)LBeam[i][q].peak.X;//q - 1 がおかしい。ここで何をしたかったのかを思い出そう。 //pix_dY += (int)LBeam[i][q - 1].peak.Y - (int)LBeam[i][q].peak.Y;//予想した地点とのピクセルのズレかな? pix_dX += (int)LBeam[i + 1][q].peak.X - (int)LBeam[i][q].peak.X; pix_dY += (int)LBeam[i + 1][q].peak.Y - (int)LBeam[i][q].peak.Y; } Ms_x = Ms_x / MSDXDY_BEAM.Count(); Ms_y = Ms_y / MSDXDY_BEAM.Count(); Ms_esti = new Point2d(Ms_x, Ms_y); Msdxdy.Add(Ms_esti);//算出した角度をぶち込む。 //LBeam.Add(beam_data); pix_dX = pix_dX / MSDXDY_BEAM.Count(); //ずれたピクセル量 pix_dY = pix_dY / MSDXDY_BEAM.Count(); //ずれたピクセル量 double cenX = LStage[i].X - pix_dX * 0.000267; double cenY = LStage[i].Y + pix_dY * 0.000267; double cenZ = LStage[i].Z; LCenter.Add(new Point3d(cenX, cenY, cenZ));//検出したそれぞれのbeamのズレから算出したパターンマッチの際の中心座標(stage)。 }//for i-loop //baseまたぎ int lcen_counter = LCenter.Count(); int msdxdy_counter = Msdxdy.Count(); mc.MovePoint( LCenter[lcen_counter - 1].X + Msdxdy[msdxdy_counter - 1].X * (surface.LowTop - surface.UpBottom), LCenter[lcen_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)); int lbeam_counter = LBeam.Count(); LBeam_Low.Add(LBeam[lbeam_counter - 1]); //今までの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 = (number_of_images - 1) * dz_img; double predpoint = moverange + initialpos.Z; if (predpoint < surface.LowBottom)//もしもbaseに入りそうなら、8枚目の画像がちょうど下gelを撮影するようにdzを調整する。 { goto_dgel = 1; dz = surface.LowBottom - initialpos.Z + (number_of_images - 1) * dz_price_img; } //////// //goto_dgel == 1のときは、移動して画像を撮影するようにする。 if (i != 0) { Vector3 dstpoint = new Vector3( LCenter_Low[i - 1].X + Msdxdy_Low[i].X * dz * Sh_low, LCenter_Low[i - 1].Y + Msdxdy_Low[i].Y * dz * Sh_low, LCenter_Low[i - 1].Z + dz ); mc.MovePoint(dstpoint); mc.Join(); } //今までのtrack追跡プログラムとは異なる角度等の使い方をする。 List <ImageTaking> LiITLowMid = TakeSequentialImage( Msdxdy_Low[i].X * Sh_low, Msdxdy_Low[i].Y * Sh_low, dz_img, number_of_images); //画像・座標の記録 LStage_Low.Add(new OpenCvSharp.CPlusPlus.Point3d( LiITLowMid[number_of_images - 1].StageCoord.X, LiITLowMid[number_of_images - 1].StageCoord.Y, LiITLowMid[number_of_images - 1].StageCoord.Z)); LiITLowMid[number_of_images - 1].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 <= number_of_images - 1; t++) { Mat bin = (Mat)DogContrastBinalize(LiITLowMid[t].img, 31, 60); Cv2.Dilate(bin, bin, new Mat()); binimages.Add(bin); } List <pozi> beam_data_low = new List <pozi>(); //各stepごとで検出したbeamそれぞれのデータを格納する。 List <Point2d> MSDXDY_BEAM_LOW = new List <Point2d>(); //それぞれのbeamから算出した角度を格納するList。 for (int r = 0; r < LBeam_Low[0].Count(); r++) { pozi beam_pozi = new pozi(); beam_pozi.img = LStage_Low[i];//画像の撮影場所を格納する。 //trackを重ねる処理を入れる。 Point2d beam_peak = TrackDetection_verold( binimages, (int)LBeam_Low[i][r].peak.X, (int)LBeam_Low[i][r].peak.Y, 0, 0, 4, 10, hits);// true); if (beam_peak.X == -1 & beam_peak.Y == -1) { mc.Join(); not_detect = 1; //goto not_detect_track; とりあえずコメントアウトしておく } beam_pozi.peak.X = beam_peak.X; beam_pozi.peak.Y = beam_peak.Y; double firstx = beam_pozi.img.X - (beam_pozi.peak.X - 256) * 0.000267; double firsty = beam_pozi.img.Y + (beam_pozi.peak.Y - 220) * 0.000267; double firstz = beam_pozi.img.Z; beam_pozi.stage = new Point3d(firstx, firsty, firstz); beam_data_low.Add(beam_pozi); }//r_loop Point2d Ms_esti = new Point2d(); double Ms_x = new double(); double Ms_y = new double(); int pix_dX = new int(); int pix_dY = new int(); LBeam_Low.Add(beam_data_low); for (int k = 0; k < LBeam_Low[i].Count(); k++) { if (i == 0) { OpenCvSharp.CPlusPlus.Point2d Tangle_l = ApproximateStraightBase( Sh, Sh_low, LBeam[lbeam_counter - 2][k].stage, LBeam[lbeam_counter - 1][k].stage, LBeam_Low[i][k].stage, surface); MSDXDY_BEAM_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, LBeam[lbeam_counter - 1][k].stage, LBeam_Low[i - 1][k].stage, LBeam_Low[i][k].stage, surface); MSDXDY_BEAM_LOW.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle_l.X, Tangle_l.Y)); } else { OpenCvSharp.CPlusPlus.Point2d Tangle_l = ApproximateStraight( Sh_low, LBeam_Low[i - 2][k].stage, LBeam_Low[i - 1][k].stage, LBeam_Low[i][k].stage); MSDXDY_BEAM_LOW.Add(new OpenCvSharp.CPlusPlus.Point2d(Tangle_l.X, Tangle_l.Y)); } }//roop_k for (int q = 0; q < MSDXDY_BEAM_LOW.Count(); q++) //ここで個々のbeamの角度を平均してstageの移動角度を算出する。 { Ms_x += MSDXDY_BEAM_LOW[q].X; Ms_y += MSDXDY_BEAM_LOW[q].Y; pix_dX += (int)LBeam_Low[i + 1][q].peak.X - (int)LBeam_Low[i][q].peak.X; pix_dY += (int)LBeam_Low[i + 1][q].peak.Y - (int)LBeam_Low[i][q].peak.Y; } Ms_x = Ms_x / MSDXDY_BEAM_LOW.Count(); Ms_y = Ms_y / MSDXDY_BEAM_LOW.Count(); Ms_esti = new Point2d(Ms_x, Ms_y); Msdxdy_Low.Add(Ms_esti);//算出した角度をぶち込む。 pix_dX = pix_dX / MSDXDY_BEAM_LOW.Count(); //ずれたピクセル量 pix_dY = pix_dY / MSDXDY_BEAM_LOW.Count(); //ずれたピクセル量 double cenX = LStage_Low[i].X - pix_dX * 0.000267; double cenY = LStage_Low[i].Y + pix_dY * 0.000267; double cenZ = LStage_Low[i].Z; LCenter_Low.Add(new Point3d(cenX, cenY, cenZ));//検出したそれぞれのbeamのズレから算出したパターンマッチの際の中心座標(stage)。 }//i_loop // int lcen_low_count = LCenter_Low.Count(); mc.MovePointXY(LCenter_Low[lcen_low_count - 1].X, LCenter_Low[lcen_low_count - 1].Y); mc.Join(); //検出に失敗した場合は、ループを抜けてここに来る。 //file write out up_gel 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}.png", 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_lbeam = datarootdirpath + string.Format(@"\lbeam_up.txt"); StreamWriter twriter_lbeam = File.CreateText(txtfileName_lbeam); for (int i = 0; i < LBeam.Count(); i++) { for (int r = 0; r < LBeam[i].Count(); r++) { twriter_lbeam.WriteLine("{0} {1} BeamPeak: {2} {3} LBeam(Stage): {4} {5} {6}", i, r, LBeam[i][r].peak.X, LBeam[i][r].peak.Y, LBeam[i][r].stage.X, LBeam[i][r].stage.Y, LBeam[i][r].stage.Z); } } twriter_lbeam.Close(); string txtfileName_LCenter = datarootdirpath + string.Format(@"\LCenter_up.txt"); StreamWriter twriter_LCenter = File.CreateText(txtfileName_LCenter); for (int i = 0; i < LCenter.Count(); i++) { twriter_LCenter.WriteLine("{0} {1} {2}", LCenter[i].X, LCenter[i].Y, LCenter[i].Z); } twriter_LCenter.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}.png", 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_lbeam_low = datarootdirpath + string.Format(@"\lbeam_low.txt"); StreamWriter twriter_lbeam_low = File.CreateText(txtfileName_lbeam_low); for (int i = 0; i < LBeam_Low.Count(); i++) { for (int r = 0; r < LBeam_Low[i].Count(); r++) { twriter_lbeam_low.WriteLine("{0} {1} BeamPeak: {2} {3} LBeam(Stage): {4} {5} {6}", i, r, LBeam_Low[i][r].peak.X, LBeam_Low[i][r].peak.Y, LBeam_Low[i][r].stage.X, LBeam_Low[i][r].stage.Y, LBeam_Low[i][r].stage.Z); } } twriter_lbeam_low.Close(); string txtfileName_LCenter_low = datarootdirpath + string.Format(@"\LCenter_low.txt"); StreamWriter twriter_LCenter_low = File.CreateText(txtfileName_LCenter_low); for (int i = 0; i < LCenter_Low.Count(); i++) { twriter_LCenter_low.WriteLine("{0} {1} {2}", LCenter_Low[i].X, LCenter_Low[i].Y, LCenter_Low[i].Z); } twriter_LCenter_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(); }
/// <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); }
private void task() { TracksManager tm = parameterManager.TracksManager; Track myTrack = tm.GetTrack(tm.TrackingIndex); MotorControler mc = MotorControler.GetInstance(parameterManager); Camera camera = Camera.GetInstance(); List <Mat> image_set = new List <Mat>(); List <Mat> image_set_reverse = new List <Mat>(); Surface surface = Surface.GetInstance(parameterManager);//表面認識から境界値を取得 double uptop = surface.UpTop; double upbottom = surface.UpBottom; double lowtop = surface.LowTop; double lowbottom = surface.LowBottom; double now_x = mc.GetPoint().X; double now_y = mc.GetPoint().Y; double now_z = mc.GetPoint().Z; common_dx = myTrack.MsDX + ((0.265625 * over_dx * 3) / (0.024 * 2.2 * 1000)); common_dy = myTrack.MsDY - ((0.265625 * over_dy * 3) / (0.024 * 2.2 * 1000)); for (int i = 0; i < 8; i++) { //myTrack.MsD○はdz1mmあたりのd○の変位mm double next_x = now_x - i * common_dx * 0.003 * 2.2; //3μm間隔で撮影 double next_y = now_y - i * common_dy * 0.003 * 2.2; //Shrinkage Factor は2.2で計算(仮) mc.MovePoint(next_x, next_y, now_z - 0.003 * i); mc.Join(); byte[] b = camera.ArrayImage; Mat image = new Mat(440, 512, MatType.CV_8U, b); Mat imagec = image.Clone(); image_set.Add(imagec); } for (int i = 7; i >= 0; i--) { image_set_reverse.Add(image_set[i]); } int n = image_set.Count();//1回分の取得画像の枚数 Mat cont = new Mat(440, 512, MatType.CV_8U); Mat gau_1 = new Mat(440, 512, MatType.CV_8U); Mat gau_2 = new Mat(440, 512, MatType.CV_8U); Mat sub = new Mat(440, 512, MatType.CV_8U); Mat bin = new Mat(440, 512, MatType.CV_8U); double Max_kido; double Min_kido; OpenCvSharp.CPlusPlus.Point maxloc; OpenCvSharp.CPlusPlus.Point minloc; List <Mat> two_set = new List <Mat>(); List <Mat> Part_img = new List <Mat>(); for (int i = 0; i < image_set.Count(); i++) { Cv2.GaussianBlur((Mat)image_set_reverse[i], gau_1, Cv.Size(3, 3), -1); //パラメータ見ないといけない。 Cv2.GaussianBlur(gau_1, gau_2, Cv.Size(51, 51), -1); //パラメータ見ないといけない。 Cv2.Subtract(gau_2, gau_1, sub); Cv2.MinMaxLoc(sub, out Min_kido, out Max_kido, out minloc, out maxloc); cont = (sub - Min_kido) * 255 / (Max_kido - Min_kido); cont.ImWrite(string.Format(@"C:\set\cont_{0}.bmp", i)); Cv2.Threshold(cont, bin, 115, 1, ThresholdType.Binary);//パラメータ見ないといけない。 two_set.Add(bin); } List <mm> white_area = new List <mm>(); int x0 = 256; int y0 = 220;//視野の中心 for (int delta_xx = -1; delta_xx <= 1; delta_xx++)//一番下の画像よりどれだけずらすか { for (int delta_yy = -1; delta_yy <= 1; delta_yy++) { { // //積層写真の型作り(行列の中身は0行列) // Mat superimposed = Mat.Zeros(440 + (n - 1) * Math.Abs(delta_yy), 512 + (n - 1) * Math.Abs(delta_xx), MatType.CV_8UC1); // // //各写真の型作り // for (int i = 0; i < two_set.Count; i++) { // Mat Part = Mat.Zeros(440 + (n - 1) * Math.Abs(delta_yy), 512 + (n - 1) * Math.Abs(delta_xx), MatType.CV_8UC1); // Part_img.Add(Part); // } //積層写真の型作り(行列の中身は0行列) Mat superimposed = Mat.Zeros(440 + 3 * Math.Abs(delta_yy), 512 + 3 * Math.Abs(delta_xx), MatType.CV_8UC1); //各写真の型作り for (int i = 0; i < two_set.Count; i++) { Mat Part = Mat.Zeros(440 + 3 * Math.Abs(delta_yy), 512 + 3 * Math.Abs(delta_xx), MatType.CV_8UC1); Part_img.Add(Part); }//2枚を1セットにしてずらす場合 if (delta_xx >= 0 && delta_yy >= 0)//画像の右下への移動 { for (int i = 0; i < two_set.Count; i++) { if (i == 0 || i == 1) { Part_img[i][ 0 , 440 , 0 , 512 ] = two_set[i]; //処理済み画像をPartの対応する部分に入れていく } else if (i == 2 || i == 3) { Part_img[i][ 0 + Math.Abs(delta_yy) //yの値のスタート地点 , 440 + Math.Abs(delta_yy) //yの値のゴール地点 , 0 + Math.Abs(delta_xx) //xの値のスタート地点 , 512 + Math.Abs(delta_xx) //xの値のゴール地点 ] = two_set[i]; //処理済み画像をPartの対応する部分に入れていく } else if (i == 4 || i == 5) { Part_img[i][ 0 + 2 * Math.Abs(delta_yy) //yの値のスタート地点 , 440 + 2 * Math.Abs(delta_yy) //yの値のゴール地点 , 0 + 2 * Math.Abs(delta_xx) //xの値のスタート地点 , 512 + 2 * Math.Abs(delta_xx) //xの値のゴール地点 ] = two_set[i]; //処理済み画像をPartの対応する部分に入れていく } else if (i == 6 || i == 7) { Part_img[i][ 0 + 3 * Math.Abs(delta_yy) //yの値のスタート地点 , 440 + 3 * Math.Abs(delta_yy) //yの値のゴール地点 , 0 + 3 * Math.Abs(delta_xx) //xの値のスタート地点 , 512 + 3 * Math.Abs(delta_xx) //xの値のゴール地点 ] = two_set[i]; //処理済み画像をPartの対応する部分に入れていく } } for (int i = 0; i < Part_img.Count(); i++) { superimposed += Part_img[i]; } Cv2.Threshold(superimposed, superimposed, 5, 255, ThresholdType.ToZero);//パラメータ見ないといけない。 superimposed.SubMat(0 , 440 , 0 , 512).CopyTo(superimposed); //1枚目の画像の大きさ、場所で切り取る } if (delta_xx >= 0 && delta_yy < 0)//画像の右上への移動 { for (int i = 0; i < two_set.Count; i++) { if (i == 0 || i == 1) { Part_img[i][ 0 + 3 , 440 + 3 , 0 , 512 ] = two_set[i]; //処理済み画像をPartの対応する部分に入れていく } else if (i == 2 || i == 3) { Part_img[i][ 0 + 3 - 1 //yの値のスタート地点 , 440 + 3 - 1 //yの値のゴール地点 , 0 + Math.Abs(delta_xx) //xの値のスタート地点 , 512 + Math.Abs(delta_xx) //xの値のゴール地点 ] = two_set[i]; //処理済み画像をPartの対応する部分に入れていく } else if (i == 4 || i == 5) { Part_img[i][ 0 + 3 - 2 //yの値のスタート地点 , 440 + 3 - 2 //yの値のゴール地点 , 0 + 2 * Math.Abs(delta_xx) //xの値のスタート地点 , 512 + 2 * Math.Abs(delta_xx) //xの値のゴール地点 ] = two_set[i]; //処理済み画像をPartの対応する部分に入れていく } else if (i == 6 || i == 7) { Part_img[i][ 0 + 3 - 3 //yの値のスタート地点 , 440 + 3 - 3 //yの値のゴール地点 , 0 + 3 * Math.Abs(delta_xx) //xの値のスタート地点 , 512 + 3 * Math.Abs(delta_xx) //xの値のゴール地点 ] = two_set[i]; //処理済み画像をPartの対応する部分に入れていく } } for (int i = 0; i < Part_img.Count(); i++) { superimposed += Part_img[i]; } Cv2.Threshold(superimposed, superimposed, 5, 255, ThresholdType.ToZero);//パラメータ見ないといけない。 superimposed.SubMat(0 + 3 , 440 + 3 , 0 , 512).CopyTo(superimposed); //1枚目の画像の大きさ、場所で切り取る } if (delta_xx < 0 && delta_yy < 0)//画像の左上への移動 { for (int i = 0; i < two_set.Count; i++) { if (i == 0 || i == 1) { Part_img[i][ 0 + 3 , 440 + 3 , 0 + 3 , 512 + 3 ] = two_set[i]; //処理済み画像をPartの対応する部分に入れていく } else if (i == 2 || i == 3) { Part_img[i][ 0 + 3 - 1 //yの値のスタート地点 , 440 + 3 - 1 //yの値のゴール地点 , 0 + 3 - 1 //xの値のスタート地点 , 512 + 3 - 1 //xの値のゴール地点 ] = two_set[i]; //処理済み画像をPartの対応する部分に入れていく } else if (i == 4 || i == 5) { Part_img[i][ 0 + 3 - 2 //yの値のスタート地点 , 440 + 3 - 2 //yの値のゴール地点 , 0 + 3 - 2 //xの値のスタート地点 , 512 + 3 - 2 //xの値のゴール地点 ] = two_set[i]; //処理済み画像をPartの対応する部分に入れていく } else if (i == 6 || i == 7) { Part_img[i][ 0 + 3 - 3 //yの値のスタート地点 , 440 + 3 - 3 //yの値のゴール地点 , 0 + 3 - 3 //xの値のスタート地点 , 512 + 3 - 3 //xの値のゴール地点 ] = two_set[i]; //処理済み画像をPartの対応する部分に入れていく } } for (int i = 0; i < Part_img.Count(); i++) { superimposed += Part_img[i]; } Cv2.Threshold(superimposed, superimposed, 5, 255, ThresholdType.ToZero);//パラメータ見ないといけない。 superimposed.SubMat(0 + 3 , 440 + 3 , 0 + 3 , 512 + 3).CopyTo(superimposed); //1枚目の画像の大きさ、場所で切り取る } if (delta_xx < 0 && delta_yy >= 0)//画像の左下への移動 { for (int i = 0; i < two_set.Count; i++) { if (i == 0 || i == 1) { Part_img[i][ 0 , 440 , 0 + 3 , 512 + 3 ] = two_set[i]; //処理済み画像をPartの対応する部分に入れていく } else if (i == 2 || i == 3) { Part_img[i][ 0 + Math.Abs(delta_yy) //yの値のスタート地点 , 440 + Math.Abs(delta_yy) //yの値のゴール地点 , 0 + 3 - 1 //xの値のスタート地点 , 512 + 3 - 1 //xの値のゴール地点 ] = two_set[i]; //処理済み画像をPartの対応する部分に入れていく } else if (i == 4 || i == 5) { Part_img[i][ 0 + 2 * Math.Abs(delta_yy) //yの値のスタート地点 , 440 + 2 * Math.Abs(delta_yy) //yの値のゴール地点 , 0 + 3 - 2 //xの値のスタート地点 , 512 + 3 - 2 //xの値のゴール地点 ] = two_set[i]; //処理済み画像をPartの対応する部分に入れていく } else if (i == 6 || i == 7) { Part_img[i][ 0 + 3 * Math.Abs(delta_yy) //yの値のスタート地点 , 440 + 3 * Math.Abs(delta_yy) //yの値のゴール地点 , 0 + 3 - 3 //xの値のスタート地点 , 512 + 3 - 3 //xの値のゴール地点 ] = two_set[i]; //処理済み画像をPartの対応する部分に入れていく } } for (int i = 0; i < Part_img.Count(); i++) { superimposed += Part_img[i]; } Cv2.Threshold(superimposed, superimposed, 5, 255, ThresholdType.ToZero);//パラメータ見ないといけない。 superimposed.SubMat(0 , 440 , 0 + 3 , 512 + 3).CopyTo(superimposed); //1枚目の画像の大きさ、場所で切り取る } Mat one1 = Mat.Ones(y0 - 20, 512, MatType.CV_8UC1);//視野の中心からどれだけの窓を開けるか Mat one2 = Mat.Ones(41, x0 - 20, MatType.CV_8UC1); Mat one3 = Mat.Ones(41, 491 - x0, MatType.CV_8UC1); Mat one4 = Mat.Ones(419 - y0, 512, MatType.CV_8UC1); superimposed[0, y0 - 20, 0, 512] = one1 * 0; superimposed[y0 - 20, y0 + 21, 0, x0 - 20] = one2 * 0; superimposed[y0 - 20, y0 + 21, x0 + 21, 512] = one3 * 0; superimposed[y0 + 21, 440, 0, 512] = one4 * 0;//中心から○μmの正方形以外は黒くする。 superimposed.ImWrite("C:\\set\\superimposed25_1.bmp"); using (CvMemStorage storage = new CvMemStorage()) { using (CvContourScanner scanner = new CvContourScanner(superimposed.ToIplImage(), storage, CvContour.SizeOf, ContourRetrieval.Tree, ContourChain.ApproxSimple)) { 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; mm koko = new mm(); koko.white_x = mx; koko.white_y = my; koko.white_kido = mom.M00; koko.white_dx = delta_xx; koko.white_dy = delta_yy; white_area.Add(koko); stage.WriteLine(String.Format("mx={0:f2} , my={1:f2} , dx={2:f2} , dy={3:f2} , M={4:f2}", mx, my, delta_xx, delta_yy, mom.M00)); } } } Part_img.Clear(); } //pixel移動x } //pixel移動y } if (white_area.Count > 0) { double center_x = 0; double center_y = 0; double center_dx = 0; double center_dy = 0; double kido_sum = 0; for (int i = 0; i < white_area.Count; i++) { kido_sum += white_area[i].white_kido; center_x += white_area[i].white_x * white_area[i].white_kido; center_y += white_area[i].white_y * white_area[i].white_kido; center_dx += white_area[i].white_dx * white_area[i].white_kido; center_dy += white_area[i].white_dy * white_area[i].white_kido; } center_x = center_x / kido_sum; center_y = center_y / kido_sum; center_dx = center_dx / kido_sum; center_dy = center_dy / kido_sum; int c_o_g_x; int c_o_g_y; if (center_x >= 0) { c_o_g_x = (int)(center_x + 0.5); } else { c_o_g_x = (int)(center_x - 0.5); } if (center_x >= 0) { c_o_g_y = (int)(center_y + 0.5); } else { c_o_g_y = (int)(center_y - 0.5); } int dx_pixel = c_o_g_x - x0; int dy_pixel = c_o_g_y - y0; double dx_micron = dx_pixel * 0.265625 / 1000; double dy_micron = dy_pixel * 0.265625 / 1000; double now_x2 = mc.GetPoint().X; double now_y2 = mc.GetPoint().Y; mc.MovePointXY(now_x2 - dx_micron, now_y2 + dy_micron);//pixelの軸とstageの軸の関係から mc.Join(); over_dx = center_dx; over_dy = center_dy; } }
private void task() { TracksManager tm = parameterManager.TracksManager; Track myTrack = tm.GetTrack(tm.TrackingIndex); MotorControler mc = MotorControler.GetInstance(parameterManager); Camera camera = Camera.GetInstance(); int nshot = 70; byte[] bb = new byte[440 * 512 * nshot]; Vector3 now_p = mc.GetPoint(); //スライス画像を取るための残し DateTime starttime = System.DateTime.Now; string datfileName = string.Format(@"C:\Documents and Settings\stage1-user\デスクトップ\window_model\7601_135\d.dat"); BinaryWriter writer = new BinaryWriter(File.Open(datfileName, FileMode.Create)); string txtfileName = string.Format(@"C:\Documents and Settings\stage1-user\デスクトップ\window_model\7601_135\d.txt"); StreamWriter twriter = File.CreateText(txtfileName); string stlog = ""; int kk = 0; while (kk < nshot) { byte[] b = camera.ArrayImage;//画像を取得 //Mat image = new Mat(440, 512, MatType.CV_8U, b); //Mat image_clone = image.Clone(); b.CopyTo(bb, 440 * 512 * kk); //image_clone.ImWrite(string.Format(@"E:\20141015\5201_136\{0}.bmp", kk)); stlog += String.Format("{0} {1} {2} {3} {4}\n", System.DateTime.Now.ToString("HHmmss\\.fff"), (now_p.X * 1000).ToString("0.0"), (now_p.Y * 1000).ToString("0.0"), (now_p.Z * 1000).ToString("0.0"), kk); kk++; //mc.MovePointZ(now_p.Z - 0.0020); mc.MoveDistance(-0.0020, VectorId.Z); mc.Join(); } twriter.Write(stlog); writer.Write(bb); writer.Flush(); writer.Close(); return; Surface surface = Surface.GetInstance(parameterManager);//表面認識から境界値を取得 double uptop = surface.UpTop; double upbottom = surface.UpBottom; double lowtop = surface.LowTop; double lowbottom = surface.LowBottom; while (mc.GetPoint().Z >= upbottom + 0.030)//上ゲル内での連続移動 { Follow(); } if (mc.GetPoint().Z >= upbottom + 0.024) { double now_x = mc.GetPoint().X; double now_y = mc.GetPoint().Y; double now_z = mc.GetPoint().Z; mc.MovePoint(now_x - common_dx * (now_z - (upbottom + 0.024)) * 2.2, now_y - common_dy * (now_z - (upbottom + 0.024)) * 2.2, upbottom + 0.024); } else { double now_x = mc.GetPoint().X; double now_y = mc.GetPoint().Y; double now_z = mc.GetPoint().Z; mc.MovePoint(now_x - common_dx * ((upbottom + 0.024) - now_z) * 2.2, now_y - common_dy * ((upbottom + 0.024) - now_z) * 2.2, upbottom + 0.024); } while (mc.GetPoint().Z >= upbottom)//上ゲル内での連続移動 { Follow(); } mc.MovePoint(mc.GetPoint().X - common_dx * (upbottom - lowtop) , mc.GetPoint().Y - common_dy * (upbottom - lowtop) , lowtop);//Base下側への移動 }
}//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); int mod = parameterManager.ModuleNo; int pl = parameterManager.PlateNo; 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); } mc.Join(); 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。 "); } }
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() { MotorControler mc = MotorControler.GetInstance(parameterManager); Surface sur = Surface.GetInstance(parameterManager); Camera camera = Camera.GetInstance(); Led led = Led.GetInstance(); CoordManager cm = new CoordManager(parameterManager); Vector3 initialpoint = mc.GetPoint(); int pixthre = 500; for (int bx = -1; bx <= 1; bx++) { for (int by = -1; by <= 1; by++) { string txtfileName = string.Format(@"E:\img\{0}_{1}.txt", bx, by); StreamWriter twriter = File.CreateText(txtfileName); Vector3 blockstartpoint = new Vector3(); blockstartpoint.X = initialpoint.X + bx * 1.0; blockstartpoint.Y = initialpoint.Y + by * 1.0; blockstartpoint.Z = initialpoint.Z; mc.MoveTo(new Vector3(blockstartpoint.X + 0.5, blockstartpoint.Y + 0.5, initialpoint.Z)); mc.Join(); camera.Start(); int ledbrightness = led.AdjustLight(parameterManager); camera.Stop(); //初期位置からはじめてZ方向正の向きにスキャン for (int vy = 0; vy < 10; vy++) { Vector3 linestartpoint = mc.GetPoint(); linestartpoint.X = blockstartpoint.X; linestartpoint.Y = blockstartpoint.Y + vy * parameterManager.SpiralShiftY; linestartpoint.Z = initialpoint.Z; for (int vx = 0; vx < 8;) { if (vx == 0) { Vector3 approachingpoint = mc.GetPoint(); approachingpoint.X = blockstartpoint.X + vx * parameterManager.SpiralShiftX - 0.05; approachingpoint.Y = blockstartpoint.Y + vy * parameterManager.SpiralShiftY - 0.05; approachingpoint.Z = linestartpoint.Z; mc.MoveTo(approachingpoint); mc.Join(); } Vector3 viewstartpoint = mc.GetPoint(); viewstartpoint.X = blockstartpoint.X + vx * parameterManager.SpiralShiftX; viewstartpoint.Y = blockstartpoint.Y + vy * parameterManager.SpiralShiftY; viewstartpoint.Z = linestartpoint.Z; mc.MoveTo(viewstartpoint); mc.Join(); Thread.Sleep(100); Vector3 viewpoint = mc.GetPoint(); int npict = 300; byte[] bb = new byte[440 * 512 * npict]; string datfileName = string.Format(@"E:\img\u_{0}_{1}_{2}_{3}.dat", (int)(viewpoint.X * 1000), (int)(viewpoint.Y * 1000), vx, vy ); BinaryWriter writer = new BinaryWriter(File.Open(datfileName, FileMode.Create)); mc.Inch(PlusMinus.Plus, 0.15 / 12, VectorId.Z); int viewcounter = 0; while (viewcounter < npict + 3) { byte[] b = Ipt.CaptureMain(); Vector3 p = mc.GetPoint(); if (viewcounter >= 3) { b.CopyTo(bb, 440 * 512 * (viewcounter - 3)); string stlog = ""; stlog += String.Format("{0} {1} {2} {3}\n", ledbrightness, p.X, p.Y, p.Z); twriter.Write(stlog); } viewcounter++; }//view viewcounter = 0; double endz = mc.GetPoint().Z; mc.SlowDownStop(VectorId.Z); mc.Join(); Thread.Sleep(100); if (endz - viewstartpoint.Z < 0.15) { vx++; writer.Write(bb); writer.Flush(); writer.Close(); } } //vx } //vy camera.Stop(); twriter.Close(); } //blocky } //blockx } //task
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 task() { Camera camera = Camera.GetInstance(); MotorControler mc = MotorControler.GetInstance(parameterManager); Vector3 CurrentPoint = mc.GetPoint(); Vector3 p = new Vector3(); int BinarizeThreshold = 10; int BrightnessThreshold = 7; Mat sum = Mat.Zeros(440, 512, MatType.CV_8UC1); string datfileName = string.Format(@"c:\img\{0}.dat", System.DateTime.Now.ToString("yyyyMMdd_HHmmss_fff")); BinaryWriter writer = new BinaryWriter(File.Open(datfileName, FileMode.Create)); for (int i = 0; i < 10; i++) { byte[] b = camera.ArrayImage; writer.Write(b); p = mc.GetPoint(); Mat mat = new Mat(440, 512, MatType.CV_8U, b); mat.ImWrite(String.Format(@"c:\img\{0}_{1}_{2}_{3}.bmp", System.DateTime.Now.ToString("yyyyMMdd_HHmmss_fff"), (int)(p.X * 1000), (int)(p.Y * 1000), (int)(p.Z * 1000))); Cv2.GaussianBlur(mat, mat, Cv.Size(3, 3), -1); Mat gau = mat.Clone(); Cv2.GaussianBlur(gau, gau, Cv.Size(31, 31), -1); Cv2.Subtract(gau, mat, mat); Cv2.Threshold(mat, mat, BinarizeThreshold, 1, ThresholdType.Binary); Cv2.Add(sum, mat, sum); mc.MoveDistance(-0.003, VectorId.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")); string fileName = string.Format(@"c:\img\u.txt"); 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; File.AppendAllText(fileName, string.Format("{0:F} {1:F}\n", mx, my)); } } } sum *= 255; sum.ImWrite(String.Format(@"c:\img\{0}_{1}_{2}.bmp", System.DateTime.Now.ToString("yyyyMMdd_HHmmss_fff"), (int)(p.X * 1000), (int)(p.Y * 1000))); Vector2 encoderPoint = new Vector2(-1, -1); encoderPoint.X = mc.GetPoint().X; encoderPoint.Y = mc.GetPoint().Y;//おこられたのでしかたなくこうする 吉田20150427 Vector2 viewerPoint = new Vector2(-1, -1); if (TigerPatternMatch.PatternMatch(ref viewerPoint)) { encoderPoint = coordManager.TransToEmulsionCoord(viewerPoint); mc.MovePointXY(encoderPoint); mc.Join(); } }
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 GridMark getNearestGirdMark() { MotorControler mc = MotorControler.GetInstance(); return(coordManager.GetTheNearestGridMark(mc.GetPoint())); }
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 <Vector2> list_grid_pred = new List <Vector2>(); List <Vector2> list_grid_meas = new List <Vector2>(); List <Vector2> list_grid_part = new List <Vector2>(); List <Vector2> list_grid_meas2 = new List <Vector2>(); /* * list_grid_pred.Add(new Vector2( 0.0, 0.0)); * list_grid_pred.Add(new Vector2(30.0, 0.0)); * list_grid_pred.Add(new Vector2( 0.0, 30.0)); * list_grid_pred.Add(new Vector2(30.0, 30.0)); * * list_grid_pred.Add(new Vector2(60.0, 0.0)); * list_grid_pred.Add(new Vector2(0.0, 60.0)); * list_grid_pred.Add(new Vector2(60.0, 60.0)); * * * * * //list_grid_pred.Add(new Vector2(0.0, 10.0)); * //list_grid_pred.Add(new Vector2(10.0, 0.0)); * //list_grid_pred.Add(new Vector2(10.0, 10.0)); * * //list_grid_pred.Add(new Vector2(20.0, 0.0)); * //list_grid_pred.Add(new Vector2(0.0, 20.0)); * //list_grid_pred.Add(new Vector2(20.0, 20.0)); * * //list_grid_pred.Add(new Vector2(20.0, 10.0)); * //list_grid_pred.Add(new Vector2(30.0, 0.0)); * //list_grid_pred.Add(new Vector2(30.0, 10.0)); * //list_grid_pred.Add(new Vector2(10.0, 20.0)); * //list_grid_pred.Add(new Vector2(30.0, 20.0)); * //list_grid_pred.Add(new Vector2(10.0, 30.0)); * //list_grid_pred.Add(new Vector2(20.0, 30.0)); * * camera.Start(); * * Affine ap = new Affine(1.0, 0.0, 0.0, 1.0, 0.0, 0.0); * for (int i = 0; i < list_grid_pred.Count; i++) { * Vector2 predpoint = ap.Trance(list_grid_pred[i]); * //mc.MovePoint(predpoint.X, predpoint.Y, InitPoint.Z + 0.030); * mc.MoveTo(new Vector3(predpoint.X, predpoint.Y, InitPoint.Z + 0.030), new Vector3(0, 0, 0), new Vector3(0, 0, 0)); * mc.Join(); * * led.AdjustLight(parameterManager); * * * 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); * * if (brightness > 5000 ) flag = false; * } * * Thread.Sleep(100); * * mc.SetSpiralCenterPoint(); * flag = true; * int counter = 0; * while (flag) { * if(counter!=0){ * mc.MoveInSpiral(true); * mc.Join(); * } * counter++; * * Camera c = Camera.GetInstance(); * byte[] b = c.ArrayImage; * Mat mat = new Mat(440, 512, MatType.CV_8U, b); * Cv2.GaussianBlur(mat, mat, Cv.Size(7, 7), -1); * * Cv2.Threshold(mat, mat, 60, 255, ThresholdType.BinaryInv); * * Moments mom = new Moments(mat); * if (mom.M00 < 1000 * 255) continue; * * double cx = mom.M10 / mom.M00; * double cy = mom.M01 / mom.M00; * Mat innercir = Mat.Zeros(440, 512, MatType.CV_8UC1); * Cv2.Circle(innercir, new Point(cx, cy), 10, new Scalar(255, 255, 255), 3); * int innerpath = Cv2.CountNonZero(innercir); * Cv2.BitwiseAnd(innercir, mat, innercir); * int innersum = Cv2.CountNonZero(innercir); * * Mat outercir = Mat.Zeros(440, 512, MatType.CV_8UC1); * Cv2.Circle(outercir, new Point(cx, cy), 100, new Scalar(255, 255, 255), 3); * int outerpath = Cv2.CountNonZero(outercir); * Cv2.BitwiseAnd(outercir, mat, outercir); * int outersum = Cv2.CountNonZero(outercir); * * double innerratio = innersum * 1.0 / innerpath * 1.0; * double outerratio = outersum * 1.0 / outerpath * 1.0; * * if (innerratio < 0.8) continue; * if (outerratio > 0.2) continue; * * flag = false; * * * // * Vector2 grid_meas = cm.TransToEmulsionCoord((int)(cx), (int)(cy)); * * Vector3 to = new Vector3(grid_meas.X, grid_meas.Y, mc.GetPoint().Z); * mc.MoveTo(to, new Vector3(0, 0, 0), new Vector3(0, 0, 0)); * mc.Join(); * * b = c.ArrayImage; * mat = new Mat(440, 512, MatType.CV_8U, b); * Cv2.GaussianBlur(mat, mat, Cv.Size(7, 7), -1); * * Cv2.Threshold(mat, mat, 60, 255, ThresholdType.BinaryInv); * * mom = new Moments(mat); * if (mom.M00 < 1000 * 255) continue; * * cx = mom.M10 / mom.M00; * cy = mom.M01 / mom.M00; * innercir = Mat.Zeros(440, 512, MatType.CV_8UC1); * Cv2.Circle(innercir, new Point(cx, cy), 10, new Scalar(255, 255, 255), 3); * innerpath = Cv2.CountNonZero(innercir); * Cv2.BitwiseAnd(innercir, mat, innercir); * innersum = Cv2.CountNonZero(innercir); * * outercir = Mat.Zeros(440, 512, MatType.CV_8UC1); * Cv2.Circle(outercir, new Point(cx, cy), 100, new Scalar(255, 255, 255), 3); * outerpath = Cv2.CountNonZero(outercir); * Cv2.BitwiseAnd(outercir, mat, outercir); * outersum = Cv2.CountNonZero(outercir); * * innerratio = innersum * 1.0 / innerpath * 1.0; * outerratio = outersum * 1.0 / outerpath * 1.0; * * if (innerratio < 0.8) continue; * if (outerratio > 0.2) continue; * * grid_meas = cm.TransToEmulsionCoord((int)(cx), (int)(cy)); * System.Diagnostics.Debug.WriteLine(string.Format("gridmark {0} {1}", grid_meas.X, grid_meas.Y)); * * * list_grid_meas.Add(grid_meas); * list_grid_part.Add(list_grid_pred[i]); * if (i >= 3) { * ap = Affine.CreateAffineBy(list_grid_part, list_grid_meas); * } * } * * * }//for(int i = 0; i < list_grid_pred.Count; i++) */ Affine ap = new Affine(1.00055550, -0.00152264, 0.00174182, 1.00096792, 12.7498, -62.5798); string txtfileName = string.Format(@"c:\img\grid_g.txt"); StreamWriter twriter = File.CreateText(txtfileName); for (int x = 0; x < 6; x++) { for (int y = 0; y < 6; y++) { //if (x == 0 && y == 5) continue; //if (x == 1 && y == 5) continue; Vector2 predpoint = ap.Trance(new Vector2(x * 10, y * 10)); mc.MoveTo(new Vector3(predpoint.X, predpoint.Y, InitPoint.Z + 0.030)); mc.Join(); led.AdjustLight(parameterManager); 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); if (brightness > 5000) { flag = false; } } Thread.Sleep(100); byte[] b2 = camera.ArrayImage; Mat src2 = new Mat(440, 512, MatType.CV_8U, b2); Mat mat2 = src2.Clone(); string FileName = string.Format(@"C:\img\x{0}_y{1}.bmp", x, y); Cv2.ImWrite(FileName, mat2); Cv2.GaussianBlur(mat2, mat2, Cv.Size(7, 7), -1); Cv2.Threshold(mat2, mat2, 60, 255, ThresholdType.BinaryInv); Moments mom2 = new Moments(mat2); double cx2 = mom2.M10 / mom2.M00; double cy2 = mom2.M01 / mom2.M00; /* Mat innercir = Mat.Zeros(440, 512, MatType.CV_8UC1); * Cv2.Circle(innercir, new Point(cx2, cy2), 10, new Scalar(255, 255, 255), 3); * int innerpath = Cv2.CountNonZero(innercir); * Cv2.BitwiseAnd(innercir, mat2, innercir); * int innersum = Cv2.CountNonZero(innercir); * * Mat outercir = Mat.Zeros(440, 512, MatType.CV_8UC1); * Cv2.Circle(outercir, new Point(cx2, cy2), 100, new Scalar(255, 255, 255), 3); * int outerpath = Cv2.CountNonZero(outercir); * Cv2.BitwiseAnd(outercir, mat2, outercir); * int outersum = Cv2.CountNonZero(outercir); * * double innerratio = innersum * 1.0 / innerpath * 1.0; * double outerratio = outersum * 1.0 / outerpath * 1.0; */ Vector2 grid_meas2 = cm.TransToEmulsionCoord((int)(cx2), (int)(cy2)); FileName = string.Format(@"C:\img\after_x{0}_y{1}.bmp", x, y); Cv2.ImWrite(FileName, mat2); string stlog = ""; stlog += String.Format("{0} {1} {2:f4} {3:f4} {4:f4} {5:f4}\n", x * 10, y * 10, predpoint.X, predpoint.Y, grid_meas2.X, grid_meas2.Y); twriter.Write(stlog); } //for y } //for x twriter.Close(); }