private void ProcessImage(object sender, RoutedEventArgs e) { if (loadedImage == null) { MessageBox.Show("Image needs to be loaded first"); return; } processedImage = loadedImage.Clone(); Emgu.CV.Features2D.Feature2D detector = (Emgu.CV.Features2D.Feature2D)Parameter.ValueFor("Feature Detector", parametersInput.Parameters, parametersInput.Values); MKeyPoint[] kps = detector.Detect(loadedImage.Mat); // TODO: open cv probably has some function to draw features automatically foreach (var kp in kps) { DrawCricle(processedImage, new Bgr(Color.Wheat), new System.Drawing.Point((int)kp.Point.X, (int)kp.Point.Y), new System.Drawing.Size(10, 10)); } Emgu.CV.Util.VectorOfKeyPoint vectorOfKp = new Emgu.CV.Util.VectorOfKeyPoint(kps); var desc = new Emgu.CV.XFeatures2D.BriefDescriptorExtractor(32); Mat descriptors = new Mat(); desc.Compute(loadedImage, vectorOfKp, descriptors); imageViewer.Source = ImageLoader.ImageSourceForBitmap(processedImage.Bitmap); }
void Application_Idle(object sender, EventArgs e) { if (vc != null && !pause) { SIFT detector = new SIFT(); Emgu.CV.Util.VectorOfKeyPoint keypoints = new Emgu.CV.Util.VectorOfKeyPoint(); vc.Read(frame); System.Threading.Thread.Sleep((int)(1000.0 / rate - 5)); //imageBox1.Image = frame; frLbl.Text = rate.ToString(); cfLbl.Text = currentFrame.ToString(); fcLbl.Text = frameCount.ToString(); vc.Read(frame); imageBox1.Image = frame; //detector.Detect(frame); detector.DetectRaw(frame, keypoints); numOfKeyPoints = keypoints.Size; kpLbl.Text = numOfKeyPoints.ToString(); Features2DToolbox.DrawKeypoints(frame, keypoints, siftFrame, new Bgr(Color.Blue)); imageBox2.Image = siftFrame; GC.Collect(); currentFrame++; if (currentFrame >= frameCount) { pause = true; button4.Enabled = false; } } }
public static void DetectBlobEllipses(string myFile) { var grayImage = new Image <Gray, byte>(myFile); Mat imageCopy = Emgu.CV.CvInvoke.Imread(myFile, Emgu.CV.CvEnum.ImreadModes.Color); //grayImage._Not(); var grayImageCopy = grayImage.Clone(); var res = CvInvoke.Threshold(grayImage, grayImageCopy, 170, 255, Emgu.CV.CvEnum.ThresholdType.Binary); CvInvoke.Imwrite(Path.GetDirectoryName(myFile) + "\\" + Path.GetFileNameWithoutExtension(myFile) + "-threshold" + Path.GetExtension(myFile), grayImageCopy, new KeyValuePair <Emgu.CV.CvEnum.ImwriteFlags, int>(Emgu.CV.CvEnum.ImwriteFlags.JpegQuality, 95)); //grayImageCopy._Not(); // Set our filtering parameters // Initialize parameter settiing using cv2.SimpleBlobDetector var paramsx = new Emgu.CV.Features2D.SimpleBlobDetectorParams(); // Set Area filtering parameters paramsx.FilterByArea = true; paramsx.MinArea = 1000; // Set Circularity filtering parameters paramsx.FilterByCircularity = false; paramsx.MinCircularity = 0.9f; // Set Convexity filtering parameters paramsx.FilterByConvexity = false; paramsx.MinConvexity = 0.2f; // Set inertia filtering parameters paramsx.FilterByInertia = false; paramsx.MinInertiaRatio = 0.01f; // Create a detector with the parameters var detector = new Emgu.CV.Features2D.SimpleBlobDetector(paramsx); // Detect blobs var keypoints = detector.Detect(grayImageCopy); var keyPoints = new Emgu.CV.Util.VectorOfKeyPoint(keypoints); // Draw blobs on our image as red circles var blank = imageCopy.Clone(); Emgu.CV.Features2D.Features2DToolbox.DrawKeypoints(imageCopy, keyPoints, blank, new Bgr(System.Drawing.Color.Red), Emgu.CV.Features2D.Features2DToolbox.KeypointDrawType.DrawRichKeypoints); var number_of_blobs = keyPoints.Size; CvInvoke.Imwrite(Path.GetDirectoryName(myFile) + "\\" + Path.GetFileNameWithoutExtension(myFile) + "-copy" + Path.GetExtension(myFile), blank, new KeyValuePair <Emgu.CV.CvEnum.ImwriteFlags, int>(Emgu.CV.CvEnum.ImwriteFlags.JpegQuality, 95)); }
public SearchResult(string name, List <Emgu.CV.Structure.MDMatch[]> matches, Emgu.CV.Util.VectorOfKeyPoint kp) { Name = name.Split('_')[0]; Matches = matches.Where(m => m[0].Distance <= matches.Select(m0 => (double)m0[0].Distance).Median()).ToList(); var nmatches = matches.Count; var mp = matches.Select(m => kp[m[0].TrainIdx].Point).ToList(); MatchPointsRaw = mp; var averdist = new Dictionary <PointF, double>(); var median_point = new PointF((float)mp.Select(pt => (double)pt.X).Median(), (float)mp.Select(pt => (double)pt.Y).Median()); if (mp.Count > 1) { mp.ForEach(pt => averdist[pt] = Math.Sqrt(Math.Pow(pt.X - median_point.X, 2) + Math.Pow(pt.Y - median_point.Y, 2))); } else { averdist[mp.First()] = 0; } //mp.ForEach(pt => averdist[pt] = mp.Where(pt1 => pt1 != pt) //.Select(pt1 => Math.Sqrt(Math.Pow(pt1.X - pt.X, 2) + Math.Pow(pt1.Y - pt.Y, 2))).Average()); var deviation = averdist.Values.Median() * 2; //var deviation = averdist.Count > 1 ? averdist.Values.Average() + averdist.Values.StDev() * 3 : 0; mp = mp.Where(pt => averdist[pt] <= deviation).ToList(); var xs = mp.Select(pt => pt.X).OrderBy(n => n).ToList(); var ys = mp.Select(pt => pt.Y).OrderBy(n => n).ToList(); var(minx, maxx) = (xs.First(), xs.Last()); var(miny, maxy) = (ys.First(), ys.Last()); MatchPoints = mp; Rect = new RectangleF(minx, miny, maxx - minx, maxy - miny); Location = new PointF(Rect.X + Rect.Width / 2, Rect.Y + Rect.Height / 2); Distance = matches.Select(m => (double)1 / m[0].Distance).Sum(); /*if (HeroName == "chromie") * Distance *= 2; * else if (HeroName == "greymane") * Distance *= 3;*/ }
static void CreateDescriptions(string dirname, string message) { Console.Write("Creating descriptions for the {0:s}...", message); using (var stream = new MemoryStream()) { using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true)) { foreach (var filename in Directory.GetFiles(dirname, "*.png")) { var heroname = Path.GetFileNameWithoutExtension(filename); using (var portrait = new Emgu.CV.Image <Emgu.CV.Structure.Bgra, byte>(filename)) using (var kp = new Emgu.CV.Util.VectorOfKeyPoint()) using (var des = new Emgu.CV.Mat()) { sift.DetectAndCompute(portrait, null, kp, des, false); var file = archive.CreateEntry(heroname, CompressionLevel.Optimal); using (var fstream = file.Open()) using (var writer = new BinaryWriter(fstream)) { var bytes = new byte[des.Cols * des.Rows * des.ElementSize]; unsafe { fixed(byte *ptr = bytes) { Buffer.MemoryCopy(des.DataPointer.ToPointer(), ptr, bytes.Length, bytes.Length); } } writer.Write(bytes); } } } } using (var fstream = new FileStream(dirname + ".zip", FileMode.Create)) { stream.Seek(0, SeekOrigin.Begin); stream.CopyTo(fstream); } } Console.WriteLine("Done."); }
private static void ExternalBRISKDescriptors(ref Image<Gray, Byte> frame, MKeyPoint[] keypoints, out Matrix<byte> features) { //Console.WriteLine("External BRISK Keypoint Descriptor Extractor"); Emgu.CV.Util.VectorOfKeyPoint keypoint_vector = new Emgu.CV.Util.VectorOfKeyPoint(); keypoint_vector.Push(keypoints); features = featureDetector.ComputeDescriptorsRaw(frame, null, keypoint_vector); }
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(); })); } }