📄 blendimages.cpp
字号:
///////////////////////////////////////////////////////////////////////////
//
// NAME
// BlendImages.cpp -- blend together a set of overlapping images
//
// DESCRIPTION
// This routine takes a collection of images aligned more or less horizontally
// and stitches together a mosaic.
//
// The images can be blended together any way you like, but I would recommend
// using a soft halfway blend of the kind Steve presented in the first lecture.
//
// Once you have blended the images together, you should crop the resulting
// mosaic at the halfway points of the first and last image. You should also
// take out any accumulated vertical drift using an affine warp.
// Lucas-Kanade Taylor series expansion of the registration error.
//
// SEE ALSO
// BlendImages.h longer description of parameters
//
// Copyright ?Richard Szeliski, 2001. See Copyright.h for more details
// (modified for CSE455 Winter 2003)
//
///////////////////////////////////////////////////////////////////////////
#include "ImageLib/ImageLib.h"
#include "BlendImages.h"
#include <math.h>
/******************* TO DO 4 *********************
* AccumulateBlend:
* INPUT:
* img: a new image to be added to acc
* acc: portion of the accumulated image where img is to be added
* blendWidth: width of the blending function (horizontal hat function;
* try other blending functions for extra credit)
* OUTPUT:
* add a weighted copy of img to the subimage specified in acc
* the first 3 band of acc records the weighted sum of pixel colors
* the fourth band of acc records the sum of weight
*/
static void AccumulateBlend(CByteImage& img, CFloatImage& acc, float blendWidth)
{
// *** BEGIN TODO ***
// fill in this routine..
// *** END TODO ***
}
/******************* TO DO 5 *********************
* NormalizeBlend:
* INPUT:
* acc: input image whose alpha channel (4th channel) contains
* normalizing weight values
* img: where output image will be stored
* OUTPUT:
* normalize r,g,b values (first 3 channels) of acc and store it into img
*/
static void NormalizeBlend(CFloatImage& acc, CByteImage& img)
{
// *** BEGIN TODO ***
// fill in this routine..
// *** END TODO ***
}
/******************* TO DO 5 *********************
* BlendImages:
* INPUT:
* ipv: list of input images and their relative positions in the mosaic
* blendWidth: width of the blending function
* OUTPUT:
* create & return final mosaic by blending all images
* and correcting for any vertical drift
*/
CByteImage BlendImages(CImagePositionV& ipv, float blendWidth)
{
// Assume all the images are of the same shape (for now)
CByteImage& img0 = ipv[0].img;
CShape sh = img0.Shape();
int width = sh.width;
int height = sh.height;
int nBands = sh.nBands;
int dim[2] = {width, height};
// Compute the bounding box for the mosaic
int n = ipv.size();
float min_x = 0, min_y = 0;
float max_x = 0, max_y = 0;
int i;
for (i = 0; i < n; i++)
{
float* pos = ipv[i].position;
// *** BEGIN TODO #1 ***
// add some code here to update min_x, ..., max_y
// *** END TODO #1 ***
}
// Create a floating point accumulation image
CShape mShape((int)(ceil(max_x) - floor(min_x)),
(int)(ceil(max_y) - floor(min_y)), nBands);
CFloatImage accumulator(mShape);
accumulator.ClearPixels();
// Add in all of the images
for (i = 0; i < n; i++)
{
// Compute the sub-image involved
float* pos = ipv[i].position;
int start_x = 0, start_y = 0;
// *** BEGIN TODO #2 ***
// fill in with the correct values for start_x and start_y,
// the relative position of i-th image in the mosaic
// hint: we only require you to use pixel accuracy to start_x and start_y;
// *** END TODO #2 ***
CByteImage& img = ipv[i].img;
CFloatImage sub = accumulator.SubImage(start_x, start_y, width, height);
// Perform the accumulation
AccumulateBlend(img, sub, blendWidth);
}
// Normalize the results
CByteImage compImage(mShape);
NormalizeBlend(accumulator, compImage);
bool debug_comp = false;
if (debug_comp)
WriteFile(compImage, "tmp_comp.tga");
// Allocate the final image shape
CShape cShape(mShape.width - width, height, nBands);
CByteImage croppedImage(cShape);
// Compute the affine deformation
CTransform3x3 A;
// *** BEGIN TODO #3 ***
// fill in the right entries in A to trim the left edge and
// to take out the vertical drift
// *** END TODO #3 ***
// Warp and crop the composite
WarpGlobal(compImage, croppedImage, A, eWarpInterpLinear);
return croppedImage;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -