public void DetectPupilBlob(Image <Gray, Byte> inputimg, int fullSizeImageW, int fullSizeImageH) { pupilBlobCenter = new AForge.Point(0, 0); //Blob_Aforge PupilBlob; //PupilBlob = new Blob_Aforge(inputimg.Bitmap, // (int)(METState.Current.MinPupilDiameter), // (int)(METState.Current.MaxPupilDiameter), // (int)(METState.Current.MinPupilDiameter), // (int)(METState.Current.MaxPupilDiameter), // 0.55, 2);//should be defined in each frame PupilBlob = new Blob_Aforge(inputimg.Bitmap, (int)(METState.Current.MinPupilScale * METState.Current.IrisDiameter), (int)(METState.Current.MaxPupilScale * METState.Current.IrisDiameter), (int)(METState.Current.MinPupilScale * METState.Current.IrisDiameter), (int)(METState.Current.MaxPupilScale * METState.Current.IrisDiameter), 0.55, 4);//???????????????????????????????????????????????? ,2) if (PupilBlob.blobs_Filtered.Count == 0)//Pupil not found { eyeData[0].pupilFound = false; } else// Pupil found { METState.Current.eye.PupilBlob.SelectedBlob = PupilBlob.blobs_Filtered[0]; #region distance from borders //if (PupilBlob.blobs_Filtered.Count == 1) //{ // double dist = Math.Sqrt((Math.Pow(PupilBlob.blobs_Filtered[0].CenterOfGravity.X - METState.Current.EyeImageOrginal.Width / 2, 2)) + (Math.Pow(PupilBlob.blobs_Filtered[0].CenterOfGravity.Y - METState.Current.EyeImageOrginal.Height / 2, 2))); // if (dist > 0.850 * (METState.Current.EyeImageOrginal.Height / 2)) // { // // PupilBlob.blobs_Filtered.Clear(); // // PupilBlob.SelectedBlob = null; // _PupilBlobCenter.X = 0; // _PupilBlobCenter.Y = 0; // } //} #endregion filter by location #region distance from center if (PupilBlob.blobs_Filtered.Count > 1)//??????????????? mage nagofti faghat yedoone blob peida kon ... 0.55, 1); { double minDis = 10000; foreach (AForge.Imaging.Blob blob in PupilBlob.blobs_Filtered) { double dist = Math.Sqrt((Math.Pow(blob.CenterOfGravity.X - METState.Current.EyeImageOrginal.Width / 2, 2)) + (Math.Pow(blob.CenterOfGravity.Y - METState.Current.EyeImageOrginal.Height / 2, 2))); if (dist < minDis) { PupilBlob.SelectedBlob = blob; // border = PupilBlob.hulls[i]; minDis = dist; } } } #endregion distance from center //get SelectedBlob pupilBlobCenter.X = (float)METState.Current.eye.PupilBlob.SelectedBlob.CenterOfGravity.X; pupilBlobCenter.Y = (float)METState.Current.eye.PupilBlob.SelectedBlob.CenterOfGravity.Y; // pupildata[0].pupilFound = true;//.............2 bool blobOnROIBorder; AForge.Point[] border = PupilBlob.GetBlobBorder(PupilBlob.SelectedBlob, pupilROI, new Size(fullSizeImageW, fullSizeImageH), out blobOnROIBorder); // pupildata[0].pupilEllipse = EmgImgProcssing.EllipseLeastSquareFitting(border);//.................2 //...................................1 //............................remove 2 if (blobOnROIBorder)// { eyeData[0].pupilFound = false; } else { eyeData[0].pupilEllipse = EmgImgProcssing.EllipseLeastSquareFitting(border); eyeData[0].pupilFound = true; } } }
public void FindGlint(Image <Bgr, Byte> inputimg, int threshold) { glintPyrLevel = 0; Image <Gray, Byte> GrayImg = new Image <Gray, byte>(inputimg.Bitmap); //rough determination of ROI (size of ROI may exceeds the size of the image no problem for cvSetImageROI) int d = (int)(METState.Current.IrisDiameter); if (METState.Current.detectPupil & eyeData[1].pupilFound) { glintROI = new Rectangle( Math.Max(Convert.ToInt32(eyeData[1].pupilCenter.X) - d / 2, 0), Math.Max(Convert.ToInt32(eyeData[1].pupilCenter.Y) - d / 2, 0), Math.Min(d, GrayImg.Width), Math.Min(d, GrayImg.Height) ); } else { d = 1 * d;//a little bit larger glintROI = new Rectangle(inputimg.Width / 2 - d / 2, inputimg.Height / 2 - d / 2, d, d); } //Modify ROI, it's needed when ROI is not inside the image area CvInvoke.cvSetImageROI(GrayImg, glintROI); //ROI will be used later for drawing glintROI = new Rectangle(glintROI.X, glintROI.Y, GrayImg.Width, GrayImg.Height); for (int i = 0; i < glintPyrLevel; i++) { GrayImg = GrayImg.PyrDown(); } if (METState.Current.GAdaptive) { GrayImg = EmgImgProcssing.Filter_GlintAdaptiveThreshold(GrayImg, 255, false).Erode(1).Dilate(1); } else { GrayImg = EmgImgProcssing.Filter_Threshold(GrayImg, threshold, false); //.Erode(2).Dilate(2);//; } // METState.Current.EyeImageTest = GrayImg; // GrayImg = GrayImg.Erode(1).Dilate(1); // GrayImg = GrayImg.Dilate(1).Erode(1); AForge.Point tempGlintCenter = new AForge.Point(0, 0); METState.Current.ProcessTimeEyeBranch.Timer("GlintBlob", "Start"); glintBlob = new Blob_Aforge(GrayImg.Bitmap, 5, 30, 5, 30, 0.4, 10); METState.Current.ProcessTimeEyeBranch.Timer("GlintBlob", "Stop"); glintPyrLevel++; #region filter blob if (glintBlob.blobs_Filtered.Count > 0) { glintBlob.SelectedBlob = glintBlob.blobs_Filtered[0]; tempGlintCenter = CorrectGlintPoint(glintBlob.SelectedBlob.CenterOfGravity); if (glintBlob.blobs_Filtered.Count >= 2) { List <Blob> temp = glintBlob.blobs_Filtered; List <double> dists = new List <double>(); foreach (AForge.Imaging.Blob blob in temp) { tempGlintCenter = CorrectGlintPoint(blob.CenterOfGravity); double dist = Math.Sqrt((Math.Pow(tempGlintCenter.X - eyeData[1].pupilCenter.X, 2)) + (Math.Pow(tempGlintCenter.Y - eyeData[1].pupilCenter.Y, 2))); dists.Add(dist); } int i = dists.IndexOf(dists.Min()); ////----------------------------------------------------This filter takes the one which is closer to the pupil glintBlob.SelectedBlob = temp[i]; tempGlintCenter = CorrectGlintPoint(glintBlob.SelectedBlob.CenterOfGravity); ////----------------------------------------------------This filter takes the average of the two closest glint to the pupil //dists.RemoveAt(i); //temp.RemoveAt(i); //i = dists.IndexOf(dists.Min());//this will select the second closest glint //AForge.Point tempGlintCenter2 = CorrectGlintPoint(temp[i].CenterOfGravity); //tempGlintCenter = new AForge.Point((tempGlintCenter.X + tempGlintCenter2.X) / 2, (tempGlintCenter.Y + tempGlintCenter2.Y) / 2); } #endregion filter blob } eyeData[0].glintCenter = tempGlintCenter;//(0,0) if there is no glint //CvInvoke.cvResetImageROI(GrayImg); }