Exemplo n.º 1
0
        /// <summary>
        /// Draw the model image and observed image, the matched features and homography projection.
        /// </summary>
        /// <param name="modelImage">The model image</param>
        /// <param name="observedImage">The observed image</param>
        /// <returns>The model image and observed image, the matched features and homography projection.</returns>
        public Bitmap GetImageWithDrawnMatches(Bitmap modelImage, Bitmap observedImage, MatchingTechnique matchingTechnique)
        {
            VectorOfKeyPoint modelKeyPoints;
            VectorOfKeyPoint observedKeyPoints;

            using (Image <Bgr, byte> modelImg = new Image <Bgr, byte>(modelImage))
                using (Image <Bgr, byte> observedImg = new Image <Bgr, byte>(observedImage))
                    using (Emgu.CV.Mat modelMat = modelImg.Mat)
                        using (Emgu.CV.Mat observedMat = observedImg.Mat)
                            using (VectorOfVectorOfDMatch matches = new VectorOfVectorOfDMatch())
                            {
                                ImageFeatureDetector.FindMatches(modelMat, observedMat, out modelKeyPoints, out observedKeyPoints, matches, out Mat mask, out Mat homography, matchingTechnique);

                                try
                                {
                                    using (Mat result = new Mat())
                                    {
                                        Features2DToolbox.DrawMatches(modelMat, modelKeyPoints, observedMat, observedKeyPoints, matches, result, new MCvScalar(255, 0, 0), new MCvScalar(0, 0, 255), mask);

                                        return(result.ToBitmap());
                                    }
                                }
                                catch (Exception)
                                {
                                    throw;
                                }
                                finally
                                {
                                    mask?.Dispose();
                                    homography?.Dispose();
                                }
                            }
        }
Exemplo n.º 2
0
        public Bitmap GetAlignedImage(Bitmap imageToAlign, Bitmap referenceImage, ImageAlignmentType imageAlignmentType)
        {
            if (imageAlignmentType == ImageAlignmentType.CROP || imageAlignmentType == ImageAlignmentType.MAP)
            {
                Tuple <int, int> offsets = ImageFeatureDetector.GetXYOffsets(imageToAlign, referenceImage);

                if (imageAlignmentType == ImageAlignmentType.CROP)
                {
                    return(GetCroppedImage(imageToAlign, offsets.Item1, offsets.Item2, referenceImage.Width, referenceImage.Height));
                }

                Bitmap bmp = new Bitmap(referenceImage.Width, referenceImage.Height);
                using (Graphics g = Graphics.FromImage(bmp))
                    using (Brush brush = new SolidBrush(Color.Black))
                    {
                        g.FillRectangle(brush, 0, 0, bmp.Width, bmp.Height);
                        g.DrawImage(imageToAlign, -offsets.Item1, -offsets.Item2);
                    }

                return(bmp);
            }

            using (Image <Bgr, byte> alignImg = new Image <Bgr, byte>(imageToAlign))
                using (Image <Bgr, byte> refImg = new Image <Bgr, byte>(referenceImage))
                    using (Mat alignMat = alignImg.Mat)
                        using (Mat refMat = refImg.Mat)
                        {
                            using (VectorOfVectorOfDMatch matches = new VectorOfVectorOfDMatch())
                            {
                                MatchingTechnique matchingTechnique;
                                if (imageAlignmentType == ImageAlignmentType.FASTWARP)
                                {
                                    matchingTechnique = MatchingTechnique.FAST;
                                }
                                else if (imageAlignmentType == ImageAlignmentType.FULLWARP)
                                {
                                    matchingTechnique = MatchingTechnique.ORB;
                                }
                                else
                                {
                                    throw new NotImplementedException();
                                }

                                ImageFeatureDetector.FindMatches(alignMat, refMat, out VectorOfKeyPoint modelKeyPoints, out VectorOfKeyPoint observedKeyPoints, matches, out Mat mask, out Mat homography, matchingTechnique, 1.0f);

                                try
                                {
                                    using (Mat result = new Mat())
                                    {
                                        Features2DToolbox.DrawMatches(alignMat, modelKeyPoints, refMat, observedKeyPoints, matches, result, new MCvScalar(255, 0, 0), new MCvScalar(0, 0, 255), mask);
                                        result.Save(@"D:\Downloads\Draw.jpg");
                                    }

                                    using (Mat warped = new Mat())
                                    {
                                        if (homography == null)
                                        {
                                            throw new Exception("Could not determine homography between images.");
                                        }

                                        CvInvoke.WarpPerspective(alignMat, warped, homography, refMat.Size);

                                        return(warped.ToBitmap());
                                    }
                                }
                                catch (Exception)
                                {
                                    throw;
                                }
                                finally
                                {
                                    mask.Dispose();
                                    homography.Dispose();
                                }
                            }
                        }
        }