adding an alpha channel to a gpuMat?
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?
either your images don't have the same size, or are empty()
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.what is
mat.type()
,mat.depth()
andmat.channels()
for your bmp images ?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.ok. main question now is, why does it think it is BGR555 ? (16bits in 2 channels)
ohh, elephant in the room ;)
aha my mistake! changing to
cv::cuda::cvtColor(tmpBg, tmpBg, CV_8UC4);
gives me:Assertion failed (src.channels() == 2) in
anonymous-namespace'::BGR555_to_BGR,`still, no cigar (that's the same as type() returns.)
read above comment again ...