adding an alpha channel to a gpuMat?

asked 2018-11-20 05:11:26 -0500

antithing gravatar image

updated 2018-11-20 06:23:43 -0500

I have three images, a foreground, a background and a mask. I need to add the mask as an alpha channel to the foreground, then use cv::cuda::alphaComp to composite the results.

I am hitting some issues getting the alpha channel added, however. My code is:

    cv::Mat fore = cv::imread("fore.bmp",1);
cv::Mat back = cv::imread("back.bmp",1);
cv::Mat alpha = cv::imread("matte.jpg", 0);


std::cout << fore.rows << " " << fore.cols << std::endl;
std::cout << back.rows << " " << back.cols << std::endl;
std::cout << alpha.rows << " " << alpha.cols << std::endl;

cv::Mat src1_rgba, src2_rgba, alphaImage;

cv::cuda::GpuMat tmpImage, tmpMask, tmpAlphaImage, tmpBg, outPut;
std::vector<cv::cuda::GpuMat> channels;

tmpImage.upload(fore);
tmpMask.upload(alpha);
tmpBg.upload(back);

cv::cuda::split(tmpImage, channels); // break image into channels
channels.push_back(tmpMask); // append alpha channel
cv::cuda::merge(channels, tmpAlphaImage); // combine channels

cv::cuda::cvtColor(tmpBg, tmpBg, tmpAlphaImage.type());
cv::cuda::cvtColor(outPut, outPut, tmpAlphaImage.type());

cv::cuda::alphaComp(tmpAlphaImage, tmpBg, outPut, cv::cuda::ALPHA_OVER);


outPut.download(alphaImage);

This compiles, but crashes with:

OpenCV(4.0.0-pre) Error: Assertion failed (src.channels() == 2) in `anonymous-namespace'::BGR555_to_BGR, file D:\thirdParty\opencv_340\opencv-master\opencv-master\modules\cudaimgproc\src\color.cpp, line 315

What am i doing wrong here?

edit retag flag offensive close merge delete

Comments

either your images don't have the same size, or are empty()

berak gravatar imageberak ( 2018-11-20 06:05:49 -0500 )edit
1

Ah, thank you! I was missing a comma in cv::imread("fore.bmp" -1); Now, I get a new error, despite converting the Mats to be the same type. (Please see updated question) thank you once again for your time.

antithing gravatar imageantithing ( 2018-11-20 06:22:41 -0500 )edit

what is mat.type() , mat.depth() and mat.channels() for your bmp images ?

berak gravatar imageberak ( 2018-11-20 07:21:41 -0500 )edit
1

I converted them to jpg, to get everything the same. This now gives me rows: 1080 cols: 1920 channels: 3 type: 16 depth: 0 for both, and type = 0 for the alpha.

antithing gravatar imageantithing ( 2018-11-20 07:36:57 -0500 )edit
1

ok. main question now is, why does it think it is BGR555 ? (16bits in 2 channels)

ohh, elephant in the room ;)

cv::cuda::cvtColor(tmpBg, tmpBg, tmpAlphaImage.type()); //<-- you need a COLOR_XXX2YYY enum here, not the mat's type !
berak gravatar imageberak ( 2018-11-20 07:46:07 -0500 )edit

aha my mistake! changing to cv::cuda::cvtColor(tmpBg, tmpBg, CV_8UC4); gives me: Assertion failed (src.channels() == 2) inanonymous-namespace'::BGR555_to_BGR,`

antithing gravatar imageantithing ( 2018-11-20 08:05:19 -0500 )edit
1

still, no cigar (that's the same as type() returns.)

read above comment again ...

berak gravatar imageberak ( 2018-11-20 08:07:17 -0500 )edit

AHA! COLOR_BGR2BGRA. Of Course. Sorry for my idiocy. Thank you yet again for your patience. Working great.

antithing gravatar imageantithing ( 2018-11-20 08:13:27 -0500 )edit