OpenCV 2.4 Jpeg to PNG with alpha channel

2020-07-10 06:45发布

问题:

I have a JPEG and a Mask. I want to create a PNG with the three JPEG channels and the alpha channel should be the Mask. How can I achieve this with OpenCV?

Regards

回答1:

std::vector<cv::Mat> channels;
cv::split(jpgImage, channels);
channels.push_back(mask);
cv::Mat bgraImage;
cv::merge(channels, bgrAImage);

Documentation for split and merge functions



回答2:

Thanks for your answer, I found a second solution:

cv::Mat transparent( height, width, CV_8UC4);
cv::Mat srcImg[] = {JPEG_img, alpha_Mask};
int from_to[] = { 0,0, 1,1, 2,2, 3,3 };
cv::mixChannels( srcImg, 2, &transparent, 1, from_to, 4 );

This works perfect, not sure which solution is better.



标签: c++ opencv