Ask Your Question

danfur's profile - activity

2019-06-17 18:45:56 -0500 received badge  Supporter (source)
2019-06-17 18:45:51 -0500 commented answer widthStep in C++ API

Thanks you so much! That did it. This saved my day. 🙏

2019-06-17 18:32:11 -0500 marked best answer widthStep in C++ API

Hi I had some legacy code in OpenCV C API that I want to port to C++.

In the code I have some wavelet coefficients computed into a vector: vector<vector<double>> detail This is done without OpenCv but I late ron want to use OpenCV to display and process results further.

I there store the vector detail into C API opencv Image dvImg = cvCreateImage( imgSz, 16, 1 ); and I do this using widthStep:

((ushort*)(dvImg->imageData + dvImg->widthStep*i))[j] = (short) (detail[i][j]);

Specifically it is a for loop that looks like this:

    int J = 6; //number of detail coefficients on the image
    vector<vector<double>>  detail(1*rowW, vector<double>(J * colW)); //detail coefficients
    cv::Size imgSz; // size of output image
    //the output dimensions I wish to store detail coefficients to.
    imgSz.width = J*colW;
    imgSz.height = 1*rowW;
   //the OpenCV 16-bit monochrome image Im storing to
    dvImg = cvCreateImage( imgSz, 16, 1 );

    //loop through the image
    for (int i = 0; i < imgSz.height; i++ ) {
        for (int j = 0; j < imgSz.width; j++ ){
          if ( detail[i][j] <= 0.0){
            //make sure no negative values
            detail[i][j] = 0.0;
          }
           //assign to opencv image
          ((ushort*)(dvImg->imageData + dvImg->widthStep*i))[j] = (short) (detail[i][j]);
        }
      }

Naively I thought I could simply replace this to cv::Mat for C++ API like this:

cv::Mat dvImg( imgSz, CV_16U);

And instead of:

((ushort*)(dvImg->imageData + dvImg->widthStep*i))[j] = (short) (detail[i][j]);

I would do:

dvImg.at<ushort>(j,i) = (short) (detail[i][j]);

But this doesnt seem to work, output is all scrambled, there are some particulars of how widthStep works that I seem to miss.

Helpful for any input.

2019-06-17 18:32:11 -0500 received badge  Scholar (source)
2019-06-17 15:32:54 -0500 asked a question widthStep in C++ API

widthStep in C++ API Hi I had some legacy code in OpenCV C API that I want to port to C++. In the code I have some wave