OpenCV: How to Convert the CV_8UC1 Dial to CV_8UC3

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

+12
source share
3 answers

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.

+27
source

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.

3 , , . .

+2

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:

Mat::convertTo(newImage, CV_32FC1, 255.0);
+2
source

All Articles