簡體   English   中英

打開 CV - iOS PNG 具有透明背景的疊加圖像顯示白色背景

[英]Open CV - iOS PNG Overlaying images with transparent background shows white background

我正在使用以下 open cv 方法將 UIImage 轉換為 Matrix。

cv::Mat gtpl;// Stores BGRA matrix
UIImage *tplImg = [UIImage imageNamed:@"lion"];
cv::Mat tpl;

UIImageToMat(tplImg, tpl); //Converts Image to Matrix

cv::cvtColor(tpl, gtpl, CV_RGBA2BGRA); // Converts matrix to 4 channels and save into gtpl variable.


//While Adding it to camera's live feed


- (void)processImage:(cv::Mat &)img {

cv::Rect roi( cv::Point(100, 100), cv::Size(gtpl.size()));//Creating rect on which image will be mapped


cv::Mat destinationROI = img( roi );

 cv::Mat channels[4];
 split(gtpl, channels);
 gtpl.copyTo( destinationROI ); //Copying gtpl matrix onto cameras matrix.

}

圖像在相機上產生白色背景。

圖片

如您所見,圖像的背景顯示為白色,但它是透明的。

我會很感激你的幫助。

我自己解決了。

cv::Mat overlayImage(const cv::Mat &background, const cv::Mat &foreground,
                     cv::Mat &output, cv::Point2i location)
{
    background.copyTo(output);


    // start at the row indicated by location, or at row 0 if location.y is negative.
    for(int y = std::max(location.y , 0); y < background.rows; ++y)
    {

        int fY = y - location.y; // because of the translation

        // we are done of we have processed all rows of the foreground image.
        if(fY >= foreground.rows)
        break;

        // start at the column indicated by location,

        // or at column 0 if location.x is negative.
        for(int x = std::max(location.x, 0); x < background.cols; ++x)
        {
            int fX = x - location.x; // because of the translation.

            // we are done with this row if the column is outside of the foreground image.
            if(fX >= foreground.cols)
            break;

            // determine the opacity of the foregrond pixel, using its fourth (alpha) channel.
            double opacity;
            try{

                opacity = ((double)foreground.data[fY * foreground.step + fX * foreground.channels() + 3]) / 255.;
            }
            catch(Exception e){

            }


            // and now combine the background and foreground pixel, using the opacity,

            // but only if opacity > 0.
            for(int c = 0; opacity > 0 && c < output.channels(); ++c)
            {
                try{
                    if (foreground.data != nil)
                    {
                        unsigned char foregroundPx =
                        foreground.data[fY * foreground.step + fX * foreground.channels() + c];
                        unsigned char backgroundPx =
                        background.data[y * background.step + x * background.channels() + c];
                        output.data[y*output.step + output.channels()*x + c] =
                        backgroundPx * (1.-opacity) + foregroundPx * opacity;
                    }
                }catch(Exception e){

                }
            }
        }
    }

    return output;
}

暫無
暫無

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

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