Fastest conversion from TriclopsImage to cv::Mat

asked 2014-02-11 09:10:40 -0500

mark_vision gravatar image

updated 2014-02-12 05:05:07 -0500

Hi there, I'm forced to use the Bumblebee stereo algorithm that use the TriclopsImage and TriclopsImage16 data structures defined by Triclops. I want to convert those structures into a cv::Mat in the fastest way. I've tried with memcpy but it does not work:

Mat srcright(HEIGHT, WIDTH, CV_8UC3);    
for (int i = 0; i < HEIGHT; i++) {
                    memcpy(&srcleft.ptr(i)[0], &rectified_left_color_image.blue[i * rectified_left_color_image.rowinc], WIDTH * sizeof (uchar));
                    memcpy(&srcleft.ptr(i)[1], &rectified_left_color_image.blue[i * rectified_left_color_image.rowinc], WIDTH * sizeof (uchar));
                    memcpy(&srcleft.ptr(i)[2], &rectified_left_color_image.blue[i * rectified_left_color_image.rowinc], WIDTH * sizeof (uchar));
        }

it produces kind of a shrinked image. With the nested for and the point to point assignement with at() is ok but I would prefer a fastest way to do it.

EDIT Now it works, here there's the code:

vector<Mat> channels;

channels.push_back(Mat(HEIGHT, WIDTH, CV_8UC1, rectified_left_color_image.blue));
channels.push_back(Mat(HEIGHT, WIDTH, CV_8UC1, rectified_left_color_image.green));
channels.push_back(Mat(HEIGHT, WIDTH, CV_8UC1, rectified_left_color_image.red));

merge(channels, srcleft);
edit retag flag offensive close merge delete

Comments

besides your memcpy being horribly broken, you seem to copy only the blue channel ?


what if you just construct a mat from the triclops data , and convert it later ?

Mat tri16(HEIGHT, WIDTH, CV_16UC3, rectified_left_color_image );

Mat rgb; tri16.convertTo(CV_8UC3, rgb);

berak gravatar imageberak ( 2014-02-12 02:30:23 -0500 )edit

This is interesting. The blue channel thing is just because of my copy/paste haste, the original code was correct. Now i try with your solution. What do you mean with "horribly broken", anyway?

mark_vision gravatar imagemark_vision ( 2014-02-12 04:48:44 -0500 )edit

broken means: you tried to copy a 1channel row to 'interleaved' pixels.

hmm, if the color image comes in seperate channels, ignore my remark above.

your channels / merge solution seems to be the best approach possible

berak gravatar imageberak ( 2014-02-12 05:10:55 -0500 )edit