Ask Your Question
3

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

asked 2013-12-02 05:52:32 -0500

stereomatching gravatar image

updated 2017-10-22 13:04:32 -0500

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

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
5

answered 2013-12-02 07:14:20 -0500

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);
edit flag offensive delete link more

Comments

1

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

stereomatching gravatar imagestereomatching ( 2013-12-02 08:02:45 -0500 )edit
1

You are welcome

Michael Burdinov gravatar imageMichael Burdinov ( 2013-12-02 08:49:55 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-12-02 05:52:32 -0500

Seen: 291 times

Last updated: Dec 02 '13