问题
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.
回答1:
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:
right:
stitched:
Note black artifacts and funny hair on the right due to stitching
来源:https://stackoverflow.com/questions/54800001/minimal-working-example-for-microsoft-image-composite-editor-stitchengine-dll