Click here to Skip to main content
15,891,316 members

How to use tbb::parallel_for & tbb::blocked_range2d

wuling asked:

Open original thread
Hi all:
I don't know to use tbb::parall_for & tbb::backed_range2d ,if i have a four for loop.
for example:

C++
void  Filter_Giws ( const Mat src,Mat &dst, const int win_size )
{
 int num_rows=0, num_cols=0;
 int half_win=0;
 int ir=0, ic=0;
 int iwr=0, iwc=0;
 int r_begin=0, r_end=0;/* vertical limits of the filtering operation */
 int c_begin=0, c_end=0;/* horizontal limits of the filtering operation */
 int wr_begin=0, wr_end=0;/* vertical limits of the filtering window */
 int wc_begin=0, wc_end=0;/* horizontal limits of the filtering window */
 int center_val=0;
 int cur_val=0;
 double weight=0;
 double weight_sum=0;
 double conv_sum=0;

 Mat dst2;
 Mat src2;

 CV_FUNCNAME("Filter_Giws");

 tbb::task_scheduler_init init;

 __CV_BEGIN__;
 CV_ASSERT(src.type()==CV_8UC1);

 half_win = win_size / 2;

 src2.create(cv::Size(src.rows+2*half_win,src.cols+2*half_win),CV_8UC1);
 copyMakeBorder(src,src2,half_win,half_win,half_win,half_win,BORDER_CONSTANT);

 num_rows = src2.rows;//get_num_rows ( in_img );
 num_cols =src2.cols;// get_num_cols ( in_img );

 dst.create(src.size(),src.type());

 /*
    Determine the limits of the filtering operation. Pixels
    in the output image outside these limits are set to 0.
  */
 r_begin = half_win;
 r_end = num_rows - half_win;
 c_begin = half_win;
 c_end = num_cols - half_win;

 /* Initialize the vertical limits of the filtering window */
 wr_begin = 0;
 wr_end = win_size;

 /* For each image row */

 tbb::parallel_for(tbb::blocked_range2d<int,int>(r_begin,r_end,c_begin,c_end),
   ParallelApplyGiwsFilter(src2,dst,win_size),tbb::auto_partitioner());

 init.terminate();
.......
}


&
C#
class ParallelApplyGiwsFilter
{
public:
    ParallelApplyGiwsFilter(Mat  imgin,Mat& imgout,int win_size) : _imgin(imgin),_imgout(imgout),_win_size(win_size) {}
    void operator() (const tbb::blocked_range2d<int,int>& r) const
    {
        int wc_begin=0;
        int center_val=0;
        int cur_val=0;
        double weight=0;
        double weight_sum=0;
        double conv_sum=0;
        int wr_begin = 0;
        int wr_end = _win_size;
        int wc_end=0;
        int iwr=0;
        int iwc=0;

        for(size_t i=r.rows().begin(); i!=r.rows().end(); ++i)
        {
            /* Initialize the horizontal limits of the filtering window */
            wc_begin = 0;
            wc_end = _win_size;

            /* For each image column */
            for ( size_t j=r.cols().begin(); j!=r.cols().end(); ++j )
            {
                center_val = _imgin.at<uchar>(i,j);
                conv_sum = 0.0;
                weight_sum = 0.0;

                for ( iwr = wr_begin; iwr < wr_end; iwr++ )
                {
                    for ( iwc = wc_begin; iwc < wc_end; iwc++ )
                    {
                        cur_val =_imgin.at<uchar>(iwr,iwc) ;

                        /* Determine the weight for the current pixel */
                        if ( cur_val == center_val )
                        {
                            weight = 2.0;
                        }
                        else
                        {
                            weight = 1.0 / std::abs ( cur_val - center_val );
                        }
                        conv_sum += weight * cur_val;
                        weight_sum += weight;
                    }
                }
                _imgout.at<uchar>(i-2,j-2)=saturate_cast<uchar>(conv_sum/weight_sum);
                /* Update the horizontal limits of the filtering window */
                wc_begin++;
                wc_end++;
            }

            /* Update the vertical limits of the filtering window */
            wr_begin++;
            wr_end++;
        }
    }
private:
    Mat _imgin,&_imgout;
    int _win_size;
};


Is it right???
Tags: C++

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900