private void MS_File_Open_Click(object sender, System.EventArgs e) { OFD.FileName = ""; SS_LBStatus.Text = "圖片開啟中"; if (OFD.ShowDialog() == DialogResult.OK) { StopProcess(); this.Cursor = Cursors.WaitCursor; string filepath = OFD.FileName; OImg = new Bitmap(filepath); RImg = new Bitmap(filepath); OrQImg = GrayImg.From(filepath); OrSImg = BGRImg.From(filepath); PB_Result.Image = OrQImg.ToImage(); PB_Result.Height = FLP_Result.Height; PB_Result.Width = OrQImg.Width * FLP_Result.Height / OrQImg.Height; QImg = OrQImg.Extend(); SImg = OrSImg.Extend(); K = new SimpleMotionKernel(QImg.Width, QImg.Height, 53); QImg.FFT2(); SImg.FFT2(); QThread = new Thread(QProcess); QThread.Start(); this.Cursor = Cursors.Default; } }
private void QProcess() { SS_LBStatus.Text = "產生預覽圖片中"; XProcessing = true; SS_PB.Value = 0; SS_PB.Visible = true; int length = TrB_Length.Value; int lambda = TrB_Lambda.Value; SS_PB.Value = 10; K.Reset(length); K.FFT2(); PB_Kernel.Image = K.ToPreviewImage(); SS_PB.Value = 20; SS_PB.Value = 30; GrayImg ReQImg = QImg.InsideSimpleMotionDeblur(K, lambda); SS_PB.Value = 40; ReQImg.BFFTShift(); SS_PB.Value = 50; ReQImg.IFFT2(); SS_PB.Value = 60; ReQImg = ReQImg.UnExtend(OrQImg.Width, OrQImg.Height); SS_PB.Value = 70; if (PB_Result.Image != null) { PB_Result.Image.Dispose(); PB_Result.Image = null; } PB_Result.Image = ReQImg.ToImage(); SS_PB.Value = 100; SS_LBStatus.Text = "產生完整圖片中"; if (SThread != null) { SThread.Abort(); SThread = null; } SThread = new Thread(SProcess); SThread.Start(); GC.Collect(); }
public void FindPupil(Image <Bgr, Byte> inputimg, int threshold) { try { #region Find Pupil Image <Bgr, Byte> CropedInputimg = new Image <Bgr, byte>(inputimg.Bitmap); Image <Gray, Byte> temp = new Image <Gray, byte>(inputimg.Width, inputimg.Height); // CropedInputimg = CropedInputimg.Erode(1).Dilate(1); SetPupilROI(CropedInputimg, true); SetPupilROI(METState.Current.EyeImageForShow, true); //if you want to reduce the size of the image e.g. PyrDown you should consider it for translation of the coordinates after blobDetection. //Now I have assumed that translation is only ROI.corner and there is no *Factor #region Remove Glint if (!LargScan & METState.Current.RemoveGlint == true) { METState.Current.ProcessTimeEyeBranch.Timer("GlintRemove", "Start"); Image <Gray, Byte> GlintMask = new Image <Gray, byte>(CropedInputimg.Width, CropedInputimg.Height); CropedInputimg = CropedInputimg.PyrDown(); GlintMask = CropedInputimg.Convert <Gray, byte>(); GlintMask = EmgImgProcssing.Filter_Threshold(GlintMask, METState.Current.glintThreshold, false).Dilate(1); if (METState.Current.RemoveGlint == true) { CvInvoke.cvInpaint(CropedInputimg.Ptr, GlintMask.Ptr, CropedInputimg.Ptr, 3, Emgu.CV.CvEnum.INPAINT_TYPE.CV_INPAINT_NS); } CropedInputimg = CropedInputimg.PyrUp(); METState.Current.ProcessTimeEyeBranch.Timer("GlintRemove", "Stop"); } #endregion Remove Glint #region Gray & ErodeDilate Image <Gray, Byte> GrayImg; if (METState.Current.DilateErode == true) { // only when largescan=false METState.Current.ProcessTimeEyeBranch.Timer("Fill Gaps", "Start"); // GrayImg = (LargScan == true) ? CropedInputimg.Convert<Gray, byte>() : CropedInputimg.Convert<Gray, byte>().Erode(4).Dilate(4); GrayImg = CropedInputimg.Convert <Gray, byte>().Erode(4).Dilate(3); METState.Current.ProcessTimeEyeBranch.Timer("Fill Gaps", "Stop"); } else { GrayImg = CropedInputimg.Convert <Gray, byte>(); } #endregion Gray & ErodeDilate // METState.Current.ProcessTimeEyeBranch.Timer("Pupil Detection", "Start"); #region Threshold int constant = -1; // if (!LargScan & METState.Current.PAdaptive_new) constant = PAConstantSet(GrayImg); //Debug.WriteLine(constant + "," + METState.Current.PAdaptive_Constant); if (METState.Current.PAdaptive) { GrayImg = EmgImgProcssing.Filter_PupilAdaptiveThreshold(GrayImg, 255, true, constant); } else { GrayImg = EmgImgProcssing.Filter_Threshold(GrayImg, threshold, true); } #endregion Threshold // GrayImg = GrayImg.Erode(3).Dilate(2); GrayImg = GrayImg.Dilate(2).Erode(2); DetectPupilBlob(GrayImg, inputimg.Width, inputimg.Height); // METState.Current.ProcessTimeEyeBranch.Timer("Pupil Detection", "Stop"); #region Show threshold mask if (METState.Current.showPupil) { if (LargScan) { CropedInputimg = EmgImgProcssing.ColoredMask((Bitmap)PupilBlob.image, CropedInputimg, new Bgr(System.Drawing.Color.Green), true); } // else CropedInputimg = EmgImgProcssing.ColoredMask((Bitmap)PupilBlob.image, CropedInputimg, new Bgr(System.Drawing.Color.Chartreuse), true); } #endregion Show threshold mask CvInvoke.cvCopy(CropedInputimg.Ptr, METState.Current.EyeImageForShow.Ptr, new IntPtr()); CvInvoke.cvResetImageROI(METState.Current.EyeImageForShow); CvInvoke.cvResetImageROI(CropedInputimg);//?? #endregion Find Pupil #region Ellipse Drawing and modifing if (eyeData[0].pupilFound) { if (METState.Current.showPupil) { EmgImgProcssing.DrawEllipse(METState.Current.EyeImageForShow, eyeData[0].pupilEllipse, new Bgr(255, 255, 255)); } //emgu ellipse bug: I need to modify it again after drawing. now ellipse box, verteces and angle is fine eyeData[0].pupilEllipse = new Emgu.CV.Structure.Ellipse(eyeData[0].pupilEllipse.MCvBox2D.center, new SizeF(eyeData[0].pupilEllipse.MCvBox2D.size.Width, eyeData[0].pupilEllipse.MCvBox2D.size.Height), eyeData[0].pupilEllipse.MCvBox2D.angle - 90); // EmgImgProcssing.DrawRectangle(METState.Current.EyeImageForShow, pupildata[0].pupilEllipse.MCvBox2D.GetVertices(), 0, false, ""); // EmgImgProcssing.DrawRectangle(METState.Current.EyeImageForShow, pupildata[0].pupilEllipse.MCvBox2D.center, pupildata[0].pupilEllipse.MCvBox2D.size); } #endregion Ellipse Drawing and modifing pupildata_Update(MeasureCenter(), MeasureDiameter());//MeasureDiameter should be after modification of ellipse blink(ref Blink, ref DoubleBlink); } catch (Exception error) { System.Windows.Forms.MessageBox.Show(error.ToString()); } }