Search code examples
iosobjective-copencvaugmented-reality

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


I am using following open cv method to convert UIImage to 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.

}

Image producing white background on camera.

image

As you can see the image's background is showing white but it is transparent.

I would appreciate your help.


Solution

  • I solved it by myself.

    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;
    }