在使用OpenCV的三维立体重建的库时,一个重要的步骤就是生成左右视图的差异图Disparity,而控制生成disparity的参数的类是cv::StereoBM,我们有时候需要拷贝一份cv::StereoBM,然后改变其中的参数值,但是如果用默认的等号‘=’来进行拷贝,其实是浅拷贝,如果改变拷贝项的参数值,原来的参数值也会跟着改变,所以我们需要自己写一个深拷贝的函数,如下所示:

/**
* Deep copy cv::StereoBM bm1 to bm2
*/
void copy_bm(const cv::StereoBM &bm1, cv::StereoBM &bm2) {
if (!bm1.state) {
return;
}
bm2.init(CV_STEREO_BM_NORMALIZED_RESPONSE, , );
bm2.state->preFilterSize = bm1.state->preFilterSize;
bm2.state->preFilterCap = bm1.state->preFilterCap;
bm2.state->SADWindowSize = bm1.state->SADWindowSize;
bm2.state->minDisparity = bm1.state->minDisparity;
bm2.state->numberOfDisparities = bm1.state->numberOfDisparities;
bm2.state->textureThreshold = bm1.state->textureThreshold;
bm2.state->uniquenessRatio = bm1.state->uniquenessRatio;
bm2.state->speckleWindowSize = bm1.state->speckleWindowSize;
bm2.state->speckleRange = bm1.state->speckleRange;
}
05-11 09:21