First time here? Check out the FAQ!

Ask Your Question
3

Do we need to optimize matrix operation of cv::Mat?

asked Dec 2 '13

stereomatching gravatar image

updated Oct 22 '17

clean and easy to read, but this kind of operation may generate two temporary objects

foreground_current = (frame_current_ - (1 - Beta_) * background_current_) / Beta_; //#1

Hard to read and verbose

//can't alter the value of background_current
background_current_.copyTo(temp_);
temp_ *= 1 - Beta_; //can't use (Beta_ - 1) since it is negative
frame_current_.copyTo(foreground_current_);
foreground_current_ -= temp_;
foreground_mask_ /= Beta_;

or write a for loop for it?

//same as for loop
OCV::transform_channels<uchar>(frame_current_, background_current_, foreground_current_, [=](uchar fr, uchar bg)
    {
        return cv::saturate_cast<Type>((fr - (1 - Beta_) * bg) / Beta_);
    });

What I want to ask is, do openCV provide some optimization for operator overloading? With the help of expression template, it is possible to eliminate the temporary objects of #1. Do openCV2 support this kind of optimization?Thanks

Preview: (hide)

1 answer

Sort by » oldest newest most voted
5

answered Dec 2 '13

Michael Burdinov gravatar image

You already has function in OpenCV that highly optimized for this specific task: addWeighted.

addWeighted(frame_current, 1/Beta, background_current, 1-1/Beta, 0, foreground_current);
Preview: (hide)

Comments

1

Thanks, this work.Looks like the operation overloading of cv::Mat are designed for fast prototyping only

stereomatching gravatar imagestereomatching (Dec 2 '13)edit
1

You are welcome

Question Tools

Stats

Asked: Dec 2 '13

Seen: 561 times

Last updated: Dec 02 '13