Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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???
Posted
Comments
Sergey Alexandrovich Kryukov 4-Sep-12 14:45pm    
Not a valid question. "Right" or "wrong" depends on what you want to achieve.
--SA
wuling 4-Sep-12 21:42pm    
dear Kryukov,
If I don't use tbb, I get right result.
But I use tbb, then i get wrong.
I don't know what's happening.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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