OpenCV:带有 alpha 通道的 PNG 图像

OpenCV: PNG image with alpha channel

我是 OpenCV 的新手,我已经完成了一个小的 POC 来从一些 URL.
中读取图像 我正在使用视频捕获从 URL 中读取图像。代码如下:

VideoCapture vc;
vc.open("http://files.kurento.org/img/mario-wings.png");
if(vc.isOpened() && vc.grab()) 
{
       cv::Mat logo;
       vc.retrieve(logo);
       cv::namedWindow("t");
       imwrite( "mario-wings-opened.png", logo);
       cv::imshow("t", logo);
       cv::waitKey(0);
       vc.release();
}

此图片未正确打开,可能是由于 alpha 通道。 保留alpha通道并正确获取图像的方法是什么?

感谢任何帮助。
-谢谢

预期输出

实际产量

如果你只加载一张图片,我建议你使用imread代替,另外,你还需要指定imread的第二个参数来加载alpha通道,即CV_LOAD_IMAGE_UNCHANGEDcv::IMREAD_UNCHANGED,取决于版本(在最坏的情况下 -1 也有效)。

据我所知,VideoCaptureclass 不会加载 images/video 第 4 个频道。由于您使用的是网络 url,因此无法使用 imread 加载图像,但您可以使用任何方法下载数据(例如 curl),然后使用 imdecode用数据缓冲区得到cv::Mat。 OpenCV 是一个用于图像处理的库,而不是用于下载图像的库。

如果你想把它画在另一张图片上,你可以这样做:

/**
 * @brief Draws a transparent image over a frame Mat.
 * 
 * @param frame the frame where the transparent image will be drawn
 * @param transp the Mat image with transparency, read from a PNG image, with the IMREAD_UNCHANGED flag
 * @param xPos x position of the frame image where the image will start.
 * @param yPos y position of the frame image where the image will start.
 */
void drawTransparency(Mat frame, Mat transp, int xPos, int yPos) {
    Mat mask;
    vector<Mat> layers;

    split(transp, layers); // seperate channels
    Mat rgb[3] = { layers[0],layers[1],layers[2] };
    mask = layers[3]; // png's alpha channel used as mask
    merge(rgb, 3, transp);  // put together the RGB channels, now transp insn't transparent 
    transp.copyTo(frame.rowRange(yPos, yPos + transp.rows).colRange(xPos, xPos + transp.cols), mask);
}