Ask Your Question

Iona's profile - activity

2017-11-10 09:06:49 -0600 received badge  Notable Question (source)
2017-02-05 23:41:39 -0600 received badge  Popular Question (source)
2014-09-19 16:28:57 -0600 asked a question Loop performance

Hello,

I have a loop within fast Fourier transform function which takes in the values and computes the real and imaginary parts accordingly.

It is working well within the function. However, this loop is taking the maximum time which is quite concerning. Removing it is reducing the time by few seconds but this loop is needed within the function.

Previously, it was a nested loop and I modified it into a single for loop. Any idea on how to further reduce the processing time for the following loop would be much appreciated.

     for (unsigned int iter = 1; iter < N; iter <<=1)
        {
           const unsigned int step = iter << 1;
           const double theta =  pi / double(iter);
           double wtemp = sin(theta * .5);
           double wreal = -2 * wtemp * wtemp;
           double wimag = sin(theta);
           //   Factors
           double wr = 1.0;
           double wi = 0.0;  
           for (unsigned int m = 0; m < iter; m++)
           {
              for (unsigned int i = m; i < N; i += step)
              {
                 const unsigned int j = i + iter;
                 double tempr= wr * real(data[j]) - wi * imag(data[j]);
                 double tempi= wr * imag(data[j]) + wi * real(data[j]);
                             complex<double> temp(tempr,tempi);
                             data[j]= data[i]- temp;
                             data[i] += temp;
              }
           }
        }

where data is complex vector of type double.

2014-09-17 15:34:52 -0600 asked a question Increase performance of the function

Hello,

I have fft function within opencv which is called from another function. Everything is working good. However, I do have performance concern as fft is taking time and I need to run the code several times per second. I tried modifying insert by using push_back instead but that didn't help much. Any idea how the function could be optimized so that it takes less run-time?

void* fft(vector<complex<double> > &data, int sign,unsigned  int N){
        double pi = - 3.14159265358979;
        if ( sign == 1 || sign == -1) {
        pi = sign * pi;
        } else {
                cout << "Format fft(data, num), num = +1(fft) and num = -1 (Ifft)"  << endl;
            exit(1);
        }
        unsigned int len = data.size();
        vector<complex<double> >::iterator it;
        it = data.end();

        if ( len != N) {
                unsigned int al = N - len;      
                data.insert(it,al,complex<double>(0,0));
        }

        unsigned int K = (unsigned int) pow(2.0,ceil(log10(static_cast<double>(N))/log10(2.0)));
        vector<complex<double> >::iterator it1;
        it1 = data.end();

        if ( N < K) {
                unsigned int al = K - N;
                data.insert(it1,al,complex<double>(0,0));
                N = K;
        }

      bitreverse(data);

         for (unsigned int iter = 1; iter < N; iter <<=1)
            {
               const unsigned int step = iter << 1;

               const double theta =  pi / double(iter);

               double wtemp = sin(theta * .5);
               //   Multipliers
               double wreal = -2 * wtemp * wtemp;
               double wimag = sin(theta);

               //   Factors
               double wr = 1.0;
               double wi = 0.0;
               //   Iteration through two loops

               for (unsigned int m = 0; m < iter; m++)
               {
                  //   Iteration within m
                  for (unsigned int i = m; i < N; i += step)
                  {
                     //   jth position
                     const unsigned int j = i + iter;

                     double tempr= wr * real(data[j]) - wi * imag(data[j]);
                     double tempi= wr * imag(data[j]) + wi * real(data[j]);

                                 complex<double> temp(tempr,tempi);
                                 data[j]= data[i]- temp;
                                 data[i] += temp;

                  }
                  //   Twiddle Factors updated
              wtemp = wr;
                  wr += wr * wreal - wi * wimag;
                  wi += wi * wreal + wtemp * wimag ;
               }

            }

         if ( sign == -1) {
                 double scale = 1.0/double(N);
                 for (unsigned int i = 0; i < N; i++){
                         data[i]*=scale;
                 }
         }



        // Place holder
        return 0;
}
2014-08-28 16:18:13 -0600 received badge  Editor (source)
2014-08-28 15:09:11 -0600 asked a question Faster alternative to push_back

Hello,

I am trying to modify a loop within a loop to make it run faster. I ma doing this by replacing push_back with insert. However, insert does not seem to work for me as it is giving build errors.

for (int i =0; i < rows; i++) 
{
    vector<double> temp1;
    temp1.insert(temp1.end(), signal.begin(), signal.end());
}

This does not seem to work. It's push_back which works is:

vector<double> temp1;
temp1.reserve(512);
for (int i =0; i < rows; i++) 
{
            for (int j=0;j < cols;j++) 
            {
                    temp1.push_back(signal[i][j]);
            }
  }

I believe the error is occurring because signal is of type vector < vector < double > > while temp1 is vector < double > And since signal is further being passed into another function, I would not want to change the type. Any suggestion would be appreciated.

2014-08-23 18:06:57 -0600 commented answer Modify loop for faster execution

Thanks for your comment. However, initializing temp and oup before the loop is giving run time error. Here's how I modified the code which helped reduction to few msec. Any further ideas would be much appreciated.

vector&lt;double&gt; temp_row;
temp_row.reserve(512);
for (int i =0; i &lt; rows; i++) {
            for (int j=0;j &lt; cols;j++) {
                    double temp = signal[i][j];
                    temp_row.push_back(temp);
                   }
                    vector&lt;double&gt; oup;
                    branch_lp_dn(name,temp_row,oup);
                    temp_row.clear();

                    for (int j=0;j &lt; (int) oup.size();j++) {
                    lp_dn1[i][j] = oup[j];
                    }
    }
2014-08-21 16:17:27 -0600 asked a question Modify loop for faster execution

Hello,

Here's the code which works well for analysis while performing discrete wavelet transform. I've been wondering if this loop could be modified to make the execution faster? Any help would be appreciated.

int rows = signal.size();
int cols = signal[0].size();
int cols_lp1 =(int) ceil( (double) cols / 2);
vector<vector<double> > lp_dn1(rows, vector<double>( cols_lp1));

// Implementing row filtering and column downsampling in each branch.
for (int i =0; i < rows; i++) {
            vector<double> temp_row;
            for (int j=0;j <  cols;j++) {
                    double temp = signal[i][j];
                    temp_row.push_back(temp);
                   }
                    vector<double> oup;
                    branch_lp_dn(name,temp_row,oup);
                    temp_row.clear();

                    for (int j=0;j < (int) oup.size();j++) {
                    lp_dn1[i][j] = oup[j];
                    }
    }
2014-08-20 12:54:22 -0600 commented answer Reconstructing vector from given image

J decides how many levels of dwt the image would go through. If J is 1, then 2 levels of dwt and if J is 2 then 3 levels of dwt.

I really appreciate your help. But your suggestion distorted the image completely.

2014-08-19 18:01:53 -0600 asked a question Reconstructing vector from given image

Hello,

I have a vector:

vector<vector<double> > output2(rr1, vector<double>(cc1));
output2 = wt_output;

IplImage *OutputImg; 
CvSize img;

output2 holds the image information which is basically the output of 2D discrete wavelet transform, wt_output. I wanted to construct an image out of the vector. So I went through the following loop:

    for (int i = 0; i < img.height; i++ ) 
    {
      for (int j = 0; j < img.width; j++ )
      {
        if ( wt_output[i][j] <= 0.0)
        {
          wt_output[i][j] = 0.0;
        }
        if ( i <= (img.height/pow(2.0,double(J))) && j<=(img.width/pow(2.0,double(J))) ) 
        {
          ((uchar*)(OutputImg->imageData + OutputImg->widthStep*i))[j]=(char) ( (wt_output[i][j] / max) * 255.0);
        } 
        else 
        {
          ((uchar*)(OutputImg->imageData + OutputImg->widthStep*i))[j]=(char) (wt_output[i][j]) ;
        }
      }
    }

I am able to construct and display the scaled image, OutputImg. However, the problem starts here: I need to convert OutputImg to vector again. Due to certain constraints, I do not want to use output2. Hence I went through the following loop to convert OutputImg to vector equivalent to output2.

    int height2, width2;
    height2 = OutputImg.rows;
    width2 = OutputImg.cols;
    CvSize size2;
    size2.width =width2;
    size2.height=height2;

    int rows2 =(int) height2;
    int cols2 =(int) width2;
    Mat matimg(OutputImg);

    vector<vector<double> > vec2(rows2, vector<double>(cols2));

    int k2 =1;
    for (int i2=0; i2 < rows2; i2++) {
      for (int j2 =0; j2 < cols2; j2++){
        unsigned char temp2;
        temp2 = ((uchar*) matimg.data + i2 * matimg.step)[j2  * matimg.elemSize() + k2 ];
        vec2[i2][j2] = (double) temp2;
      }
    }

The problem is the output vec2 is not equivalent to output2. What could be the problem in this loop. Any help would be appreciated.

2014-08-16 22:17:32 -0600 commented answer Runtime error while conversion of color space from RGb to YCbCr

This looks easy and understandable. Thank you Berak!

However, I have a question, "y", "cb" and "cr" are values returned from a different function. Hence they are returned as cv::Mat * instead of cv::Mat. I was wondering what would be a suitable way to convert cv::Mat * to cV::Mat?

2014-08-15 19:27:46 -0600 asked a question Runtime error while conversion of color space from RGb to YCbCr

Hello,

I have Y, Cr, Cb channels which I need to combine together into one RGB image. Here is the code I'm using which is giving me run time error. Any help would be appreciated.

    CvMat finalLayer; //output to store the merged ycbcr image
    cvMerge(y_output, cr_output, cb_output, NULL, &finalLayer); //merging ycbcr
    cv::Mat finalLayer1 = cv::cvarrToMat(&finalLayer); //convert IplImage to cv::Mat
    cv::Mat finalLayer2; //store RGB output image
    cvtColor(finalLayer1, finalLayer2, CV_YCrCb2BGR); //convert from ycbcr to rgb

    //display
    namedWindow("Final Image",CV_WINDOW_AUTOSIZE);
    imshow("Final Image",finalLayer2);waitKey(0);
2014-08-14 03:12:57 -0600 received badge  Student (source)
2014-08-13 19:36:11 -0600 commented answer Combining multiple blocks into one image

Thanks! Just two questions:

  1. namedWindow("combined image",CV_WINDOW_AUTOSIZE); will be placed before the first for loop, right?

  2. imshow("combined image", roi); will be placed immediately after the last line (small_blocks[i].copyTo(roi);) or after the last closing flower bracket?

2014-08-13 14:16:24 -0600 asked a question Combining multiple blocks into one image

Hello,

I had 256x256 image which I successfully split into 32x32 blocks. So in total I have 64 blocks. I did some operations on these blocks obtained. And now I want to combine these 64 32x32 blocks into 256x256 image. Here is the code I tried but in vain. Any help would be appreciated.

//combine blocks
for  ( int y= 0; y<256; y+=smallBlock.height )
{
    for  ( int x= 0 ; x<256; x+=smallBlock.width )
    {
        cv::Rect rcn=cv::Rect(x,y,smallBlock.width,smallBlock.height);
        smallBlock.copyTo(rcn);
    }
}

Here smallBlock has height 32 and width 32.

2014-08-12 20:23:02 -0600 received badge  Scholar (source)
2014-08-12 20:22:59 -0600 received badge  Supporter (source)
2014-08-11 17:22:50 -0600 asked a question dwt2 function in OpenCV

Hello,

I have been wondering if there is a function in OpenCV which has the same functionality as dwt2 in Matlab? I have a grayscale image which I need to decompose into four subbands. Any help would be appreciated.

Thanks,