Why does openCV's convertto function not work?

mans picture mans · Mar 4, 2014 · Viewed 18.8k times · Source

I have an image which has 4 channels and is in 4 * UINT8 format.

I am trying to convert it to 3 channel float and I am using this code:

images.convertTo(images,CV_32FC3,1/255.0);

After the conversion, the image is in a float format but still has 4 channels. How can I get rid of 4th (alpha) channel in OpenCV?

Answer

herohuyongtao picture herohuyongtao · Mar 4, 2014

As @AldurDisciple said, Mat::convertTo() is intended to be used for changing the data type of a Mat, not for changing the number of channels.

To work out, you should split it into two steps:

cvtColor(image, image, CV_BGRA2BGR);       // 1. change the number of channels
image.convertTo(image, CV_32FC3, 1/255.0); // 2. change type to float and scale