//Esta função detecta a linhas a preto //guarda o centroide da linha com área maior //desenha o centroide e o contorno da linha seleccionada //img - imagem a cores BGR public Image <Gray, Byte> Deteccao_Linha(Image <Bgr, Byte> img) { double areaC = 0; double area_max = 0; IntPtr seq_max = CvInvoke.cvCreateMemStorage(0); IntPtr storage = CvInvoke.cvCreateMemStorage(0); IntPtr contornos = new IntPtr(); MCvMoments moments = new MCvMoments(); Image <Gray, Byte> img_Contornos = new Image <Gray, Byte>(img.Width, img.Height); Emgu.CV.Image <Gray, Byte> img_Gray = img.Convert <Gray, Byte>(); //Pré-processamento img_Gray.SmoothMedian(5); img_Gray._EqualizeHist(); //Detecção da linha Emgu.CV.Image <Gray, Byte> img_aux = img_Gray.Canny(1, 1, 5); img_Gray = img_aux.Not(); img_aux = img_Gray.Erode(10); img_Gray = img_aux.Dilate(10); //detecta contornos CvInvoke.cvCopy(img_Gray, img_Contornos, System.IntPtr.Zero); CvInvoke.cvFindContours(img_Contornos, storage, ref contornos, System.Runtime.InteropServices.Marshal.SizeOf(typeof(MCvContour)), Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL, Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, new Point(0, 0)); //percorre os contornos e detecta o que tem a maior área Seq <Point> seq = new Seq <Point>(contornos, null); for (; seq != null && seq.Ptr.ToInt64() != 0; seq = seq.HNext) { areaC = CvInvoke.cvContourArea(seq, MCvSlice.WholeSeq, 1) * -1; if (areaC > area_max) { area_max = areaC; seq_max = seq.Ptr; } } //calcula o centroide do contorno com maior área if (areaC > 0) { CvInvoke.cvMoments(seq_max, ref moments, 0); Obj_centroid.X = (int)(moments.m10 / moments.m00); Obj_centroid.Y = (int)(moments.m01 / moments.m00); //desenha contorno com maior área CvInvoke.cvDrawContours(img, seq_max, new MCvScalar(0, 0, 255), new MCvScalar(255, 0, 0), 0, 2, LINE_TYPE.CV_AA, new Point(0, 0)); CvInvoke.cvCircle(img, new Point(Obj_centroid.X, Obj_centroid.Y), 10, new MCvScalar(0, 255, 0), 3, LINE_TYPE.CV_AA, 0); } else { Obj_centroid.X = -1; Obj_centroid.Y = -1; } return(img_Gray); }
private void button1_Click(object sender, EventArgs e) { Dictionary <string, Image <Bgr, byte> > bgrImages = new Dictionary <string, Image <Bgr, byte> >(); //--Initialize openFiledialogue--- OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Images (*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG"; openFileDialog1.FilterIndex = 1; openFileDialog1.RestoreDirectory = true; openFileDialog1.Title = "Image Selection"; openFileDialog1.Multiselect = true; //-------------------------------- DialogResult dialogresult = openFileDialog1.ShowDialog(); //---Save Images in Dictionary and display in ListBox---- if (dialogresult == System.Windows.Forms.DialogResult.OK) { if (imagesloaded) { //reset and clear everything if images were loaded before reset(); cleanLoadedImages.Clear(); LoadedImages.Clear(); workImages.Clear(); listBox1.Items.Clear(); //listBox1.Update(); } int currentImage = 0; // Read the files listBox1.BeginUpdate(); foreach (String file in openFileDialog1.FileNames) { // Create a PictureBox. try { //Convert Images to Emgu Format Image tempImage = Image.FromFile(file); Bitmap tempBitmap = new Bitmap(tempImage); Emgu.CV.Image <Bgr, byte> cleanImage = new Emgu.CV.Image <Bgr, byte>(tempBitmap); Emgu.CV.Image <Hsv, Byte> loadedImage = new Emgu.CV.Image <Hsv, byte>(tempBitmap); string loadedImageName = "Image:" + currentImage; //Save Images listBox1.Items.Add(loadedImageName); cleanLoadedImages.Add(loadedImageName, cleanImage); LoadedImages.Add(loadedImageName, loadedImage.SmoothMedian(mediansize)); if (checkBox1.Checked) { bgrImages.Add(loadedImageName, cleanImage.SmoothGaussian(gaussiansize)); } } catch (Exception ex) { // Could not load the image - probably related to Windows file system permissions. MessageBox.Show("Cannot load the image: " + file.Substring(file.LastIndexOf('\\')) + ".\n\n" + ex.Message); } currentImage++; } listBox1.EndUpdate(); imagesloaded = true; if (checkBox1.Checked) { LoadedImages = BackgroundSubtractor.getWithoutBackground(bgrImages); } listBox1.SelectedIndex = 0; } //---------------------------------------- }