Ask Your Question

sarah_a's profile - activity

2018-06-01 05:25:25 -0600 received badge  Popular Question (source)
2014-05-30 00:10:54 -0600 asked a question problem with converting uchar mat to float mat

I have a vector of Mat in C++:

std::vector<cv::Mat>  A;

which is filled with matrices of CV_8UC1 type. I want to have A in another vector of Mat with float type. this is my code:

std::vector<cv::Mat> B( A.size() );
for( int i=0; i< B.size(); ++i )
{
    B[i] = cv::Mat::zeros( A[i].rows, A[i].cols, CV_32FC1 ); 
    A[i].convertTo( B[i], CV_32FC1 );
}

but the conversion doesn't work correctly and the values inside B are not correct.

If instead of the above code, I use the following code it works correctly:

std::vector<cv::Mat> B( A.size() );
for( int i=0; i< B.size(); ++i )
{
    B[i] = cv::Mat::zeros( A[i].rows, A[i].cols, CV_32FC1 ); 
    for( int r=0; r<B[i].rows; ++r)
        for( int c=0; c<B [i].cols; ++c)
            B[i].at<float>(r,c) = (float) A[i].at<u_int8_t>( r,c ); 
 }

Can anyone tell me what's wrong with the first code?