How to convert CV_8UC1 Mat to CV_8UC3 with OpenCV?
Mat dst; Mat src(height, width, CV_8UC1, (unsigned char*) captureClient->data()); src.convertTo(dst, CV_8UC3);
but dst.channels () = 1
I found that the best way to do this is:
cvtColor(src, dst, COLOR_GRAY2RGB);
The image will look the same as when it was grayscale CV_8UC1, but it will be a 3-channel image of type CV_8UC3.
From documentation to convertTo
void Mat :: convertTo (Mat & m, int rtype, double alpha = 1, double beta = 0) const rtype - the desired type of destination matrix, or rather depth (since the number of channels will be the same as the original) . If rtype is negative, the destination matrix will be of the same type as the source.
void Mat :: convertTo (Mat & m, int rtype, double alpha = 1, double beta = 0) const
rtype - the desired type of destination matrix, or rather depth (since the number of channels will be the same as the original) . If rtype is negative, the destination matrix will be of the same type as the source.
3 , , . .
The convention is that for a type CV_8UC3, pixel values range from 0to 255, and for a type CV_32FC3from 0.0to 1.0. So you need to use the scaling factor 255.0instead 1.0:
CV_8UC3
0
255
CV_32FC3
0.0
1.0
255.0
Mat::convertTo(newImage, CV_32FC1, 255.0);