将图像多曝光融合功能封装为函数可以使代码更加模块化和易于重用。下面是一个示例,展示了如何将原来的代码中的图像读取、对齐和融合操作封装到一个名为 exposureFusion
的函数中。
#include "pre_process.h"
using namespace cv;
using namespace std;
// Function to read images from specified paths
void readImages(vector<Mat>& images) {
int numImages = 3;
static const char* filenames[] =
{
"Data/img_test/6.第三面侧视图/100.bmp",
"Data/img_test/6.第三面侧视图/101.bmp",
"Data/img_test/6.第三面侧视图/102.bmp",
};
// Read images
for (int i = 0; i < numImages; i++) {
Mat im = imread(filenames[i]);
if (!im.empty()) {
images.push_back(im);
} else {
cout << "Could not read image: " << filenames[i] << endl;
}
}
}
// Function to perform exposure fusion on a vector of images
void exposureFusion(const vector<Mat>& inputImages, Mat& outputImage, bool needsAlignment) {
// Align input images if needed
vector<Mat> images = inputImages;
if (needsAlignment) {
cout << "Aligning images ... " << endl;
Ptr<AlignMTB> alignMTB = createAlignMTB();
alignMTB->process(images, images);
} else {
cout << "Skipping alignment ... " << endl;
}
// Merge using Exposure Fusion
cout << "Merging using Exposure Fusion ... " << endl;
Ptr<MergeMertens> mergeMertens = createMergeMertens();
mergeMertens->process(images, outputImage);
outputImage.convertTo(outputImage, CV_8U, 255); // Convert to proper format
}
int main() {
cout << "Reading images ... " << endl;
vector<Mat> images;
// Read example images
readImages(images);
// Variable indicating whether to align the input images
bool needsAlignment = false;
// Output image for the result of exposure fusion
Mat exposureFusionResult;
// Call the function to perform exposure fusion
exposureFusion(images, exposureFusionResult, needsAlignment);
// Save output image
cout << "Saving output ... exposure-fusion.bmp" << endl;
imwrite("Data/img_test_out/6.第三面侧视图/6-6.bmp", exposureFusionResult);
return 0;
}
代码说明:
readImages
函数:负责读取指定路径的图像,并将其存储在images
向量中。exposureFusion
函数:执行曝光融合的主逻辑。它接受输入图像向量、输出结果的引用以及是否需要对齐的标志。- 在
main
函数中,我们调用readImages
来读取图片,然后通过调用exposureFusion
执行融合处理,最后保存结果。
这样,通过将功能封装到函数中,不仅增强了代码的可读性,还便于后续的维护与扩展。
内容由零声教学AI助手提供,问题来源于学员提问