From f44632ee6f6fbd868d33d823b2af52bda0697786 Mon Sep 17 00:00:00 2001 From: Alexey Spizhevoy Date: Mon, 23 May 2011 13:08:31 +0000 Subject: [PATCH] switched float with short in blending step (opencv_stitching) --- modules/imgproc/src/pyramids.cpp | 4 +++ modules/stitching/autocalib.cpp | 2 +- modules/stitching/blenders.cpp | 47 ++++++++++++++++++++------------ modules/stitching/main.cpp | 7 ++--- 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/modules/imgproc/src/pyramids.cpp b/modules/imgproc/src/pyramids.cpp index 83a75de8da..5ec5732aec 100644 --- a/modules/imgproc/src/pyramids.cpp +++ b/modules/imgproc/src/pyramids.cpp @@ -411,6 +411,8 @@ void cv::pyrDown( const InputArray& _src, OutputArray _dst, const Size& _dsz ) PyrFunc func = 0; if( depth == CV_8U ) func = pyrDown_, PyrDownVec_32s8u>; + else if( depth == CV_16S ) + func = pyrDown_, NoVec >; else if( depth == CV_16U ) func = pyrDown_, NoVec >; else if( depth == CV_32F ) @@ -433,6 +435,8 @@ void cv::pyrUp( const InputArray& _src, OutputArray _dst, const Size& _dsz ) PyrFunc func = 0; if( depth == CV_8U ) func = pyrUp_, NoVec >; + else if( depth == CV_16S ) + func = pyrUp_, NoVec >; else if( depth == CV_16U ) func = pyrUp_, NoVec >; else if( depth == CV_32F ) diff --git a/modules/stitching/autocalib.cpp b/modules/stitching/autocalib.cpp index 88086d6fdb..82013fb22c 100644 --- a/modules/stitching/autocalib.cpp +++ b/modules/stitching/autocalib.cpp @@ -96,7 +96,7 @@ double estimateFocal(const vector &features, const vector(focals.size()) >= 2 * (num_images - 1)) + if (static_cast(focals.size()) >= num_images * (num_images - 1) / 2) { nth_element(focals.begin(), focals.end(), focals.begin() + focals.size()/2); return focals[focals.size()/2]; diff --git a/modules/stitching/blenders.cpp b/modules/stitching/blenders.cpp index 42c5c7f758..8a36bb5f96 100644 --- a/modules/stitching/blenders.cpp +++ b/modules/stitching/blenders.cpp @@ -68,7 +68,7 @@ void Blender::prepare(const vector &corners, const vector &sizes) void Blender::prepare(Rect dst_roi) { - dst_.create(dst_roi.size(), CV_32FC3); + dst_.create(dst_roi.size(), CV_16SC3); dst_.setTo(Scalar::all(0)); dst_mask_.create(dst_roi.size(), CV_8U); dst_mask_.setTo(Scalar::all(0)); @@ -78,7 +78,7 @@ void Blender::prepare(Rect dst_roi) void Blender::feed(const Mat &img, const Mat &mask, Point tl) { - CV_Assert(img.type() == CV_32FC3); + CV_Assert(img.type() == CV_16SC3); CV_Assert(mask.type() == CV_8U); int dx = tl.x - dst_roi_.x; @@ -86,8 +86,8 @@ void Blender::feed(const Mat &img, const Mat &mask, Point tl) for (int y = 0; y < img.rows; ++y) { - const Point3f *src_row = img.ptr(y); - Point3f *dst_row = dst_.ptr(dy + y); + const Point3_ *src_row = img.ptr >(y); + Point3_ *dst_row = dst_.ptr >(dy + y); const uchar *mask_row = mask.ptr(y); uchar *dst_mask_row = dst_mask_.ptr(dy + y); @@ -122,7 +122,7 @@ void FeatherBlender::prepare(Rect dst_roi) void FeatherBlender::feed(const Mat &img, const Mat &mask, Point tl) { - CV_Assert(img.type() == CV_32FC3); + CV_Assert(img.type() == CV_16SC3); CV_Assert(mask.type() == CV_8U); int dx = tl.x - dst_roi_.x; @@ -132,15 +132,17 @@ void FeatherBlender::feed(const Mat &img, const Mat &mask, Point tl) for (int y = 0; y < img.rows; ++y) { - const Point3f* src_row = img.ptr(y); - Point3f* dst_row = dst_.ptr(dy + y); + const Point3_* src_row = img.ptr >(y); + Point3_* dst_row = dst_.ptr >(dy + y); const float* weight_row = weight_map_.ptr(y); float* dst_weight_row = dst_weight_map_.ptr(dy + y); for (int x = 0; x < img.cols; ++x) { - dst_row[dx + x] += src_row[x] * weight_row[x]; + dst_row[dx + x].x += static_cast(src_row[x].x * weight_row[x]); + dst_row[dx + x].y += static_cast(src_row[x].y * weight_row[x]); + dst_row[dx + x].z += static_cast(src_row[x].z * weight_row[x]); dst_weight_row[dx + x] += weight_row[x]; } } @@ -169,7 +171,7 @@ void MultiBandBlender::prepare(Rect dst_roi) for (int i = 1; i <= num_bands_; ++i) { dst_pyr_laplace_[i].create((dst_pyr_laplace_[i - 1].rows + 1) / 2, - (dst_pyr_laplace_[i - 1].cols + 1) / 2, CV_32FC3); + (dst_pyr_laplace_[i - 1].cols + 1) / 2, CV_16SC3); dst_band_weights_[i].create((dst_band_weights_[i - 1].rows + 1) / 2, (dst_band_weights_[i - 1].cols + 1) / 2, CV_32F); dst_pyr_laplace_[i].setTo(Scalar::all(0)); @@ -180,7 +182,7 @@ void MultiBandBlender::prepare(Rect dst_roi) void MultiBandBlender::feed(const Mat &img, const Mat &mask, Point tl) { - CV_Assert(img.type() == CV_32FC3); + CV_Assert(img.type() == CV_16SC3); CV_Assert(mask.type() == CV_8U); int top = tl.y - dst_roi_.y; @@ -212,13 +214,17 @@ void MultiBandBlender::feed(const Mat &img, const Mat &mask, Point tl) { for (int y = 0; y < dst_pyr_laplace_[i].rows; ++y) { - const Point3f* src_row = src_pyr_laplace[i].ptr(y); - Point3f* dst_row = dst_pyr_laplace_[i].ptr(y); + const Point3_* src_row = src_pyr_laplace[i].ptr >(y); + Point3_* dst_row = dst_pyr_laplace_[i].ptr >(y); const float* weight_row = weight_pyr_gauss[i].ptr(y); for (int x = 0; x < dst_pyr_laplace_[i].cols; ++x) - dst_row[x] += src_row[x] * weight_row[x]; + { + dst_row[x].x += static_cast(src_row[x].x * weight_row[x]); + dst_row[x].y += static_cast(src_row[x].y * weight_row[x]); + dst_row[x].z += static_cast(src_row[x].z * weight_row[x]); + } } dst_band_weights_[i] += weight_pyr_gauss[i]; } @@ -265,14 +271,18 @@ Rect resultRoi(const vector &corners, const vector &sizes) void normalize(const Mat& weight, Mat& src) { CV_Assert(weight.type() == CV_32F); - CV_Assert(src.type() == CV_32FC3); + CV_Assert(src.type() == CV_16SC3); for (int y = 0; y < src.rows; ++y) { - Point3f *row = src.ptr(y); + Point3_ *row = src.ptr >(y); const float *weight_row = weight.ptr(y); for (int x = 0; x < src.cols; ++x) - row[x] *= 1.f / (weight_row[x] + WEIGHT_EPS); + { + row[x].x = static_cast(row[x].x / (weight_row[x] + WEIGHT_EPS)); + row[x].y = static_cast(row[x].y / (weight_row[x] + WEIGHT_EPS)); + row[x].z = static_cast(row[x].z / (weight_row[x] + WEIGHT_EPS)); + } } } @@ -296,7 +306,7 @@ void createLaplacePyr(const vector &pyr_gauss, vector &pyr_laplace) for (size_t i = 0; i < pyr_laplace.size() - 1; ++i) { pyrUp(pyr_gauss[i + 1], tmp, pyr_gauss[i].size()); - pyr_laplace[i] = pyr_gauss[i] - tmp; + subtract(pyr_gauss[i], tmp, pyr_laplace[i]); } pyr_laplace[pyr_laplace.size() - 1] = pyr_gauss[pyr_laplace.size() - 1].clone(); } @@ -311,6 +321,7 @@ void restoreImageFromLaplacePyr(vector &pyr) for (size_t i = pyr.size() - 1; i > 0; --i) { pyrUp(pyr[i], tmp, pyr[i - 1].size()); - pyr[i - 1] += tmp; + add(tmp, pyr[i - 1], pyr[i - 1]); } } + diff --git a/modules/stitching/main.cpp b/modules/stitching/main.cpp index c9960f8bdd..2228725179 100644 --- a/modules/stitching/main.cpp +++ b/modules/stitching/main.cpp @@ -401,7 +401,6 @@ int main(int argc, char* argv[]) cameras[i].R, masks_warped[i], INTER_NEAREST, BORDER_CONSTANT); } - // Convert to float for blending vector images_warped_f(num_images); for (int i = 0; i < num_images; ++i) images_warped[i].convertTo(images_warped_f[i], CV_32F); @@ -426,7 +425,7 @@ int main(int argc, char* argv[]) LOGLN("Compositing..."); t = getTickCount(); - Mat img_warped, img_warped_f; + Mat img_warped, img_warped_s; Mat dilated_mask, seam_mask, mask, mask_warped; Ptr blender; double compose_seam_aspect = 1; @@ -461,7 +460,7 @@ int main(int argc, char* argv[]) // Warp the current image warper->warp(img, static_cast(cameras[img_idx].focal), cameras[img_idx].R, img_warped); - img_warped.convertTo(img_warped_f, CV_32F); + img_warped.convertTo(img_warped_s, CV_16S); img_warped.release(); img.release(); @@ -497,7 +496,7 @@ int main(int argc, char* argv[]) } // Blend the current image - blender->feed(img_warped_f, mask_warped, corners[img_idx]); + blender->feed(img_warped_s, mask_warped, corners[img_idx]); } Mat result, result_mask; -- GitLab