簡體   English   中英

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也可以)。

據我所知, VideoCapture類不會加載第4個通道的圖像/視頻。 由於您使用的是Web網址,因此無法使用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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM