Android 如何优化多图像拼接? 我正在Visual Studio 2012,C++中进行多图像拼接。我已经根据我的要求进行了修改,它提供了高质量的结果。这里的问题是,它需要太多的时间来执行。对于10张图像,大约需要110秒

Android 如何优化多图像拼接? 我正在Visual Studio 2012,C++中进行多图像拼接。我已经根据我的要求进行了修改,它提供了高质量的结果。这里的问题是,它需要太多的时间来执行。对于10张图像,大约需要110秒,android,c++,opencv,image-stitching,Android,C++,Opencv,Image Stitching,以下是花费大部分时间的地方: 1) 成对匹配-10张图像需要55秒!我正在使用ORB查找特征点。代码如下: vector<MatchesInfo> pairwise_matches; BestOf2NearestMatcher matcher(false, 0.35); matcher(features, pairwise_matches); matcher.collectGarbage(); for (int img_idx = 0; img_idx < num_image

以下是花费大部分时间的地方:

1) 成对匹配-10张图像需要55秒!我正在使用ORB查找特征点。代码如下:

vector<MatchesInfo> pairwise_matches;
BestOf2NearestMatcher matcher(false, 0.35);
matcher(features, pairwise_matches);
matcher.collectGarbage();
for (int img_idx = 0; img_idx < num_images; ++img_idx)
{
    printf("Compositing image #%d\n",indices[img_idx]+1);

    // Read image and resize it if necessary
    full_img = imread(img_names[img_idx]);

    Mat K;
    cameras[img_idx].K().convertTo(K, CV_32F);

    // Warp the current image
    warper->warp(full_img, K, cameras[img_idx].R, INTER_LINEAR, BORDER_REFLECT, img_warped);

    // Warp the current image mask
    mask.create(full_img.size(), CV_8U);
    mask.setTo(Scalar::all(255));
    warper->warp(mask, K, cameras[img_idx].R, INTER_NEAREST, BORDER_CONSTANT, mask_warped);

    // Compensate exposure
    compensator->apply(img_idx, corners[img_idx], img_warped, mask_warped);

    img_warped.convertTo(img_warped_s, CV_16S);
    img_warped.release();
    full_img.release();
    mask.release();

    dilate(masks_warped[img_idx], dilated_mask, Mat());
    resize(dilated_mask, seam_mask, mask_warped.size());
    mask_warped = seam_mask & mask_warped;

    // Blend the current image
    blender->feed(img_warped_s, mask_warped, corners[img_idx]);
}

Mat result, result_mask;
blender->blend(result, result_mask);
向量成对匹配;
最接近匹配者的最佳匹配者(假,0.35);
匹配器(特征、成对匹配);
matcher.collectGarbage();
我尝试使用此代码,因为我已经知道图像的顺序:

vector<MatchesInfo> pairwise_matches;
BestOf2NearestMatcher matcher(false, 0.35);

Mat matchMask(features.size(),features.size(),CV_8U,Scalar(0));
for (int i = 0; i < num_images -1; ++i)                                                 
    matchMask.at<char>(i,i+1) =1;                                                       
matcher(features, pairwise_matches, matchMask);                                         

matcher.collectGarbage();
向量成对匹配;
最接近匹配者的最佳匹配者(假,0.35);
Mat matchMask(features.size()、features.size()、CV_8U、标量(0));
对于(int i=0;i
它肯定会缩短时间(18秒),但不会产生所需的结果。只有6个图像被缝合(最后4个被忽略,因为图像6和图像7的特征点不匹配。因此循环中断。)

2) 合成-10张图像需要38秒!代码如下:

vector<MatchesInfo> pairwise_matches;
BestOf2NearestMatcher matcher(false, 0.35);
matcher(features, pairwise_matches);
matcher.collectGarbage();
for (int img_idx = 0; img_idx < num_images; ++img_idx)
{
    printf("Compositing image #%d\n",indices[img_idx]+1);

    // Read image and resize it if necessary
    full_img = imread(img_names[img_idx]);

    Mat K;
    cameras[img_idx].K().convertTo(K, CV_32F);

    // Warp the current image
    warper->warp(full_img, K, cameras[img_idx].R, INTER_LINEAR, BORDER_REFLECT, img_warped);

    // Warp the current image mask
    mask.create(full_img.size(), CV_8U);
    mask.setTo(Scalar::all(255));
    warper->warp(mask, K, cameras[img_idx].R, INTER_NEAREST, BORDER_CONSTANT, mask_warped);

    // Compensate exposure
    compensator->apply(img_idx, corners[img_idx], img_warped, mask_warped);

    img_warped.convertTo(img_warped_s, CV_16S);
    img_warped.release();
    full_img.release();
    mask.release();

    dilate(masks_warped[img_idx], dilated_mask, Mat());
    resize(dilated_mask, seam_mask, mask_warped.size());
    mask_warped = seam_mask & mask_warped;

    // Blend the current image
    blender->feed(img_warped_s, mask_warped, corners[img_idx]);
}

Mat result, result_mask;
blender->blend(result, result_mask);
for(int-img\u-idx=0;img\u-idx翘曲(完整的img,K,摄像机[img\u idx].R,内部线性,边界反射,img\u翘曲);
//扭曲当前图像遮罩
创建(完整img.size(),CV_8U);
setTo(标量::all(255));
翘曲器->翘曲(遮罩,K,摄像机[img_idx].R,最近的间隔,边界常数,遮罩翘曲);
//补偿曝光
补偿器->应用(img_idx、角[img_idx]、img_扭曲、掩模_扭曲);
img_翘曲。转换为(img_翘曲,CV_16S);
img_翘曲。释放();
完全释放();
面具。释放();
扩张(遮罩扭曲[img_idx],扩张遮罩,Mat());
调整大小(放大的遮罩、接缝遮罩、扭曲的遮罩.size());
遮罩扭曲=接缝遮罩和遮罩扭曲;
//混合当前图像
搅拌机->进料(img_翘曲、掩模_翘曲、角[img_idx]);
}
Mat result,result\u mask;
混合器->混合(结果,结果屏蔽);
原始图像分辨率为4160*3120。我没有在合成中使用压缩,因为它会降低质量。我在代码的其余部分使用了压缩图像

如您所见,我修改了代码并缩短了时间。但我还是想尽量减少时间

3) 使用ORB查找特征点。10张图像需要10秒。为图像查找最大1530个要素点

55+38+10=103+7代码的其余部分=110。

当我在安卓系统中使用这段代码时,几乎需要整个智能手机的内存(RAM)才能执行。如何减少android设备的时间和内存消耗?(我使用的安卓设备有2GB内存)

我已经优化了其余的代码。非常感谢您的帮助

编辑1:我在合成步骤中使用了图像压缩,时间从38秒减少到16秒。我还设法减少了代码其余部分的时间

现在,从110->85秒开始。帮助我减少成对匹配的时间;我不知道如何减少它


编辑2:我在中找到成对匹配的代码。我在主代码中创建了自己的函数来优化时间。对于合成步骤,我使用了压缩,直到最终图像不丢失清晰度。对于特征查找,我使用图像缩放以缩小图像比例查找图像特征。现在我可以轻松地缝合多达50张图像。

因为55到18秒是一个很好的改进,也许你可以稍微控制一下匹配过程。我首先建议的是——如果你还没有——学会调试过程中的每一步,了解图像没有粘贴时会出现什么问题。这样,您将始终学会控制正在检测的ORB功能的数量。也许在某些情况下,您可以限制它们,但仍然可以获得结果,从而加快过程(这不仅应该加快查找特征的速度,还应该加快匹配过程)

希望这将使您能够在循环中断时检测到情况。因此,您可以相应地对情况作出反应。您仍然可以在循环中匹配序列,从而节省时间,但当您检测到匹配该特定对存在问题时,会强制程序继续(或更改参数并再次尝试匹配该对)

我认为这里的构图过程没有太大的改进空间,因为你不想失去质量。如果我是你,我会尝试研究的是线程和并行计算是否有帮助

这是一个有趣且广泛存在的问题——如果你能够在不放弃质量的情况下加快速度,你应该给LG或谷歌打电话,因为我在Nexus中的算法质量很差:)它既慢又不准确