Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimal working example for Microsoft Image Composite Editor StitchEngine.dll

We want to use the StitchEngine.dll from Microsofts Image Composite Editor (ICE) in one of our C# projects. There is minimal evidence, that people already accomplished this task, e.g. Richard in this SO question Getting an "InvalidCastException" when passing a Rectangle ValueType in c#. So, our question is, if somebody could provide a minimal working example, i.e. loading two images, stitching them, and exporting the resulting image.

At the moment, we are already stuck at the loading (or initialization) part. We have investigated the StitchEngineWrapper and StitchProjectInfo classes, but could not figure out, how exactly to load images. The StitchEngineWrapper.AddImageCacheLocations(IReadOnlyList<string> imageCacheLocations) method did not work for us!? The List<ImageInfo> StitchProjectInfo.SourceImages property is not accessible!? Using ILSpy didn't help us either.

Every hint in the right direction is much appreciated! Hopefully, Richard will see this question.

like image 765
HansHirse Avatar asked Sep 03 '25 03:09

HansHirse


1 Answers

ICE is a state machine running in the background, so you have to wait for each step completion before going to the next one. Here is a sample Console C# app that should work, provided you added at least two valid images (test with the UI first):

class Program
{
    static void Main(string[] args)
    {
        using (var stitch = new StitchEngineWrapper()) // using Microsoft.Research.ICE.Stitching;
        {
            var taskCompleted = new AutoResetEvent(false);
            stitch.ProgressChanged += (s, e) => Console.Write(".");
            stitch.TaskCompleted += (s, e) =>
            {
                Console.WriteLine();
                taskCompleted.Set();
            };

            var pi = new StitchProjectInfo();
            pi.SourceImages.Add(new ImageInfo(@"c:\myPath\lenna1.jpg", null));
            pi.SourceImages.Add(new ImageInfo(@"c:\myPath\lenna2.jpg", null));
            if (!stitch.InitializeFromProjectInfo(pi) || stitch.HasLastError)
            {
                Console.WriteLine("Initialization failed.");
                if (stitch.HasLastError)
                {
                    Console.WriteLine("Error 0x" + stitch.LastError.ToString("x8") + ": " + stitch.LastErrorMessage);
                }
                return;
            }
            Console.WriteLine("Initialization ok.");

            stitch.StartAligning();
            taskCompleted.WaitOne(Timeout.Infinite);
            if (stitch.AlignedCount < 2 || stitch.HasLastError)
            {
                Console.WriteLine("Alignement failed. Wrong input.");
                Console.WriteLine("Error 0x" + stitch.LastError.ToString("x8") + ": " + stitch.LastErrorMessage);
                return;
            }
            Console.WriteLine("Alignement ok.");

            stitch.StartCompositing();
            taskCompleted.WaitOne(Timeout.Infinite);
            if (stitch.HasLastError)
            {
                Console.WriteLine("Composition failed.");
                Console.WriteLine("Error 0x" + stitch.LastError.ToString("x8") + ": " + stitch.LastErrorMessage);
                return;
            }
            Console.WriteLine("Composition ok.");

            stitch.StartProjecting();
            taskCompleted.WaitOne(Timeout.Infinite);
            if (stitch.HasLastError)
            {
                Console.WriteLine("Projection failed.");
                Console.WriteLine("Error 0x" + stitch.LastError.ToString("x8") + ": " + stitch.LastErrorMessage);
                return;
            }
            Console.WriteLine("Projection ok.");

            var options = new OutputOptions(ExportFormat.JPEG, 75, true, false, false);
            stitch.StartExporting(@"c:\myPath\stitched.jpg", stitch.ResetCropRect, 1, options, false);
            taskCompleted.WaitOne(Timeout.Infinite);
            Console.WriteLine("Export ok.");
        }
    }
}

Here is a sample Lenna stiched (note images Y-offset and overlap):

left:

enter image description here

right:

enter image description here

stitched:

enter image description here

Note black artifacts and funny hair on the right due to stitching

like image 136
Simon Mourier Avatar answered Sep 05 '25 00:09

Simon Mourier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!