public async Task downloadLatestPhoto(int frameId) { var paths = await GoProControl.GetMediaList(); string path = paths[paths.Count - 1]; string url = dcimUrl + path; String fileName = String.Format("{0}/{1:000}.png", snapshotsDir, frameId); Image image = await GoProControl.GetImage(url); var img = new Emgu.CV.Image <Emgu.CV.Structure.Bgr, byte>(new Bitmap(image)); double angle = checkBoxRotateCamera.Checked ? -90 : 0; var rimg = img.Rotate(angle, new Emgu.CV.Structure.Bgr(0, 0, 0), false); rimg.ROI = new Rectangle((rimg.Width - rimg.Height * targetWidth / targetHeight) / 2, 0, rimg.Height * targetWidth / targetHeight, rimg.Height); var cropped = rimg.Copy(); var resized = cropped.Resize(targetWidth, targetHeight, Emgu.CV.CvEnum.Inter.Linear); resized.Save(fileName); rimg.Dispose(); img.Dispose(); cropped.Dispose(); resized.Dispose(); }
private void Worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { Image image = (Image)e.Argument; var minSize = new Size(3840, 2160); Size newSize; if (image.Width < minSize.Width || image.Height < minSize.Height) { var ratio = Math.Max((double)minSize.Width / image.Width, (double)minSize.Height / image.Height); newSize = new Size((int)(ratio * image.Width), (int)(ratio * image.Height)); } else { newSize = image.Size; } var newRect = new Rectangle(Point.Empty, newSize); Emgu.CV.Image <Emgu.CV.Structure.Bgr, byte> cvImage; using (var bitmap = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb)) { using (var graphics = Graphics.FromImage(bitmap)) { graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; using (var wrapMode = new ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); graphics.DrawImage(image, newRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); } } Invoke(new Action(() => { if (screenshotViewer == null) { screenshotViewer = new ScreenshotViewer(this) { Top = Top, Left = Right } } ; screenshotViewer.SetImage(new Bitmap(bitmap)); screenshotViewer.Show(); })); var data = bitmap.LockBits(newRect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); var nBytes = data.Stride * data.Height; cvImage = new Emgu.CV.Image <Emgu.CV.Structure.Bgr, byte>(newSize); unsafe { Buffer.MemoryCopy(data.Scan0.ToPointer(), cvImage.Mat.DataPointer.ToPointer(), nBytes, nBytes); } bitmap.UnlockBits(data); } if (sift == null) { sift = new Emgu.CV.Features2D.SIFT(edgeThreshold: 25, sigma: 1.2); } if (matcher == null) { var use_bf = true; if (use_bf) { matcher = new Emgu.CV.Features2D.BFMatcher(Emgu.CV.Features2D.DistanceType.L2); } else { matcher = new Emgu.CV.Features2D.FlannBasedMatcher(new Emgu.CV.Flann.KdTreeIndexParams(5), new Emgu.CV.Flann.SearchParams()); } } if (heroDescriptors == null) { Invoke(new Action(() => { screenshotViewer.SetProgress(Stage.LoadingData); })); heroDescriptors = loadDescriptors("portraits.zip"); bgnameDescriptors = loadDescriptors("bgnames.zip"); } int nTotal = heroDescriptors.Count + bgnameDescriptors.Count; int nCurrent = 0; using (var kp = new Emgu.CV.Util.VectorOfKeyPoint()) using (var des = new Emgu.CV.Mat()) { Invoke(new Action(() => { screenshotViewer.SetProgress(Stage.ProcessingImage); })); sift.DetectAndCompute(cvImage, null, kp, des, false); cvImage.Dispose(); var searchResults = new List <SearchResult>(); Invoke(new Action(() => { screenshotViewer.SetProgress(0.0); })); foreach (var kvp in heroDescriptors) { using (var vMatches = new Emgu.CV.Util.VectorOfVectorOfDMatch()) { matcher.KnnMatch(kvp.Value, des, vMatches, 2); const float maxdist = 0.7f; var matches = vMatches.ToArrayOfArray().Where(m => m[0].Distance < maxdist * m[1].Distance).ToList(); if (matches.Any()) { searchResults.Add(new SearchResult(kvp.Key, matches, kp)); } } nCurrent++; Invoke(new Action(() => { screenshotViewer.SetProgress((double)nCurrent / nTotal); })); } searchResults.Sort((a, b) => - a.Distance.CompareTo(b.Distance)); searchResults.RemoveAll(t => searchResults.Take(searchResults.IndexOf(t)).Select(u => u.Name).Contains(t.Name)); var bans_picks = searchResults.Take(16).OrderBy(t => t.Location.Y).ToList(); var bans = bans_picks.Take(6).OrderBy(t => t.Location.X).ToList(); var picks = bans_picks.Skip(6).OrderBy(t => t.Location.X).ToList(); var t1picks = picks.Take(5).OrderBy(t => t.Location.Y).ToList(); var t2picks = picks.Skip(5).OrderBy(t => t.Location.Y).ToList(); var bgSearchResults = new List <SearchResult>(); foreach (var kvp in bgnameDescriptors) { using (var vMatches = new Emgu.CV.Util.VectorOfVectorOfDMatch()) { matcher.KnnMatch(kvp.Value, des, vMatches, 2); const float maxdist = 0.7f; var matches = vMatches.ToArrayOfArray().Where(m => m[0].Distance < maxdist * m[1].Distance).ToList(); if (matches.Any()) { bgSearchResults.Add(new SearchResult(kvp.Key, matches, kp)); } } nCurrent++; Invoke(new Action(() => { screenshotViewer.SetProgress((double)nCurrent / nTotal); })); } var bgSearchResult = bgSearchResults.OrderBy(t => - t.Distance).First(); Invoke(new Action(() => { screenshotViewer.SetProgress(Stage.Complete); screenshotViewer.SetSearchResults(bans_picks.ToArray(), bgSearchResult); c_bg.Text = bgSearchResult.Name; screenshotViewer.Show(); Focus(); })); } }
//główne operacje na obrazie public void ocr() { //otworzenie pliku FileStream srcstream = new FileStream(pic_file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //stworzenie bitmapy Bitmap source = new Bitmap(srcstream); //zmiana ustawień webform Panel1.Visible = false; Image1.Dispose(); Label2.Text = "Processing..."; Panel3.Visible = true; //Preperation code Bitmap ext = source; //AForge.Imaging.Filters. //Przekształcenie obrazu na skalę odcieni szarości - testować dla obrazów o różnej kolorystyce(opracować system wyznaczania parametrów filtru na podstawie RGB zdjęcia) AForge.Imaging.Filters.Grayscale grScl = new AForge.Imaging.Filters.Grayscale(0.2125, 0.0154, 0.0721 ); source = grScl.Apply(source); //Zwiększenie kontrastu AForge.Imaging.Filters.ContrastStretch conCor = new AForge.Imaging.Filters.ContrastStretch(); source = conCor.Apply(source); //Wyostrzenie AForge.Imaging.Filters.Sharpen shp = new AForge.Imaging.Filters.Sharpen(); source = shp.Apply(source); //Segmentation code bool procesed = false; // Image2.Width = 350; // Image2.Height = (int)((source.Height * 200) / source.Width); try { Emgu.CV.Image<Bgr, Byte> to_rec = new Emgu.CV.Image<Bgr, byte>(source); Do_ocr = new Tesseract("tessdata", "eng", Tesseract.OcrEngineMode.OEM_DEFAULT); try { Do_ocr.Recognize<Bgr>(to_rec); //recognizedText.Text = ocr.GetText(); PastOCRBox.Text = Do_ocr.GetText(); // StatusBox.Text = "Finished! Ready for next one..."; Do_ocr.Dispose(); to_rec.Dispose(); } catch (Exception exp) { Label2.Text = "Recognition error! " + exp.Message; Do_ocr.Dispose(); return; } } catch (Exception exp) { Label2.Text = "OCR engine failed! " + exp.Message; return; } //czyszczenie z plików tymczasowych // source.Save("D:\\test.bmp"); // ext.Save("D:\\testcor.bmp"); source.Dispose(); srcstream.Close(); srcstream.Dispose(); //System.IO.File.Delete(pic_file); System.IO.File.Delete(Server.MapPath("~/img/prev.bmp")); System.IO.File.Delete(Server.MapPath("~/img/tmp.bmp")); //przygotować wygląd strony po rozpoznawaniu Panel3.Visible = false; Label1.Visible = false; Panel0.Visible = false; Panel5.Visible = false; Panel4.Visible = true; }
private static void GrayValueTask() { while (true) { while (m_CheckList.Count > 0) { DataGridViewRow t_Checks = m_CheckList[0]; lock (m_Mutex) { string t_FolderPath = t_Checks.Cells["FolderPath"].Value.ToString(); string[] t_ImageFiles = System.IO.Directory.GetFiles(t_FolderPath); foreach (string t_ImageFile in t_ImageFiles) { string[] t_ImageFileSplit = System.IO.Path.GetFileNameWithoutExtension(t_ImageFile).Split('_'); string t_StationNumber = t_ImageFileSplit[2]; if (t_ImageFileSplit[2].CompareTo("15") == 0) { t_StationNumber = t_ImageFileSplit[2] + "_" + t_ImageFileSplit[3]; } string t_StationName = m_ImageNameMappings[t_StationNumber]; Point[] t_ROI = m_FixROILocations[t_StationName]; System.Drawing.Bitmap t_Bitmap = new Bitmap(t_ImageFile); Emgu.CV.Mat t_Mat = Emgu.CV.CvInvoke.Imread(t_ImageFile, Emgu.CV.CvEnum.ImreadModes.AnyColor); //Emgu.CV.CvInvoke.NamedWindow("A", Emgu.CV.CvEnum.NamedWindowType.FreeRatio); double t_Average = 0.0; if (t_Bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed) { Emgu.CV.Image <Emgu.CV.Structure.Gray, byte> t_Image = new Emgu.CV.Image <Emgu.CV.Structure.Gray, byte>(t_Mat.Bitmap); t_Image.ROI = new Rectangle(t_ROI[0].X, t_ROI[0].Y, t_ROI[1].X - t_ROI[0].X, t_ROI[1].Y - t_ROI[0].Y); Emgu.CV.Structure.Gray t_AverageBGR = t_Image.GetAverage(); t_Average = t_AverageBGR.MCvScalar.V0; t_Image.Dispose(); t_Image = null; } else { Emgu.CV.Image <Emgu.CV.Structure.Bgr, byte> t_Image = new Emgu.CV.Image <Emgu.CV.Structure.Bgr, byte>(t_Mat.Bitmap); t_Image.ROI = new Rectangle(t_ROI[0].X, t_ROI[0].Y, t_ROI[1].X - t_ROI[0].X, t_ROI[1].Y - t_ROI[0].Y); Emgu.CV.Structure.Bgr t_AverageBGR = t_Image.GetAverage(); t_Average = t_AverageBGR.MCvScalar.V2; t_Image.Dispose(); t_Image = null; } t_Checks.Cells[t_StationName].Value = t_Average; t_Mat.Dispose(); t_Mat = null; t_Bitmap.Dispose(); t_Bitmap = null; GC.Collect(); } } string t_ResultsString = string.Empty; foreach (DataGridViewCell t_Check in t_Checks.Cells) { t_ResultsString += t_Check.Value + ","; } t_ResultsString = t_ResultsString.Remove(t_ResultsString.Length - 1, 1); System.IO.File.AppendAllText("Results.csv", t_ResultsString); System.IO.File.AppendAllText("Results.csv", System.Environment.NewLine); m_CheckList.RemoveAt(0); } } }