Beispiel #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();
                                }
                            }
        }
Beispiel #2
0
        public Bitmap GetCombinedImage(Bitmap originalImage, Bitmap updateImage, bool simpleCombine)
        {
            int width, height, originalImageX, originalImageY, updateImageX, updateImageY;

            if (simpleCombine)
            {
                width          = originalImage.Width + updateImage.Width;
                height         = Math.Max(originalImage.Height, updateImage.Height);
                originalImageX = 0;
                originalImageY = 0;
                updateImageX   = originalImage.Width;
                updateImageY   = 0;
            }
            else
            {
                Tuple <int, int> offsets = ImageFeatureDetector.GetXYOffsets(originalImage, updateImage);

                if (offsets.Item1 >= 0)
                {
                    width          = (originalImage.Width > offsets.Item1 + updateImage.Width) ? originalImage.Width : offsets.Item1 + updateImage.Width;
                    originalImageX = 0;
                    updateImageX   = offsets.Item1;
                }
                else
                {
                    width          = -offsets.Item1 + ((originalImage.Width > offsets.Item1 + updateImage.Width) ? originalImage.Width : offsets.Item1 + updateImage.Width);
                    originalImageX = -offsets.Item1;
                    updateImageX   = 0;
                }

                if (offsets.Item2 >= 0)
                {
                    height         = (originalImage.Height > offsets.Item2 + updateImage.Height) ? originalImage.Height : offsets.Item2 + updateImage.Height;
                    originalImageY = 0;
                    updateImageY   = offsets.Item2;
                }
                else
                {
                    height         = -offsets.Item2 + ((originalImage.Height > offsets.Item2 + updateImage.Height) ? originalImage.Height : offsets.Item2 + updateImage.Height);
                    originalImageY = -offsets.Item2;
                    updateImageY   = 0;
                }
            }

            Bitmap bmp = new Bitmap(width, height);

            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.DrawImage(originalImage, originalImageX, originalImageY);
                g.DrawImage(updateImage, updateImageX, updateImageY);
            }

            return(bmp);
        }
Beispiel #3
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();
                                }
                            }
                        }
        }