Sagar
Sagar

Reputation: 237

convert image from BGR to ARGB using opencv

Upvotes: 7

Views: 17041

Answers (2)

sgarizvi
sgarizvi

Reputation: 16796

The required operation can be accomplished by swapping the image channels using cv::mixChannels as follows:

cv::Mat bgr, bgra;

//bgr initialization code here...
//.
//.
//.
cv::cvtColor(bgr, bgra, cv::COLOR_BGR2BGRA);
cv::Mat abgr(bgra.size(), bgra.type());
int from_to[] = { 0,3, 1,1, 2,2, 3,0 };
cv::mixChannels(&bgra,1,&abgr,1,from_to,4);

from_to array is the mapping function which specifies which channels from source will be copied to which channels of the destination image. The pairs indicate that channel number 0 of the input will be copied to channel number 3 of the output, 1 to 1, 2 to 2, and channel number 3 will be copied to channel number 0 of the output.

Alternatively, we can split the image channels, swap the required channels and merge again. It can be done as follows:

cv::cvtColor(bgr, bgra, cv::COLOR_BGR2BGRA);

std::vector<cv::Mat> channels_bgra;
cv::split(bgra, channels_bgra);

std::vector<cv::Mat> channels_abgr = { channels_bgra[3], channels_bgra[1], channels_bgra[2], channels_bgra[0] };
cv::merge(channels_abgr, abgr);

Upvotes: 9

api55
api55

Reputation: 11420

OpenCV doesn't support ARGB or ABGR formats, so you will not be able to display it or use some of the functions on it... However, it is possible to create them with split and merge functions of OpenCV. Here is some code to explain what I mean.

cv::Mat src, final_image;
// fill src as you prefer
std::vector<cv::Mat> channels;
cv::split(src, channels); // this will put each channel in a mat in the vector

// swap or add channels in the vector
cv::Mat alpha(src.rows, src.cols, CV_8U, cv::Scalar(255));
channels.push_back(alpha);
std::reverse(channels.begin(), channels.end()); //needs <algorithm>

// merge the channels in one new image
cv::merge(channels, final_image); 

This can be done faster (maybe it will be just shorter) with the function mixChannels, but I will say that this one is a little bit more confusing.

Upvotes: 2

Related Questions