Ask Your Question

Revision history [back]

Yes the second approach is correct if you want to preserve the original data. Actually the first approach will have two elements with different headers but with a data pointer pointing to the exact same memory location.

In your first approach

Mat grayImage = image; if( image.type() != CV_8U ) cvtColor( image, grayImage, COLOR_BGR2GRAY );

could simply be replaced by

cvtColor( image, image, COLOR_BGR2GRAY );

because you are creating unneccesary extra

Yes the second approach is correct if you want to preserve the original data. Actually the first approach will have two elements with different headers but with a data pointer pointing to the exact same memory location.

In your first approach

Mat grayImage = image;
if( image.type() != CV_8U ) cvtColor( image, grayImage, COLOR_BGR2GRAY );

);

could simply be replaced by

cvtColor( image, image, COLOR_BGR2GRAY );

);

because you are creating unneccesary extraextra headers. However, some functions in openCV do a check of the headers, because they don't allow identical matrices to be used for writing and reading. However only having different headers will still create problems when executing the function itself.