Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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:<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.