gpt4 book ai didi

ios - Open CV - iOS PNG Overlaying images with transparent background 显示白色背景

转载 作者:太空宇宙 更新时间:2023-11-03 22:44:03 24 4
gpt4 key购买 nike

我正在使用以下 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.

}

图像在相机上产生白色背景。

image

如您所见,图像的背景显示为白色,但它是透明的。

非常感谢您的帮助。

最佳答案

我自己解决了。

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

关于ios - Open CV - iOS PNG Overlaying images with transparent background 显示白色背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50448432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com