private void Get_Center_Click(object sender, EventArgs e) { using (Mat img1 = CvInvoke.Imread(LinkImg1, Emgu.CV.CvEnum.ImreadModes.Grayscale)) { using (Mat img2 = CvInvoke.Imread(LinkImg2, Emgu.CV.CvEnum.ImreadModes.Grayscale)) { using (Mat img3 = CvInvoke.Imread(LinkImg3, Emgu.CV.CvEnum.ImreadModes.Grayscale)) { Image <Gray, byte> iGray1 = img1.ToImage <Gray, byte>(); Image <Gray, byte> iGray2 = img2.ToImage <Gray, byte>(); Image <Gray, byte> iGray3 = img3.ToImage <Gray, byte>(); Point cRec1, cRec2, cRec3; iGray1 = ComputerVison.RoiImage(iGray1, roi); iGray2 = ComputerVison.RoiImage(iGray2, roi); iGray3 = ComputerVison.RoiImage(iGray3, roi); cRec1 = FindcRect(iGray1, 150); cRec2 = FindcRect(iGray2, 150); cRec3 = FindcRect(iGray3, 150); Point midPoint1 = new Point((int)(cRec1.X + cRec2.X) / 2, (int)(cRec1.Y + cRec2.Y) / 2); Point midPoint2 = new Point((int)(cRec3.X + cRec2.X) / 2, (int)(cRec3.Y + cRec2.Y) / 2); result = CalculatorCenter(cRec1, cRec2, cRec3); label2.Text = "Result:\n X: " + (result.X + roi.X).ToString() + "\n Y: " + (result.Y + roi.Y).ToString(); CvInvoke.Circle(iGray3, result, 10, new MCvScalar(255, 0, 255), 5); pictureBox4.Image = iGray3.ToBitmap(); iGray1.Dispose(); iGray2.Dispose(); iGray3.Dispose(); } } } //this.Close(); }
private void GetOriginImage() { try { using (Mat img = CvInvoke.Imread(@"img\origin.bmp", Emgu.CV.CvEnum.ImreadModes.Grayscale)) { Image <Gray, byte> _img = img.ToImage <Gray, byte>(); _img = ComputerVison.RoiImage(_img, Config.Parameter.ROI); VectorOfPoint cnt = new VectorOfPoint(); cnt = ComputerVison.FindContours(_img, Config.Parameter.THRESHOLD_VALUE); RotatedRect a = CvInvoke.MinAreaRect(cnt); ORGRec = a.MinAreaRect(); ORGRec.X += Config.Parameter.ROI.X - 10; ORGRec.Y += Config.Parameter.ROI.Y - 10; ORGRec.Height += 20; ORGRec.Width += 20; using (Image <Bgr, byte> iBgr2 = _img.Convert <Bgr, byte>()) { CvInvoke.Rectangle(iBgr2, ORGRec, new MCvScalar(0, 255, 0), 3); CvInvoke.Imwrite("img\\originRoi.bmp", iBgr2); } Point[] p = ComputerVison.Search2Tip(cnt); ORG.SetPointA(p[0]); ORG.SetPointB(p[1]); cnt.Dispose(); _img.Dispose(); } } catch (Exception er) { MessageBox.Show(er.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private void getOriginImageToolStripMenuItem_Click(object sender, EventArgs e) { p_imShow.Image = null; UpdateParameter(); bool response = OpenCamera(); if (response == true) { using (Image <Bgr, byte> iBgr = GetImage()) { p_imShow.Image = iBgr.ToBitmap(); DialogResult kq = MessageBox.Show("You Sure create new origin image?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (kq == DialogResult.Yes) { CvInvoke.Imwrite(@"img\origin.bmp", iBgr); Image <Gray, byte> iGray = ComputerVison.RoiImage(iBgr.Convert <Gray, byte>(), Config.Parameter.ROI); using (VectorOfPoint cnt = ComputerVison.FindContours(iGray, Config.Parameter.THRESHOLD_VALUE)) { RotatedRect a = CvInvoke.MinAreaRect(cnt); ORGRec = a.MinAreaRect(); using (Image <Bgr, byte> iBgr2 = iGray.Convert <Bgr, byte>()) { CvInvoke.Rectangle(iBgr2, ORGRec, new MCvScalar(0, 255, 0), 3); CvInvoke.Imwrite("img\\originRoi.bmp", iBgr2); } } iGray.Dispose(); } } Camera.CloseDevice(); } }
private Point FindcRect(Image <Gray, byte> img, int thresh_value) { Point cRect; using (VectorOfPoint cnt = ComputerVison.FindContours(img, thresh_value)) { RotatedRect rec1 = CvInvoke.MinAreaRect(cnt); PointF cRec1F = rec1.Center; cRect = Point.Round(cRec1F); } return(cRect); }
private void Handling() { Response result = new Response(); string name = DateTime.Now.Year.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Hour.ToString() + "_" + DateTime.Now.Minute.ToString() + "_" + DateTime.Now.Second.ToString(); Light_Mode(modeCamera.Checked); Thread.Sleep(500); Image <Bgr, byte> iBgr = GetImage(); Light_Mode(false); Image <Gray, byte> iGray = iBgr.Convert <Gray, byte>(); iGray = ComputerVison.RoiImage(iGray, Config.Parameter.ROI); VectorOfPoint cnt = new VectorOfPoint(); Point[] p = new Point[2]; cnt = ComputerVison.FindContours(iGray, Config.Parameter.THRESHOLD_VALUE); if (cnt != null) { p = ComputerVison.Search2Tip(cnt); ComputerVison.Calculator(ref result, ORG.PointA, ORG.PointB, p[0], p[1], Config.Parameter.LabelSize.Width, false); ComputerVison.RouPoint(ORG.PointO, ref p[0], result.ANGLE); ComputerVison.RouPoint(ORG.PointO, ref p[1], result.ANGLE); ComputerVison.Calculator(ref result, ORG.PointA, ORG.PointB, p[0], p[1], Config.Parameter.LabelSize.Width, true); short y = (short)Math.Round(result.X * Config.Parameter.PULSE_Y + 150); short x = (short)Math.Round(result.Y * Config.Parameter.PULSE_X + 20); short z = (short)Math.Round(-result.ANGLE * Config.Parameter.PULSE_Z / 360); if (modeCamera.Checked) { PLCCommunicate(x, y, z); } //log.Invoke(new MethodInvoker(delegate () //{ // log.Text = "X:" + x.ToString("F3") + " Y:" + y.ToString("F3") + " Angle:" + z.ToString(); //})); iGray = iBgr.Convert <Gray, byte>(); ComputerVison.RotationImage(ref iGray, Config.Parameter.ROTATION_CENTER, (float)result.ANGLE); cnt = ComputerVison.FindContours(iGray, Config.Parameter.THRESHOLD_VALUE); using (Image <Bgr, byte> iBgr2 = iGray.Convert <Bgr, byte>()) { if (cnt != null) { RotatedRect r = CvInvoke.MinAreaRect(cnt); CvInvoke.Rectangle(iBgr2, r.MinAreaRect(), new MCvScalar(0, 255, 0), 3); } if (modeCamera.Checked) { CvInvoke.Imwrite(@"backup\" + name + ".bmp", iBgr); CvInvoke.Imwrite(@"backup_H\" + name + "trans.bmp", iBgr2); } p_imShow.Invoke(new MethodInvoker(delegate() { p_imShow.Image = iBgr2.ToBitmap(); })); } cnt.Dispose(); } else { using (Image <Bgr, byte> iBgr2 = iGray.Convert <Bgr, byte>()) { CvInvoke.Rectangle(iBgr2, ORGRec, new MCvScalar(0, 255, 0), 3); p_imShow.Invoke(new MethodInvoker(delegate() { p_imShow.Image = iBgr2.ToBitmap(); })); } DialogResult kq = MessageBox.Show("Not found label from images! You want to try again!", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (kq == DialogResult.Yes) { iGray.Dispose(); iBgr.Dispose(); Handling(); } else { result.ANGLE = 0; result.X = 0; result.Y = 0; } } iGray.Dispose(); iBgr.Dispose(); }