It might be easiest to roll your own solution. Below is the current OpenCV implementation for moving from gray to RGBA:
template<typename _Tp> struct Gray2RGB { typedef _Tp channel_type; Gray2RGB(int _dstcn) : dstcn(_dstcn) {} void operator()(const _Tp* src, _Tp* dst, int n) const { if( dstcn == 3 ) for( int i = 0; i < n; i++, dst += 3 ) { dst[0] = dst[1] = dst[2] = src[i]; } else { _Tp alpha = ColorChannel<_Tp>::max(); for( int i = 0; i < n; i++, dst += 4 ) { dst[0] = dst[1] = dst[2] = src[i]; dst[3] = alpha; } } } int dstcn; };
This is where the actual cvtColor call comes in:
case CV_GRAY2BGR: case CV_GRAY2BGRA: if( dcn <= 0 ) dcn = 3; CV_Assert( scn == 1 && (dcn == 3 || dcn == 4)); _dst.create(sz, CV_MAKETYPE(depth, dcn)); dst = _dst.getMat(); if( depth == CV_8U ) CvtColorLoop(src, dst, Gray2RGB<uchar>(dcn));
This code is contained in the color.cpp file in the imgproc
library.
As you can see, since you are not setting the dstCn
parameter in your cvtColor
calls, it defaults to dcn = 3
. To switch from gray to BGRA, set dstCn
to 4. Since the default color order of OpenCV is BGR, you still need to change the color channels so that it looks right (provided that you get image data from the OpenCV function), Thus it may be appropriate to implement your own converter, perhaps by following the example above or using ypnos
answer here .
Also, look at my other answer that describes how to integrate OpenCV with Qt.
source share