dct 2-D not working

asked 2015-02-14 02:08:12 -0600

sp gravatar image

Hello to all.I am trying to implement a cv::dct on c++ language inside visual studio 2012. My code is :

cv::dct(y_values,dct1); where y_values is an std::vector containing the Y values of an image (Y Cb Cr) , and dct1 is the std::vector for the output. Both are the same size (2-D) and their sizes are even numbers.

The error is: OpenCv error: Assertion failed (0 <= i && i < (int)(vv.size()) in cv::InputArray:getMat.

Thanks for your time

edit retag flag offensive close merge delete

Comments

please show, how you get to y_values. also, those should be cv::Mat. there is a conversion between both, but we need to see your construct there, to find the error.

also, you will probably need to convert your input to float to apply dct/dft

berak gravatar imageberak ( 2015-02-14 02:17:21 -0600 )edit

Hello and thanks for your quick answer. The y_values is nothing more than a vector: std::vector< std::vector<int> > y_values(height, std::vector<int>(width)); , containing values. The extraction of the pixel values has not happened with open_cv library, so i only need to implement the dct using open_cv.

sp gravatar imagesp ( 2015-02-14 02:22:02 -0600 )edit

ah, ok, vector<vector<int>> will not work, you need consecutive (continuous) pixel data.

try something like :

Mat ocv_y;
for (size_t i=0; i<height; i++)
    ocv_y.push_back( y_values[i] );
ocv_y.convertTo(ocv_y, CV_32F);

Mat dct_result;
dct(ocv_y, dct_result);
berak gravatar imageberak ( 2015-02-14 02:33:39 -0600 )edit

Seems very interesting. Even though how can i do the above with an std::vector<int> y_values (height * width); ??

sp gravatar imagesp ( 2015-02-14 03:05:31 -0600 )edit
1

yes, like:

std::vector<int> y_values (height * width); 
Mat m(y_values); // flat 1d
m = m.reshape(1,width); // back to 2d (just make a new Mat header, no pixels copied)
berak gravatar imageberak ( 2015-02-14 03:09:09 -0600 )edit

I am sorry but i am kinda confused. I understand that this can be implemented with a Mat. But in documentation it mentioned also std::vector. The vector mentioned above is flat (1-D). So why do we need the Mat?

sp gravatar imagesp ( 2015-02-14 03:12:28 -0600 )edit

you can feed a std::vector to dct(), (it will be auto-converted to a cv::Mat) but in this case, it's only 1d, it does not know the shape of your original image.

also, again, float or double data needed

berak gravatar imageberak ( 2015-02-14 03:19:12 -0600 )edit

Thanks again for all your time. in your first example both ocv_y and dct_result would not be 1-D?

sp gravatar imagesp ( 2015-02-14 03:24:33 -0600 )edit

the 1st example is pushing rows of your vector<vector<>> into a Mat, expensive copy, but retaining the shape, the 2nd example keeps a pointer to the vector data, and builds a new 2d Mat header around it

both 2d, and the result will be 2d, too.

berak gravatar imageberak ( 2015-02-14 03:33:41 -0600 )edit

Thanks again. One last question: At some point at my program i want to access the Mat object's specific position as in std::vector vector1[i][j]. Is that possible? I get the error: expression must have pointer to object or handle-to-C++/CLI array type

sp gravatar imagesp ( 2015-02-14 04:00:07 -0600 )edit