public KeyPoints SIFTDescriptor() { KeyPoints result = new KeyPoints(); //SiFT Descriptor SIFT siftAlgo = null; VectorOfKeyPoint modelKeyPointsSift = null; try { siftAlgo = new SIFT(); modelKeyPointsSift = new VectorOfKeyPoint(); MKeyPoint[] siftPoints = siftAlgo.Detect(preProcessedImageInGrayScale); modelKeyPointsSift.Push(siftPoints); UMat siftDescriptors = new UMat(); siftAlgo.DetectAndCompute(preProcessedImageInGrayScale, null, modelKeyPointsSift, siftDescriptors, true); Image <Gray, Byte> outputImage = new Image <Gray, byte>( preProcessedImageInGrayScale.Width, preProcessedImageInGrayScale.Height); Features2DToolbox.DrawKeypoints( preProcessedImageInGrayScale, modelKeyPointsSift, outputImage, new Bgr(255, 255, 255), Features2DToolbox.KeypointDrawType.Default); string folderName = @"C:\Projects\LeafService\SiftImage"; string pathString = System.IO.Path.Combine(folderName, "Sift" + DateTime.UtcNow.Ticks); System.IO.Directory.CreateDirectory(pathString); if (Directory.Exists(pathString)) { string newFilePath = Path.Combine(pathString, "SiftImage" + DateTime.UtcNow.Ticks); outputImage.Save(folderName + ".jpg"); outputImage.Save(@"C:\Projects\LeafService\SIFTgray.jpg"); } //outputImage.Save("sift.jpg"); result.Descriptor = siftDescriptors; result.Points = siftPoints; return(result); } finally { siftAlgo.Dispose(); modelKeyPointsSift.Dispose(); } }
public void DrawSIFTDescriptor(string inputFile, string outputFile) { //SiFT Descriptor SIFT siftAlgo = null; VectorOfKeyPoint modelKeyPointsSift = null; try { siftAlgo = new SIFT(); modelKeyPointsSift = new VectorOfKeyPoint(); using (Image <Bgr, byte> inputImage = new Image <Bgr, byte>(inputFile)) { MKeyPoint[] siftPoints = siftAlgo.Detect(inputImage); modelKeyPointsSift.Push(siftPoints); UMat siftDescriptors = new UMat(); siftAlgo.DetectAndCompute(inputImage, null, modelKeyPointsSift, siftDescriptors, true); using (Image <Gray, Byte> outputImage = new Image <Gray, byte>( inputImage.Width, inputImage.Height)) { Features2DToolbox.DrawKeypoints( inputImage, modelKeyPointsSift, outputImage, new Bgr(255, 255, 255), Features2DToolbox.KeypointDrawType.Default); outputImage.Save(outputFile); } } } finally { siftAlgo.Dispose(); modelKeyPointsSift.Dispose(); } }
public override void Dispose() { sift.Dispose(); }
public ConcurrentDictionary <string, MatOfFloat> CalcSiftHashes(IEnumerable <ImageInfo> infos, out Task result, int thumbSize = 100) { Trace.WriteLine("CalcSiftHashes started"); EnablePublishingProgress(); var hashesDict = new ConcurrentDictionary <string, MatOfFloat>(); var tasks = new List <Task>(); foreach (ImageInfo info in infos) { var task = new Task(() => { Thread.CurrentThread.Priority = ThreadPriority.Lowest; //Thread.Sleep(1); //var mem = new MemoryStream(); //// copy to byte array //int stride = ((BitmapImage)info.Image).PixelWidth * 4; //byte[] buffer = new byte[stride * ((BitmapImage)info.Image).PixelHeight]; //((BitmapImage)info.Image).CopyPixels(buffer, stride, 0); //// create bitmap //Bitmap bitmap = new Bitmap(((BitmapImage)info.Image).PixelWidth, ((BitmapImage)info.Image).PixelHeight, PixelFormat.Format32bppArgb); //// lock bitmap data //BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, bitmap.PixelFormat); //// copy byte array to bitmap data //Marshal.Copy(buffer, 0, bitmapData.Scan0, buffer.Length); //// unlock //bitmap.UnlockBits(bitmapData); //bitmap.Save(mem,ImageFormat.Bmp); //Mat sourceMat = Cv2.ImDecode(mem.GetBuffer(), ImreadModes.Unchanged); //var resizedMat = sourceMat.Resize(new OpenCvSharp.Size(thumbSize, thumbSize), 0, 0, InterpolationFlags.Nearest); //var scale = (double)thumbSize / Max(sourceMat.Width, sourceMat.Height); //var resizedMat = sourceMat.Resize(new OpenCvSharp.Size(0, 0), scale, scale, InterpolationFlags.Nearest); //var grayScaledMat = new Mat(); //Cv2.CvtColor(resizedMat, grayScaledMat, ColorConversionCodes.BGR2GRAY); //var siftPoints = SURF.Create(400); SIFT siftPoints = SIFT.Create(); MatOfFloat descriptors = new MatOfFloat(); //var keypoints = siftPoints.Detect(info.StoredMat).ToArray(); //siftPoints.Compute(info.StoredMat, ref keypoints, descriptors); double scale = Math.Min((float)thumbSize / info.StoredMat.Width, (float)thumbSize / info.StoredMat.Height); Mat resized = info.StoredMat.Resize(new OpenCvSharp.Size(0, 0), scale, scale, InterpolationFlags.Area); siftPoints.DetectAndCompute(resized, null, out KeyPoint[] keypoints, descriptors); resized.Release(); if (!ValidateDescriptor(descriptors)) { descriptors.Release(); descriptors = new MatOfFloat(thumbSize, thumbSize); } hashesDict.TryAdd(info.FilePath, descriptors); //resizedMat?.Dispose(); siftPoints.Dispose(); //grayScaledMat.Dispose(); //resizedMat.Release(); //sourceMat.Release(); UpdateIterationsCount(); }); tasks.Add(task); } SetProgressIterationsScope(tasks); foreach (var task in tasks) { task.Start(); } result = Task.WhenAll(tasks.ToArray()); result.ContinueWith(o => DisiblePublishingProgress()); return(hashesDict); }