Beispiel #1
0
        public void bilinearfillHoles(Form1 form1)
        {
            Emgu.CV.Image <Gray, ushort> filledsurface = sc.image;

            for (int i = 1; i < sc.image.Height - 1; i++)
            {
                form1.progressBar1.Value = i / filledsurface.Height;
                for (int j = 1; j < sc.image.Width - 1; j++)
                {
                    if (sc.image.Data[i, j, 0] == 0)
                    {
                        if (sc.image.Data[i + 1, j + 1, 0] != 0 && sc.image.Data[i - 1, j - 1, 0] != 0 && sc.image.Data[i + 1, j - 1, 0] != 0 && sc.image.Data[i - 1, j + 1, 0] != 0)
                        {
                            ushort a11 = sc.image.Data[i - 1, j - 1, 0];
                            ushort a12 = sc.image.Data[i + 1, j - 1, 0];
                            ushort a21 = sc.image.Data[i - 1, j + 1, 0];
                            ushort a22 = sc.image.Data[i + 1, j + 1, 0];

                            float r1 = a11 / 2 + a12 / 2;
                            float r2 = a21 / 2 + a22 / 2;
                            float p  = r1 / 2 + r2 / 2;
                            filledsurface.Data[i, j, 0] = (ushort)p;
                        }
                    }
                }
                for (int j = 1; j < sc.image.Width - 1; j++)
                {
                    form1.progressBar1.Value = i / filledsurface.Height;
                    if (sc.image.Data[i, j, 0] == 0)
                    {
                        if (sc.image.Data[i + 1, j, 0] != 0 && sc.image.Data[i - 1, j, 0] != 0 && sc.image.Data[i, j - 1, 0] != 0 && sc.image.Data[i, j + 1, 0] != 0)
                        {
                            ushort a11 = sc.image.Data[i - 1, j, 0];
                            ushort a12 = sc.image.Data[i, j + 1, 0];
                            ushort a21 = sc.image.Data[i, j - 1, 0];
                            ushort a22 = sc.image.Data[i + 1, j, 0];

                            float r1 = a11 / 2 + a12 / 2;
                            float r2 = a21 / 2 + a22 / 2;
                            float p  = r1 / 2 + r2 / 2;
                            filledsurface.Data[i, j, 0] = (ushort)p;
                        }
                    }
                }
            }
            form1.progressBar1.Value = 100;
            filledsurface.Save(form1.SavePath + "\\" + "surface_bilin_filled.png");
            filledsurface.Save(form1.SavePath + "\\" + "surface.png");
            form1.pictureBox1.Image = filledsurface.ToBitmap();
            sc.image = filledsurface;
        }
Beispiel #2
0
        public static void CropAndNormalizeObjects()
        {
            List <PSM4TxSample> samples = LoadSamples(@"\users\jie\projects\Intel\data\PSM4-Tx\20160722\Original");

            for (int i = 0; i < samples.Count; i++)
            {
                PSM4TxSample sample = samples[i];

                Emgu.CV.Image <Gray, byte> image = new Emgu.CV.Image <Gray, byte>(sample.imageFile);

                //{   //Aperture
                //    //Extends in X direction to make width 240 pixels
                //    double diff_x = APERTURE_ORIGINAL_WIDTH - sample.apertureW;
                //    //Extends in Y direction to make 2000 pixels
                //    double diff_y = APERTURE_ORIGINAL_HEIGHT - sample.apertureH;

                //    Rectangle rect = new Rectangle((int)(sample.apertureX - diff_x / 2 + 0.5), (int)(sample.apertureY - diff_y / 2 + 0.5), APERTURE_ORIGINAL_WIDTH, APERTURE_ORIGINAL_HEIGHT);
                //    image.ROI = rect;
                //    Emgu.CV.Image<Gray, byte> normalized = image.Resize(APERTURE_HOG_WIDTH, APERTURE_HOG_HEIGHT, Inter.Linear);

                //    string cropped_file = sample.imageFile.Replace("original", "Aperture");
                //    normalized.Save(cropped_file);
                //}

                //{   //arrayblock
                //    //Extends in X direction to make width 1200 pixels
                //    double diff_x = ARRAYBLOCK_ORIGINAL_WIDTH - sample.arrayblockW;
                //    //Extends in Y direction to make 1600 pixels
                //    double diff_y = ARRAYBLOCK_ORIGINAL_HEIGHT - sample.arrayblockH;

                //    Rectangle rect = new Rectangle((int)(sample.arrayblockX - diff_x / 2 + 0.5), (int)(sample.arrayblockY - diff_y / 2 + 0.5), ARRAYBLOCK_ORIGINAL_WIDTH, ARRAYBLOCK_ORIGINAL_HEIGHT);
                //    image.ROI = rect;
                //    Emgu.CV.Image<Gray, byte> normalized = image.Resize(ARRAYBLOCK_HOG_WIDTH, ARRAYBLOCK_HOG_HEIGHT, Inter.Linear);

                //    string cropped_file = sample.imageFile.Replace("original", "Arrayblock");
                //    normalized.Save(cropped_file);
                //}

                {   //Isolator
                    //Extends in X direction to make width 1000 pixels
                    double diff_x = ISOLATOR_ORIGINAL_WIDTH - sample.isolatorW;
                    //Extends in Y direction to make 1600 pixels
                    double diff_y = ISOLATOR_ORIGINAL_HEIGHT - sample.isolatorH;

                    Rectangle rect = new Rectangle((int)(sample.isolatorX - diff_x / 2 + 0.5), (int)(sample.isolatorY - diff_y / 2 + 0.5), ISOLATOR_ORIGINAL_WIDTH, ISOLATOR_ORIGINAL_HEIGHT);
                    image.ROI = rect;
                    Emgu.CV.Image <Gray, byte> normalized = image.Resize(ISOLATOR_HOG_WIDTH, ISOLATOR_HOG_HEIGHT, Inter.Linear);

                    string cropped_file = sample.imageFile.Replace("original", "Isolator");
                    normalized.Save(cropped_file);
                }
            }
        }
Beispiel #3
0
 public static async Task UploadImage(Emgu.CV.Image <Bgr, Byte> img)
 {
     img.Save("face.jpg");
     try
     {
         var client   = new ImgurClient("daa2923d8975323", "d37d874c8356e6a43921688675c2343f91e2f868");
         var endpoint = new ImageEndpoint(client);
         Imgur.API.Models.IImage image;
         using (var fs = new FileStream(@"face.jpg", FileMode.Open))
         {
             image = await endpoint.UploadImageStreamAsync(fs);
         }
         Debug.Write("Image uploaded. Image Url: " + image.Link);
         sendTwilio(image.Link);
     }
     catch (ImgurException imgurEx)
     {
         Debug.Write("An error occurred uploading an image to Imgur.");
         Debug.Write(imgurEx.Message);
     }
 }
Beispiel #4
0
        public static void NegativeIsolatorPatches()
        {
            List <PSM4TxSample> samples = LoadSamples(@"\users\jie\projects\Intel\data\PSM4-Tx\20160722\Original");

            for (int i = 0; i < samples.Count; i++)
            {
                PSM4TxSample sample = samples[i];
                Emgu.CV.Image <Gray, byte> image = new Emgu.CV.Image <Gray, byte>(sample.imageFile);
                int    image_width = image.Width, image_height = image.Height;
                Random rand = new Random();

                for (int k = 0; k < 2; k++)
                {
                    int x, y, w = ISOLATOR_ORIGINAL_WIDTH, h = ISOLATOR_ORIGINAL_HEIGHT;
                    x = rand.Next(0, image_width); y = rand.Next(0, image_height);

                    if (x + w >= image_width || y + h >= image_height)
                    {
                        k--; continue;
                    }

                    Rectangle rect = new Rectangle(x, y, w, h);
                    image.ROI = rect;
                    Emgu.CV.Image <Gray, byte> neg_image = image.Resize(ISOLATOR_HOG_WIDTH, ISOLATOR_HOG_HEIGHT, Inter.Linear);

                    string negative_image_file = sample.imageFile.Replace("original", @"IsolatorNegative");
                    negative_image_file = negative_image_file.Insert(negative_image_file.LastIndexOf('.'), "." + k.ToString());
                    string folder = Path.GetDirectoryName(negative_image_file);
                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }
                    neg_image.Save(negative_image_file);
                }
            }
        }
Beispiel #5
0
        private bool elaboraFoto(byte[] img)
        {
            try
            {
                Emgu.CV.Image <Bgr, byte> image;
                using (var byteStream = new MemoryStream(img))
                {
                    image = new Emgu.CV.Image <Bgr, byte>(new Bitmap(byteStream));
                }

                long detectionTime;
                List <System.Drawing.Rectangle> faces = new List <System.Drawing.Rectangle>();
                List <System.Drawing.Rectangle> eyes  = new List <System.Drawing.Rectangle>();

                //The cuda cascade classifier doesn't seem to be able to load "haarcascade_frontalface_default.xml" file in this release
                //disabling CUDA module for now
                bool tryUseCuda = false;

                /*Per vedere se sono state rilevate facce o occhi eventualmente possiamo mettere
                 * un contatore che ci dice quante facce o occhi ci sono
                 */
                bool face_detect = false;
                bool eye_detect  = false;


                DetectFace.Detect(
                    image.Mat, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml",
                    faces, eyes,
                    tryUseCuda,
                    out detectionTime);

                foreach (System.Drawing.Rectangle face in faces)
                {
                    CvInvoke.Rectangle(image, face, new Bgr(System.Drawing.Color.Red).MCvScalar, 2);
                    face_detect = true;
                }
                foreach (System.Drawing.Rectangle eye in eyes)
                {
                    CvInvoke.Rectangle(image, eye, new Bgr(System.Drawing.Color.Blue).MCvScalar, 2);
                    eye_detect = true;
                }

                image.Save("Result.bmp");

                Bitmap result = image.ToBitmap();
                byte[] xByte  = ToByteArray(result, ImageFormat.Bmp);



                int id;
                //qui salva la foto solo se è stata rilevata una faccia o degli occhi
                if (face_detect || eye_detect)
                {
                    inserisciFoto(xByte, true);
                    id = recuperaUltimoId();
                    recuperaFoto(id);
                    return(true);
                }
                else
                {
                    inserisciFoto(xByte, false);
                    id = recuperaUltimoId();
                    recuperaFoto(id);
                    return(false);
                }
            }

            catch (Exception e)
            {
                Debug.Print("Exception: " + e);
                return(false);
            }
        }
Beispiel #6
0
        public void fillHoles(Form1 form1)
        {
            Emgu.CV.Image <Gray, ushort> filledsurface = sc.image;

            for (int i = 3; i < filledsurface.Height - 4; i++)
            {
                form1.progressBar1.Value = i / filledsurface.Height;
                for (int j = 3; j < filledsurface.Width - 4; j++)
                {
                    bool frame    = true;
                    uint sumframe = 0;
                    uint suminner = 0;
                    int  count    = 0;
                    #region frame check
                    for (int k = -3; k <= 3; k++)
                    {
                        if (filledsurface.Data[i + k, j - 3, 0] != 0)
                        {
                            sumframe += filledsurface.Data[i + k, j - 3, 0];
                        }
                        else
                        {
                            frame = false;
                        }
                        if (filledsurface.Data[i + k, j + 3, 0] != 0 && frame == true)
                        {
                            sumframe += filledsurface.Data[i + k, j + 3, 0];
                        }
                        else
                        {
                            frame = false;
                        }
                    }
                    for (int k = -2; k <= 2; k++)
                    {
                        if (filledsurface.Data[i - 3, j + k, 0] != 0 && frame == true)
                        {
                            sumframe += filledsurface.Data[i - 3, j + k, 0];
                        }
                        else
                        {
                            frame = false;
                        }
                        if (filledsurface.Data[i + 3, j + k, 0] != 0 && frame == true)
                        {
                            sumframe += filledsurface.Data[i + 3, j + k, 0];
                        }
                        else
                        {
                            frame = false;
                        }
                    }
                    #endregion

                    if (frame == false)
                    {
                        continue;
                    }
                    for (int k = -2; k <= 2; k++)
                    {
                        for (int l = -2; l <= 2; l++)
                        {
                            if (filledsurface.Data[i + k, j + l, 0] != 0)
                            {
                                suminner += filledsurface.Data[i + k, j + l, 0];
                                count++;
                            }
                        }
                    }
                    if (count == 0 || count == 25)
                    {
                        continue;
                    }
                    if (Math.Abs(suminner / count - sumframe / 24) < 100)
                    {
                        for (int k = -2; k <= 2; k++)
                        {
                            for (int l = -2; l <= 2; l++)
                            {
                                if (filledsurface.Data[i + k, j + l, 0] == 0)
                                {
                                    filledsurface.Data[i + k, j + l, 0] = (ushort)(suminner / count);
                                }
                            }
                        }
                    }
                }
            }

            form1.progressBar1.Value = 100;
            filledsurface.Save(form1.SavePath + "\\" + "surface_filled.png");
            filledsurface.Save(form1.SavePath + "\\" + "surface.png");
            form1.pictureBox1.Image = filledsurface.ToBitmap();
            sc.image = filledsurface;
        }
        public int apply(string fileName, string output)
        {
            int counter = 0;

            Emgu.CV.Image<Bgr, Byte> imgS = new Emgu.CV.Image<Bgr, Byte>(fileName);

            Emgu.CV.Image<Gray, Byte> img = new Emgu.CV.Image<Gray, Byte>(fileName);

            //Emgu.CV.Image<Gray, Byte> imgGray = new Image<Gray, byte>(img.Width, img.Height);
            //CvInvoke.cvCvtColor(img, imgGray, COLOR_CONVERSION.BGR2GRAY);

            int thresh = 1;
            int max_thresh = 255;
            img = img.ThresholdBinary(new Gray(thresh), new Gray(max_thresh));

            img.Save(output.Replace(".", "_binary."));

            Contour<Point> contur = img.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_CCOMP);
            Emgu.CV.CvInvoke.cvDrawContours(imgS, contur, new MCvScalar(0, 0, 255), new MCvScalar(0, 0, 255), 1, 1, LINE_TYPE.EIGHT_CONNECTED, new Point(0, 0));

            contur = img.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_CCOMP);

            while (contur != null && contur.HNext != null)
            {
                if (counter == 0) { counter++; }

                contur = contur.HNext;
                counter++;
            }

            MCvFont font = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_SIMPLEX, 0.8f, 0.8f);
            MCvScalar color = new MCvScalar(255, 255, 255);

            CvInvoke.cvPutText(imgS, "counter:" + counter, new Point(10, 20), ref font, color);

            imgS.Save(output);

            return counter;
        }
Beispiel #8
-1
 private void temporizador_Tick(object sender, EventArgs e)
 {
     string sufijo = "";
     string cadAux = "";
     sufijo = id.ToString();
     cadAux=sufijo.PadLeft(10, '0');
     imagenCapturada = capturador.QueryFrame();
     imagenCapturada.Save(rutaAlmacenamiento+"\\"+ nombre+cadAux + ".jpg");
     id++;
     controlImagen.Image = imagenCapturada;
 }
        public void generateOutput(List<KeyValuePair<int, int>> pairList, int bestK, string dir, string outPutDir,string fileFormat)
        {
            //Find similar data
            List<KeyValuePair<int, int>> blendList = new List<KeyValuePair<int, int>>(bestK);
            List<int> lonelyList = new List<int>(bestK);

            for (int i = 0; i < pairList.Count; i++)
            {
                for (int j = 0; j < pairList.Count; j++)
                {
                    if (pairList[i].Key == pairList[j].Value && pairList[j].Key == pairList[i].Value)
                    {
                        if (!(blendList.Contains(pairList[i]) || blendList.Contains(pairList[j])))
                        {
                            blendList.Add(pairList[i]);
                        }
                    }
                }
            }

            for (int i = 0; i < pairList.Count; i++)
            {
                bool key = true;
                for (int j = 0; j < blendList.Count; j++)
                {
                    if (pairList[i].Key == blendList[j].Key || pairList[i].Key == blendList[j].Value)
                    {
                        key = false;
                    }
                }

                if (key)
                {
                    if (!lonelyList.Contains(pairList[i].Key))
                    {
                        lonelyList.Add(pairList[i].Key);
                    }
                }
            }

            //move not changed layers
            for (int i = 0; i < lonelyList.Count; i++)
            {
                string fileName = String.Format(fileFormat, lonelyList[i]);
                System.IO.File.Copy(dir + fileName, outPutDir + fileName, true);
            }

            //blend and move
            for (int j = 0; j < blendList.Count; j++)
            {
                string fileName1 = String.Format(fileFormat, blendList[j].Key);
                Emgu.CV.Image<Bgr, int> img1 = new Emgu.CV.Image<Bgr, int>(dir + fileName1);

                string fileName2 = String.Format(fileFormat, blendList[j].Value);
                Emgu.CV.Image<Bgr, int> img2 = new Emgu.CV.Image<Bgr, int>(dir + fileName2);

                string fileName3 = String.Format(fileFormat, blendList[j].Key + "_" + blendList[j].Value);

                Emgu.CV.Image<Bgr, int> img3 = new Emgu.CV.Image<Bgr, int>(img1.Width, img1.Height);
                Emgu.CV.CvInvoke.cvAdd(img1, img2, img3, IntPtr.Zero);
                img3.Save(outPutDir + fileName3);

            }
        }