1
$\begingroup$

I'm trying to fuse different images of the same scene which each image have a different focus. I've been told that first I have to perform an image registration to the images. Right now, the result from the registration has errors, which can be seen by "aura" around the objects in the images and shrinking the image edges.

When I'm trying to register images with focuses that their distance is big, I'm getting a result images with much more significant errors.

I'm using the following code (using the Accord library):

 class Registration
{
private List<Image<Bgr, Byte>> myOriginalImages;
private List<Bitmap> registratedImages = new List<Bitmap>();

public Registration(List<Image<Bgr, Byte>> myOriginalImages)
{
    this.myOriginalImages = myOriginalImages;
}

public List<Bitmap> getRegistratedImages()
{
    return registratedImages;
}
public void Run()
{
    int numOfTargetImages = myOriginalImages.Count - 1;
    SpeededUpRobustFeaturePoint[] surfPoints1;
    SpeededUpRobustFeaturePoint[] surfPoints2;
    IntPoint[] correlationPoints1;
    IntPoint[] correlationPoints2;
    MatrixH homography;
    Bitmap registratedImage;

    // first image is the model image - no registration required
    registratedImages.Add(myOriginalImages.ElementAt(0).ToBitmap());
    Bitmap modelImage = registratedImages.ElementAt(0);

    // create the registrated images
    for (int i = 1; i <= numOfTargetImages; i++)
    {
        ExtractFeatures(modelImage, myOriginalImages.ElementAt(i).ToBitmap(), out surfPoints1, out surfPoints2);
        MatchFeatures(surfPoints1, surfPoints2, out correlationPoints1, out correlationPoints2);
        homography = RANSAC(correlationPoints1, correlationPoints2);
        registratedImage = CreateRegisteredImage(modelImage, myOriginalImages.ElementAt(i).ToBitmap(), homography);

        registratedImages.Add(registratedImage);
    }
}
private void ExtractFeatures(Bitmap img1, Bitmap img2, out SpeededUpRobustFeaturePoint[] surfPoints1, out SpeededUpRobustFeaturePoint[] surfPoints2)
{// extract features using SURF detector
    SpeededUpRobustFeaturesDetector surf = new SpeededUpRobustFeaturesDetector();

    surfPoints1 = surf.ProcessImage(img1).ToArray();
    surfPoints2 = surf.ProcessImage(img2).ToArray();
}

private void MatchFeatures(SpeededUpRobustFeaturePoint[] surfPoints1, SpeededUpRobustFeaturePoint[] surfPoints2, out IntPoint[] correlationPoints1, out IntPoint[] correlationPoints2)
{// match the features between the images by using correlation
    KNearestNeighborMatching matcher = new KNearestNeighborMatching(5);
    IntPoint[][] matches = matcher.Match(surfPoints1, surfPoints2);

    correlationPoints1 = matches[0];
    correlationPoints2 = matches[1];
}

private MatrixH RANSAC(IntPoint[] correlationPoints1, IntPoint[] correlationPoints2)
{
    RansacHomographyEstimator ransac = new RansacHomographyEstimator(0.001, 0.99);
    MatrixH homography = ransac.Estimate(correlationPoints1, correlationPoints2);

    return homography;
}

private Bitmap CreateRegisteredImage(Bitmap img1, Bitmap img2, MatrixH homography)
{
    Blend blend = new Blend(homography, img1);
    return blend.Apply(img2);
}

I'm getting the following images: Result Image with closer focuses

Sample images were taken from HeliconFocus for homework

Result image with far focuses

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.